-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc3.0.2.0.c
359 lines (284 loc) · 7.43 KB
/
sc3.0.2.0.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
int map_minerals = 5000;
int minerals = 0;
int collected_minerals = 0;
int all_minerals = 0;
int n = 0;
int m = 0;
int k = 0;
int command_centers_minerals[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
pthread_mutex_t mutex_workers;
pthread_mutex_t mutex_all_minerals;
pthread_mutex_t mutex_map_minerals;
pthread_mutex_t mutex_minerals;
pthread_mutex_t mutex_solders;
pthread_mutex_t mutex_command_centers;
pthread_mutex_t mutex_command_centers_minerals[10];
void game_over () {
m = 22;
printf("An ERROR ocured which forced Game Over!\n");
return;
}
void* worker () {
if (pthread_mutex_lock(&mutex_workers) != 0) {
perror("Failed to lock mutex: workers!\n");
game_over();
return;
}
int number = n++ + 1;
if (pthread_mutex_unlock(&mutex_workers) != 0) {
perror("Failed to unlock mutex: workers!\n");
game_over();
return;
}
int worker_minerals = 0;
int i = 0;
int flag = 0;
int main_status;
int others_status;
if (n > 5) {
printf("SCV good to go, sir.\n");
}
while ((m < 20) && (map_minerals > 0)) {
printf("SVC %d is mining\n", number);
if (pthread_mutex_lock(&mutex_map_minerals) != 0) {
perror("Failed to lock mutex: map_minerals!\n");
game_over();
return;
}
if (map_minerals >= 8) {
map_minerals -= 8;
worker_minerals += 8;
} else {
worker_minerals += map_minerals;
map_minerals = 0;
}
if (pthread_mutex_unlock(&mutex_map_minerals) != 0) {
perror("Failed to unlock mutex: map_minerals!\n");
game_over();
return;
}
printf("SVC %d is transporting minerals\n", number);
printf ("minerals: %d, k: %d, m: %d\n", all_minerals, k, m);
while (worker_minerals > 0) {
main_status = pthread_mutex_trylock(&mutex_minerals);
if (main_status == 0) {
sleep(1);
minerals += worker_minerals;
pthread_mutex_unlock(&mutex_minerals);
flag = 1;
printf("SVC %d delivered minerals to Command Center 1\n", number);
} else {
if (main_status == 16) {
while(i < k) {
others_status = pthread_mutex_trylock(&mutex_command_centers_minerals[i]);
if (others_status == 0) {
sleep(1);
command_centers_minerals[i] += worker_minerals;
if (pthread_mutex_unlock(&mutex_command_centers_minerals[i]) != 0) {
perror("Failed to unlock mutex: command_centers_minerals M!\n");
game_over();
return;
}
flag = 1;
printf("SVC %d delivered minerals to Command Center %d\n", number, (i + 2));
break;
} else {
if (others_status == 16) i++;
else {
perror("Failed to tylock mutex: all_minerals!\n");
game_over();
return;
}
}
}
} else {
perror("Failed to tylock mutex: all_minerals!\n");
game_over();
return;
}
}
if (flag == 1) {
if (pthread_mutex_lock(&mutex_all_minerals) != 0) {
perror("Failed to lock mutex: all_minerals!\n");
game_over();
return;
}
collected_minerals += worker_minerals;
all_minerals += worker_minerals;
if (pthread_mutex_unlock(&mutex_all_minerals) != 0) {
perror("Failed to unlock mutex: all_minerals!\n");
game_over();
return;
}
worker_minerals = 0;
flag = 0;
}
}
}
return;
}
void* command_center () {
if (pthread_mutex_init(&mutex_command_centers_minerals[k], NULL) != 0) {
perror("Fail to initialize mutex: command_centers_minerals M\n");
game_over();
return;
} else {
if (pthread_mutex_lock(&mutex_command_centers) != 0) {
perror("Failed to lock mutex: commadn_centers!\n");
game_over();
return;
}
k++;
if (pthread_mutex_unlock(&mutex_command_centers) != 0) {
perror("Failed to unlock mutex: commadn_centers!\n");
game_over();
return;
}
printf("Command center %d created.\n", (k + 1));
}
return;
}
void* solder () {
//sleep(1);
if (pthread_mutex_lock(&mutex_solders) != 0) {
perror("Failed to lock mutex: solders!\n");
game_over();
return;
}
m++;
if (pthread_mutex_unlock(&mutex_solders) != 0) {
perror("Failed to unlock mutex: solders!\n");
game_over();
return;
}
printf("You wanna piece of me, boy?\n");
return;
}
int main () {
char command;
int i;
pthread_t workers[80];
pthread_t solders[20];
pthread_t centers[10];
if (pthread_mutex_init(&mutex_workers, NULL) != 0) {
perror("Fail to initialize mutex: workers!\n");
game_over();
return 0;
}
if (pthread_mutex_init(&mutex_command_centers, NULL) != 0) {
perror("Fail to initialize mutex: command_center!\n");
game_over();
return 0;
}
if (pthread_mutex_init(&mutex_solders, NULL) != 0) {
perror("Fail to initialize mutex: solders!\n");
game_over();
return 0;
}
if (pthread_mutex_init(&mutex_map_minerals, NULL) != 0) {
perror("Fail to initialize mutex: map_minerals!\n");
game_over();
return 0;
}
if (pthread_mutex_init(&mutex_all_minerals, NULL) != 0) {
perror("Fail to initialize mutex: all_minerals!\n");
game_over();
return 0;
}
for(i = 0; i < 5;i++) {
if (pthread_create(&workers[i], NULL, worker, NULL) != 0) {
printf("Fail to crate new worker!\n");
game_over();
return 0;
}
}
while(m < 20) {
command = getchar();
if ((command == 'm') || (command == 's') || (command == 'c')) {
if (pthread_mutex_lock(&mutex_all_minerals) != 0) {
perror("Fail to lock mutex: minerals!\n");
game_over();
return 0;
}
if (command == 'm') {
if ((all_minerals >= 50) && (m < 20)) {
all_minerals -= 50;
if (pthread_create(&solders[m], NULL, solder, NULL) != 0) {
printf("Fail to crate new sodler!\n");
game_over();
return 0;
}
} else {
printf("No minerals!\n");
}
}
if (command == 's') {
if ((all_minerals >= 50) && (n < 80)) {
all_minerals -= 50;
if (pthread_create(&workers[n], NULL, worker, NULL) != 0) {
printf("Fail to crate new worker!\n");
game_over();
return 0;
}
} else {
printf("No minerals!\n");
}
}
if (command == 'c') {
if ((all_minerals >= 400) && (k < 10)) {
all_minerals -= 400;
if (pthread_create(¢ers[k], NULL, command_center, NULL) != 0) {
printf("Fail to crate new command_center!\n");
game_over();
return 0;
}
} else {
printf("No minerals!\n");
}
}
if (pthread_mutex_unlock(&mutex_all_minerals) != 0 ) {
perror("Fail to unlock mutex: minerals!\n");
game_over();
return 0;
}
}
if ((all_minerals == 0) && (map_minerals == 0)) {
command = '~';
break;
}
}
printf("Press Enter if nothing happen in next 10 secs!\n");
for(i = 0; i < n;i++) {
if (pthread_join(workers[i], NULL) != 0) {
perror("Fail to join main and a worker!\n");
game_over();
return 0;
}
}
for(i = 0; i < k;i++) {
if (pthread_join(centers[i], NULL) != 0) {
perror("Fail to join main and a center!\n");
game_over();
return 0;
}
}
for(i = 0; i < m;i++) {
if (pthread_join(solders[i], NULL) != 0 ) {
perror("Fail to join main and a sodler!\n");
game_over();
return 0;
}
}
if (command == '~')
printf("You Failed to create 20 solders Game Over!\n");
else {
printf("Game Won !, You successfuly created 20 solders\n");
printf("Game started with 5000 minerals on the map, %d minerals left on map and you successfuly collected %d minerals", map_minerals, collected_minerals);
}
return 0;
}