-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj2.c
328 lines (286 loc) · 10.3 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
/*
* Řešení IOS-proj2
* Autor: Tomáš Matuš, FIT VUT Brno
* Login: xmatus37
* Datum: 01.05.2021
* Přeloženo: gcc 10.2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <time.h>
#include <stdarg.h>
#include <sys/mman.h>
#include <sys/wait.h>
#define sharedMemDestruct munmap(sems, sizeof(semaphores_t)); \
munmap(actionCnt, sizeof(int)); \
munmap(elves, sizeof(int)); \
munmap(reindeer, sizeof(int)); \
munmap(closed, sizeof(int)); \
munmap(sems, sizeof(semaphores_t));
// struct for input parameters
typedef struct {
int numElves;
int numReind;
int timeElf;
int timeReind;
}params_t;
// struct for semaphores
typedef struct {
sem_t *santaSem;
sem_t *actionSem;
sem_t *elfTex;
sem_t *reindTex;
sem_t *startXmas;
sem_t *waitHelp;
sem_t *santaHelping;
}semaphores_t;
int parseParams(char *argv[], params_t *params) {
char *ptr = NULL;
int tmp;
for (int i = 1; i < 5; i++) {
tmp = (int)strtol(argv[i], &ptr, 10);
if (tmp < 0 || ptr[0] != '\0') {
// couldn't convert parameter
return -1;
}
switch (i) {
case 1:
params->numElves = tmp;
break;
case 2:
params->numReind = tmp;
break;
case 3:
params->timeElf = tmp + 1;
break;
case 4:
params->timeReind = tmp + 1;
break;
default:
break;
}
}
return 0;
}
void flushPrint(FILE *fp, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(fp, fmt, args);
va_end(args);
fflush(fp);
}
// Initializes semaphores
int semInit(semaphores_t *sems) {
// TODO check if sem_open fails
sems->santaSem = sem_open("santaSem", O_CREAT, 0644, 0); // keeps santa sleeping
sems->actionSem = sem_open("actionCount", O_CREAT, 0644, 1); // used to access shared memory
sems->elfTex = sem_open("elfTex", O_CREAT, 0644, 1); // block elves, only one at a time can enter help tree
sems->reindTex = sem_open("reindTex", O_CREAT, 0644, 0); // block reindeer until last one comes back
sems->startXmas = sem_open("startXmas", O_CREAT, 0644, 0); // tell santa when last reindeer is hitched
sems->waitHelp = sem_open("waitHelp", O_CREAT, 0644, 0); // elves are waiting for help
sems->santaHelping = sem_open("santaHelping", O_CREAT, 0644, 0); // elves are waiting for help
if (sems->santaSem == SEM_FAILED || sems->actionSem == SEM_FAILED || sems->elfTex == SEM_FAILED
|| sems->reindTex == SEM_FAILED || sems->startXmas == SEM_FAILED || sems->waitHelp == SEM_FAILED
|| sems->santaHelping == SEM_FAILED) {
return 1;
}
return 0;
}
void semDestruct(semaphores_t *sems) {
sem_close(sems->santaSem);
sem_close(sems->actionSem);
sem_close(sems->elfTex);
sem_close(sems->reindTex);
sem_close(sems->startXmas);
sem_close(sems->waitHelp);
sem_close(sems->santaHelping);
sem_unlink("santaSem");
sem_unlink("actionCount");
sem_unlink("elfTex");
sem_unlink("reindTex");
sem_unlink("startXmas");
sem_unlink("waitHelp");
sem_unlink("santaHelping");
}
int main(int argc, char *argv[]) {
if (argc != 5) {
fprintf(stderr, "Invalid input parameters\n");
return 1;
}
params_t params;
if (parseParams(argv, ¶ms) != 0) {
fprintf(stderr, "Input parameter is not a number\n");
return 1;
}
// make shared memory for sem struct and initialize
semaphores_t *sems = mmap(NULL, sizeof(semaphores_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
if (semInit(sems) == 1) {
fprintf(stderr, "Failed to initialize semaphore\n");
semDestruct(sems);
}
// shared counter to enumerate actions in output
int *actionCnt = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
// shared counter of elves waiting for santa's help
int *elves = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
// shared counter of reindeer who came back from holidays
int *reindeer = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
// flag to signal when santa closes workshop
int *closed = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
if (actionCnt == MAP_FAILED || elves == MAP_FAILED || reindeer == MAP_FAILED || closed == MAP_FAILED) {
fprintf(stderr, "Failed to allocate shared memory\n");
semDestruct(sems);
sharedMemDestruct
return 1;
}
*actionCnt = 1;
*elves = 0;
*reindeer = 0;
*closed = 0;
// output file
FILE *fp = fopen("./proj2.out", "w");
srand(time(NULL) * getpid());
// Process Santa
pid_t santa = fork();
if (santa == 0) {
while (1) {
// santa is sleeping
sem_wait(sems->actionSem);
flushPrint(fp, "%d: Santa: going to sleep\n", (*actionCnt)++);
sem_post(sems->actionSem);
sem_wait(sems->santaSem);
sem_wait(sems->actionSem);
if ((*reindeer) >= params.numReind) {
// starting christmas
flushPrint(fp, "%d: Santa: closing workshop\n", (*actionCnt)++);
*closed = 1;
sem_post(sems->actionSem);
for (int i = 0; i < params.numReind; i++) {
sem_post(sems->reindTex);
}
// wait for all reindeer to get hitched
sem_wait(sems->startXmas);
sem_wait(sems->actionSem);
flushPrint(fp, "%d: Santa: Christmas started\n", (*actionCnt)++);
sem_post(sems->actionSem);
for (int i = 0; i < params.numElves; i++) {
sem_post(sems->elfTex);
sem_post(sems->waitHelp);
}
exit(0);
} else if ((*elves) >= 3) {
// help elves
flushPrint(fp, "%d: Santa: helping elves\n", (*actionCnt)++);
for (int i = 0; i < 3; i++) {
sem_post(sems->waitHelp);
}
sem_post(sems->actionSem);
sem_wait(sems->santaHelping);
}
} // Santa while loop
} else if (santa < 0) {
fprintf(stderr, "Failed to create Santa process\n");
semDestruct(sems);
sharedMemDestruct
fclose(fp);
return 1;
}
// Process Elves
for (int i = 0; i < params.numElves; i++) {
pid_t elf = fork();
if (elf == 0) {
sem_wait(sems->actionSem);
flushPrint(fp, "%d: Elf %d: started\n", (*actionCnt)++, i+1);
sem_post(sems->actionSem);
while (1) {
// elf is working
usleep(1000 * (random() % params.timeElf));
// need help
sem_wait(sems->elfTex);
sem_wait(sems->actionSem);
flushPrint(fp, "%d: Elf %d: need help\n", (*actionCnt)++, i+1);
*elves += 1;
if ((*elves) == 3) {
// wake up santa
sem_post(sems->santaSem);
} else {
sem_post(sems->elfTex);
}
sem_post(sems->actionSem);
// wait for santa's help
sem_wait(sems->waitHelp);
sem_wait(sems->actionSem);
// received santa's help
if ((*closed) == 1) {
flushPrint(fp, "%d: Elf %d: taking holidays\n", (*actionCnt)++, i+1);
sem_post(sems->actionSem);
exit(0);
}
flushPrint(fp, "%d: Elf %d: get help\n", (*actionCnt)++, i+1);
(*elves)--;
if ((*elves) == 0) {
sem_post(sems->santaHelping);
sem_post(sems->elfTex);
}
sem_post(sems->actionSem);
} // Elf while loop
} else if (elf < 0) {
fprintf(stderr, "Failed to create Elf process\n");
kill(santa, SIGTERM);
semDestruct(sems);
sharedMemDestruct
fclose(fp);
return 1;
}
}
// Process Reindeer
for (int i = 0; i < params.numReind; i++) {
pid_t reind = fork();
if (reind == 0) {
sem_wait(sems->actionSem);
flushPrint(fp, "%d: RD %d: rstarted\n", (*actionCnt)++, i+1);
sem_post(sems->actionSem);
// Reindeer on holidays
//usleep(1000 * ((params.timeReind / 2) + (random() % (params.timeReind / 2))));
usleep(1000 * (random() % params.timeReind));
// Reindeer came back
sem_wait(sems->actionSem);
flushPrint(fp, "%d: RD %d: return home\n", (*actionCnt)++, i+1);
*reindeer += 1;
if ((*reindeer) == params.numReind) {
// all are back, wake up santa to start christmas
sem_post(sems->santaSem);
}
sem_post(sems->actionSem);
// waiting for last reindeer
sem_wait(sems->reindTex);
// all reindeer are here, get hitched
sem_wait(sems->actionSem);
flushPrint(fp, "%d: RD %d: get hitched\n", (*actionCnt)++, i+1);
(*reindeer)--;
if ((*reindeer) == 0) {
// last reindeer hitched
sem_post(sems->startXmas);
}
sem_post(sems->actionSem);
exit(0);
} else if (reind < 0) {
fprintf(stderr, "Failed to create Reindeer process\n");
kill(santa, SIGTERM);
kill(reind, SIGTERM);
semDestruct(sems);
sharedMemDestruct
fclose(fp);
return 1;
}
}
// wait for children to die
while(wait(NULL) > 0);
// destroy semaphores and free shared memory
semDestruct(sems);
sharedMemDestruct
fclose(fp);
return 0;
}