-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj2.c
414 lines (342 loc) · 8.38 KB
/
proj2.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
Autor: Martin Hlinský
Login: xhlins01
Datum: 06.05.2020
Projekt: IOS #2 - Faneuil Hall Problem
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define MMAP(ptr) {(ptr) = mmap(NULL, sizeof(*(ptr)), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);}
#define UNMAP(ptr) {munmap((ptr), sizeof((ptr)));}
FILE *file;
pid_t court, generator, IID, wpid;
/*semaphores*/
/*
noJudge = can immigrants enter/leave?
mutex = primarily for organized check
confirmed = only a certain amount of immigrants confirmed
allRegistered = entered == check?
msg = organized output, only one at a time
confirmedDone = no interrupting judge
*/
sem_t *noJudge = NULL;
sem_t *mutex = NULL;
sem_t *confirmed = NULL;
sem_t *allRegistered = NULL;
sem_t *msg = NULL;
sem_t *confirmedDone = NULL;
/*shared vars*/
int *action = NULL;
int *entered = NULL;
int *registered = NULL;
int *immCount = NULL;
int *judge = NULL;
int *done = NULL;
int *status = NULL;
/*arg check*/
int argCheck(int PI, int IG, int JG, int IT, int JT){
if (PI >= 1) {
}
else {
fprintf(stderr,"PI must be greater or equal to 1\n"); /*error*/
return 1;
}
if ((IG >= 0) && (IG <= 2000)) {
}
else {
fprintf(stderr,"IG must be between 0 and 2000\n"); /*error*/
return 1;
}
if ((JG >= 0) && (JG <= 2000)) {
}
else {
fprintf(stderr,"JG must be between 0 and 2000\n"); /*error*/
return 1;
}
if ((IT >= 0) && (IT <= 2000)) {
}
else {
fprintf(stderr,"IT must be between 0 and 2000\n"); /*error*/
return 1;
}
if ((JT >= 0) && (JT <= 2000)) {
}
else {
fprintf(stderr,"JT must be between 0 and 2000\n"); /*error*/
return 1;
}
return 0;
}
/*init function*/
int init() {
/*open file*/
file = fopen("proj2.out", "w");
/*mmap shared vars*/
MMAP(action);
MMAP(entered);
MMAP(registered);
MMAP(immCount);
MMAP(judge);
MMAP(done);
MMAP(status);
/*open semaphores*/
if ((noJudge = sem_open("/xhlins01.ios.proj2.noJudge", O_CREAT | O_EXCL, 0666, 1)) == SEM_FAILED) {
return 1;
}
if ((mutex = sem_open("/xhlins01.ios.proj2.mutex", O_CREAT | O_EXCL, 0666, 1)) == SEM_FAILED) {
return 1;
}
if ((msg = sem_open("/xhlins01.ios.proj2.msg", O_CREAT | O_EXCL, 0666, 1)) == SEM_FAILED) {
return 1;
}
if ((confirmed = sem_open("/xhlins01.ios.proj2.confirmed", O_CREAT | O_EXCL, 0666, 0)) == SEM_FAILED) {
return 1;
}
if ((allRegistered = sem_open("/xhlins01.ios.proj2.allRegistered", O_CREAT | O_EXCL, 0666, 0)) == SEM_FAILED) {
return 1;
}
if ((confirmedDone = sem_open("/xhlins01.ios.proj2.confirmedDone", O_CREAT | O_EXCL, 0666, 1)) == SEM_FAILED) {
return 1;
}
return 0;
}
/*immigrant process*/
void process_immigrant(int i, int IT) {
sem_wait(msg);
(*action)++;
fprintf(file, "%d : IMM %d : starts\n", *action, i);
fflush(file);
sem_post(msg);
sem_wait(noJudge);
sem_wait(msg);
(*action)++;
(*entered)++;
(*immCount)++;
fprintf(file, "%d : IMM %d : enters : %d : %d : %d\n", *action, i, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
sem_post(noJudge);
sem_wait(mutex);
sem_wait(msg);
(*action)++;
(*registered)++;
fprintf(file, "%d : IMM %d : checks : %d : %d : %d\n", *action, i, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
if ((*judge == 1) && (*entered == *registered)) {
sem_post(allRegistered);
sem_post(mutex);
}
else {
sem_post(mutex);
}
sem_wait(confirmed);
sem_wait(confirmedDone);
sem_post(confirmedDone);
sem_wait(msg);
(*action)++;
fprintf(file, "%d : IMM %d : wants certificate : %d : %d : %d\n", *action, i, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
if (IT > 0) {
usleep((rand() % (IT + 1)) * 1000);
}
sem_wait(confirmedDone);
sem_post(confirmedDone);
sem_wait(msg);
(*action)++;
fprintf(file, "%d : IMM %d : got certificate : %d : %d : %d\n", *action, i, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
sem_wait(noJudge);
sem_wait(msg);
(*action)++;
(*immCount)--;
fprintf(file, "%d : IMM %d : leaves : %d : %d : %d\n", *action, i, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
sem_post(noJudge);
exit(0);
}
/*immigrant generator*/
void create_immigrants(int PI, int IG, int IT) {
*status = 0;
for (int i = 1; i <= PI; i++) {
if (IG > 0) {
usleep((rand() % (IG + 1)) * 1000);
}
pid_t IID = fork();
if (IID == 0) {
process_immigrant(i, IT);
}
else if (IID < 0) {
exit(1);
}
}
while ((wpid = wait(status)) > 0);
exit(0);
}
/*judge process*/
void process_judge(int JG, int PI, int JT) {
*done = 0;
while (PI > *done) {
if (JG > 0) {
usleep((rand() % (JG + 1)) * 1000);
}
sem_wait(noJudge);
sem_wait(mutex);
sem_wait(msg);
(*action)++;
fprintf(file, "%d : JUDGE : wants to enter\n", *action);
fflush(file);
sem_post(msg);
sem_wait(msg);
(*action)++;
fprintf(file, "%d : JUDGE : enters : %d : %d : %d\n", *action, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
*judge = 1;
if (*entered > *registered) {
sem_wait(msg);
(*action)++;
fprintf(file, "%d : JUDGE : waits for imm : %d : %d : %d\n", *action, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
sem_post(mutex);
sem_wait(allRegistered);
sem_wait(mutex);
}
sem_wait(confirmedDone);
sem_wait(msg);
(*action)++;
fprintf(file, "%d : JUDGE : starts confirmation : %d : %d : %d\n", *action, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
for (int i = 0; i < *registered; i++) {
if (JT > 0) {
usleep((rand() % (JG + 1)) * 1000);
}
sem_post(confirmed);
(*done)++;
}
sem_wait(msg);
*entered = 0;
*registered = 0;
(*action)++;
fprintf(file, "%d : JUDGE : ends confirmation : %d : %d : %d\n", *action, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
sem_post(confirmedDone);
sem_wait(msg);
(*action)++;
fprintf(file, "%d : JUDGE : leaves : %d : %d : %d\n", *action, *entered, *registered, *immCount);
fflush(file);
sem_post(msg);
*judge = 0;
if (PI == *done) {
sem_wait(msg);
(*action)++;
fprintf(file, "%d : JUDGE : finishes\n", *action);
fflush(file);
sem_post(msg);
sem_post(noJudge);
sem_post(mutex);
exit(0);
}
sem_post(noJudge);
sem_post(mutex);
}
exit(0);
}
void cleanup() {
/*unmap shared vars*/
UNMAP(action);
UNMAP(entered);
UNMAP(registered);
UNMAP(immCount);
UNMAP(judge);
UNMAP(done);
UNMAP(status);
/*close semaphores*/
sem_close(noJudge);
sem_close(mutex);
sem_close(confirmed);
sem_close(allRegistered);
sem_close(msg);
sem_close(confirmedDone);
/*unlink semaphores*/
sem_unlink("/xhlins01.ios.proj2.noJudge");
sem_unlink("/xhlins01.ios.proj2.mutex");
sem_unlink("/xhlins01.ios.proj2.confirmed");
sem_unlink("/xhlins01.ios.proj2.allRegistered");
sem_unlink("/xhlins01.ios.proj2.msg");
sem_unlink("/xhlins01.ios.proj2.confirmedDone");
/*close file*/
if (file != NULL) {
fclose(file);
}
}
int main(int argc, char* argv[]) {
/*randomized seed*/
srand(time(0));
//setbuf(file, NULL);
/*6 arguments needed*/
if (argc != 6) {
fprintf(stderr, "wrong amount of arguments\n");
return 1;
}
/*Convert chars to int*/
int PI = atoi(argv[1]);
int IG = atoi(argv[2]);
int JG = atoi(argv[3]);
int IT = atoi(argv[4]);
int JT = atoi(argv[5]);
/*argcheck*/
if (argCheck(PI,IG,JG,IT,JT) == 1) {
return 1;
}
/*if init fails, else continue*/
if (init() == 1) {
cleanup();
return 1;
}
setbuf(file, NULL);
/*process that runs judge*/
pid_t court = fork();
if (court < 0) {
/*error*/
return 1;
}
else if (court == 0) {
/*child*/
process_judge(JG, PI, JT);
}
/*process generating immigrants*/
pid_t generator = fork();
if (generator < 0) {
return 1;
}
else if (generator == 0) {
create_immigrants(PI, IG, IT);
}
/*wait for the other processes*/
waitpid(generator, NULL, 0);
waitpid(court, NULL, 0);
/*cleanup everything*/
cleanup();
return 0;
}