-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMS3.c
More file actions
125 lines (106 loc) · 9.15 KB
/
MS3.c
File metadata and controls
125 lines (106 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <pthread.h>
/* This is our thread function. It is like main(), but for a thread */
int j=0;//worker id
int sum=0;//total array sum
pthread_mutex_t mymutex;
int ary[50][50]={{1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91},
{2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7},
{45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93},
{4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94},
{5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95},
{6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96},
{7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96},
{42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98},
{9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99},
{10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100},
{1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91},
{2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7},
{45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93},
{4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94},
{5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95},
{6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96},
{7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96},
{42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98},
{9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99},
{10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100},
{1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91},
{2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7},
{45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93},
{4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94},
{5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95},
{6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96},
{7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96},
{42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98},
{9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99},
{10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100},
{1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91},
{2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7},
{45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93},
{4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94},
{5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95},
{6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96},
{7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96},
{42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98},
{9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99},
{10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100},
{1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91,1,11,21,9,41,51,61,71,2,91},
{2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7,2,12,1,82,42,0,66,11,82,7},
{45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93,45,2,23,33,14,53,63,73,83,93},
{4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94,4,14,24,34,44,54,64,74,5,94},
{5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95,5,76,25,64,45,55,65,75,85,95},
{6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96,6,16,26,36,46,56,56,76,86,96},
{7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96,7,17,27,37,47,57,67,77,87,96},
{42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98,42,33,28,38,48,58,68,93,88,98},
{9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99,9,19,29,28,49,59,39,88,89,99},
{10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100}};
//sum of row 1 is: 359 (50x50) 1,11,21,31,41 = 1795
//sum of row 2 is: 305 (50x50) 2,12,22,32,42 = 1525
//sum of row 3 is: 482 (50x50) 3,13,23,33,43 = 2410
//sum of row 4 is: 411 (50x50) 4,14,24,34,44 = 2055
//sum of row 5 is: 590 (50x50) 5,15,25,35,45 = 2950
//sum of row 6 is: 500 (50x50) 6,16,26,36,46 = 2500
//sum of row 7 is: 519 (50x50) 7,17,27,37,47 = 2595
//sum of row 8 is: 594 (50x50) 8,18,28,38,48 = 2970
//sum of row 9 is: 508 (50x50) 9,19,29,39,49 = 2540
//sum of row 10 is: 550 (50x50) 10,20,30,40,50 = 2750
//total sum should be: 4818 (50x50) = 120450
void *threadFunc(void *arg)
{
char *str;
int i = 0;
int subsum=0;
str=(char*)arg;
while(i < 50)
{
subsum+=ary[j][i];
//usleep(1);
printf("Worker %d says: %s\n",j+1,str);
++i;
}
sum+=subsum;
printf("The sum of row %d is: %d\n",j+1,subsum);
pthread_exit(NULL);
return NULL;
}
int main(void)
{
pthread_mutex_init(&mymutex, NULL);
pthread_t pth; // this is our thread identifier
int i = 0;
while(j<50)
{
/* Create worker thread */
pthread_create(&pth,NULL,threadFunc,"adding up the array...");
//Lock the semaphore
//pthread_mutex_lock(&mymutex);
/* wait for our thread to finish before continuing */
pthread_join(pth, NULL /* void ** return value could go here */);
j++;
//Unlock the semaphore
//pthread_mutex_unlock(&mymutex);
}
printf("The sum of the entire array is %d\n",sum);
pthread_exit(NULL);
return 0;
}