-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathexample.c
More file actions
117 lines (50 loc) · 1.04 KB
/
example.c
File metadata and controls
117 lines (50 loc) · 1.04 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
// Camilo Riviere COP 4610
// PSEUDOCODE
typedef struct{
int sum;
Mutex mcheck_cores;
Mutex sumL;
int completed;
int running;
double average;
Condition cond_cores;
int R;
int C;
} Global_Variables;
Global_Variables G;
// Have to get number of cores M with system call to linux.
// R = number of rows
// C = number of columns
// R = C
Mutex_Lock;
Mutex_trgLock;
Mutex_unlock;
Condition_wait;
Condition_wait_timeout;
Condition_signal;
Condition_signal_all;
thread(tid, NULL, fucntion(), parameter);
// nethod for sum
void * sum(void * arg){
int * p = (int*) arg;
int sum = 0;
Mutex_Lock(G.mcheck_cores);
if(G.running > M){
Condition_wait(G.cond_cores);
G.running++;
}
Mutex_unlock(G.mcheck_cores);
int i;
for(i=0; i<G.C; ++i){
sum += P[i];
}
Mutex_Lock(G.sumL);
G.sum += sum;
Mutex_unlock(G.sumL);
Mutex_Lock(G.mcheck_cores);
G.running--;
G.completed++;
Mutex_unlock(G.mcheck_cores);
Condition_signal(G.mcheck_cores);
return null;
}