-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproj2.c
474 lines (379 loc) · 9.91 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//author: Timotej Halenar
//xlogin: xhalen00
//date: 28.4.2022
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>
#include <semaphore.h>
#define intSIZE sizeof(int)
//semaphore struct
//***********************
typedef struct
{
sem_t mutex;
sem_t queue_h;
sem_t queue_o;
}semaphores;
//***********************
#define smphSIZE sizeof(semaphores)
//barrier struct
//***********************
typedef struct
{
sem_t mutex;
sem_t turnstile1;
sem_t turnstile2;
int n;
int count;
}barrier_t;
//***********************
#define brrSIZE sizeof(barrier_t)
//atom counters
//***********************
typedef struct
{
int o;
int h;
}queue_t;
//***********************
#define qSIZE sizeof(queue_t)
//***********************
//proj2.out
FILE *file;
//process for oxygen
//***********************
void oxygen_proc(semaphores *semaphore, int *cnt, int *o_cnt, int time, int moltime, queue_t *queue, int *mol_cnt, int *mol_three, int *max_mols, barrier_t *barrier)
{
srand(getpid());
//sleep time for atoms
int sleeptime = rand()%time*1000;
srand(getpid());
//sleeptime for molecules
int molsleeptime = rand()%moltime*1000;
//wait for mutex token
sem_wait(&(semaphore->mutex));
//identificator
int id = ++(*o_cnt);
*cnt = *cnt + 1;
fprintf(file, "%d: O %d: started\n", *cnt, id);
sem_post(&(semaphore->mutex));
//sleep, then go to queue
usleep(sleeptime);
sem_wait(&(semaphore->mutex));
*cnt = *cnt + 1;
fprintf(file, "%d: O %d: going to queue\n", *cnt, id);
//increment number of oxygens in queue
queue->o = queue->o + 1;
//only make more molecules if they can be created
if (*mol_cnt <= *max_mols)
{
//if there are two hydrogens waiting, let them through
if (queue->h >= 2)
{
sem_post(&(semaphore->queue_h));
sem_post(&(semaphore->queue_h));
queue->h = queue->h - 2;
sem_post(&(semaphore->queue_o));
queue->o = queue->o - 1;
}
//if not, move on
else
{
sem_post(&(semaphore->mutex));
}
}
//waiting to be let through
sem_wait(&(semaphore->queue_o));
*cnt = *cnt + 1;
//if all molecules have been created already, signal semaphores and exit
if (*mol_cnt > *max_mols)
{
fprintf(file, "%d: O %d: not enough H\n", *cnt, id);
//printf("gOt here\n");
sem_post(&(semaphore->queue_o));
sem_post(&(semaphore->mutex));
exit(0);
}
fprintf(file, "%d: O %d: creating molecule %d\n", *cnt, id, *mol_cnt);
//i don't know if this usleep does anything
usleep(molsleeptime);
//***barrier.wait start***
sem_wait(&(barrier->mutex));
barrier->count = barrier->count + 1;
if (barrier->count == barrier->n)
{
sem_wait(&(barrier->turnstile2));
sem_post(&(barrier->turnstile1));
}
sem_post(&(barrier->mutex));
sem_wait(&(barrier->turnstile1));
sem_post(&(barrier->turnstile1));
//***critical section start***
*cnt = *cnt + 1;
fprintf(file, "%d: O %d: molecule %d created\n", *cnt, id, *mol_cnt);
//my elaborate molecule incrementing system, there's probably an easier way but this is what I've come up with
*mol_three = *mol_three + 1;
if (*mol_three == 3)
{
*mol_cnt = *mol_cnt + 1;
*mol_three = 0;
}
//if this was the last molecule, open semaphores to let all other atoms through
//these will get caught by a condition and killed
if (*mol_cnt > *max_mols)
{
for (int i=0; i<*cnt; i++)
{
sem_post(&(semaphore->queue_o));
}
for (int i=0; i<*cnt; i++)
{
sem_post(&(semaphore->queue_h));
}
}
//***critical section end***
sem_wait(&(barrier->mutex));
barrier->count = barrier->count - 1;
if (barrier->count == 0)
{
sem_wait(&(barrier->turnstile1));
sem_post(&(barrier->turnstile2));
}
sem_post(&(barrier->mutex));
sem_wait(&(barrier->turnstile2));
sem_post(&(barrier->turnstile2));
///***barrier.wait end***
sem_post(&(semaphore->mutex));
exit(0);
}
//***********************
//process for hydrogen
//***********************
void hydrogen_proc(semaphores *semaphore, int *cnt, int *h_cnt, int time, queue_t *queue, int *mol_cnt, int *mol_three, int *max_mols, barrier_t *barrier)
{
//same as the oxygen function
srand(getpid());
int sleeptime = rand()%time*1000;
sem_wait(&(semaphore->mutex));
int id = ++(*h_cnt);
*cnt = *cnt + 1;
fprintf(file, "%d: H %d: started\n", *cnt, id);
sem_post(&(semaphore->mutex));
usleep(sleeptime);
sem_wait(&(semaphore->mutex));
*cnt = *cnt + 1;
fprintf(file, "%d: H %d: going to queue\n", *cnt, id);
queue->h = queue->h + 1;
if (*mol_cnt <= *max_mols)
{
//similar to oxygen, but checking the oxygen queue as well,
//because we've just added another hydrogen, not oxygen
if (queue->h >= 2 && queue->o >= 1)
{
sem_post(&(semaphore->queue_h));
sem_post(&(semaphore->queue_h));
queue->h = queue->h - 2;
sem_post(&(semaphore->queue_o));
queue->o = queue->o - 1;
}
else
{
sem_post(&(semaphore->mutex));
}
}
sem_wait(&(semaphore->queue_h));
*cnt = *cnt + 1;
if (*mol_cnt > *max_mols)
{
fprintf(file, "%d: H %d: not enough O or H\n", *cnt, id);
sem_post(&(semaphore->mutex));
sem_post(&(semaphore->queue_h));
exit(0);
}
fprintf(file, "%d: H %d: creating molecule %d\n", *cnt, id, *mol_cnt);
//***barrier.wait start***
sem_wait(&(barrier->mutex));
barrier->count = barrier->count + 1;
if (barrier->count == barrier->n)
{
sem_wait(&(barrier->turnstile2));
sem_post(&(barrier->turnstile1));
}
sem_post(&(barrier->mutex));
sem_wait(&(barrier->turnstile1));
sem_post(&(barrier->turnstile1));
//***critical section start***
*cnt = *cnt + 1;
fprintf(file, "%d: H %d: molecule %d created\n", *cnt, id, *mol_cnt);
*mol_three = *mol_three + 1;
if (*mol_three == 3)
{
*mol_cnt = *mol_cnt + 1;
*mol_three = 0;
}
if (*mol_cnt > *max_mols)
{
for (int i=0; i<*cnt; i++)
{
sem_post(&(semaphore->queue_o));
}
for (int i=0; i<*cnt; i++)
{
sem_post(&(semaphore->queue_h));
}
}
//***critical section end***
sem_wait(&(barrier->mutex));
barrier->count = barrier->count - 1;
if (barrier->count == 0)
{
sem_wait(&(barrier->turnstile1));
sem_post(&(barrier->turnstile2));
}
sem_post(&(barrier->mutex));
sem_wait(&(barrier->turnstile2));
sem_post(&(barrier->turnstile2));
///***barrier.wait end***
exit(0);
}
//***********************
//calculate how many molecules will be created
//***********************
int mol_count(int oxygen, int hydrogen)
{
return ((2*oxygen) < hydrogen) ? oxygen : (hydrogen/2);
}
//main
//***********************
int main(int argc, char *argv[])
{
//argument checking
if (argc != 5)
{
fprintf(stderr, "Invalid arguments.\n");
return 1;
}
if (atoi(argv[3]) > 1000)
{
fprintf(stderr, "Time argument out of range.\n");
return 1;
}
if (atoi(argv[4]) > 1000)
{
fprintf(stderr, "Time argument out of range.\n");
return 1;
}
//arguments parsing
int oxygen;
oxygen = atoi(argv[1]);
int hydrogen;
hydrogen = atoi(argv[2]);
int atom_time;
atom_time = atoi(argv[3]);
int molecule_time;
molecule_time = atoi(argv[4]);
//opening file
if ((file = fopen("proj2.out", "w")) == NULL)
{
fprintf(stderr, "Unable to open file.\n");
return 1;
}
setbuf(file, NULL);
setbuf(stderr, NULL);
//shared variables
//action counter
int *cnt;
cnt = mmap(NULL, intSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*cnt = 0;
//oxygen counter
int *o_cnt;
o_cnt = mmap(NULL, intSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*o_cnt = 0;
//hydrogen counter
int *h_cnt;
h_cnt = mmap(NULL, intSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*h_cnt = 0;
//molecule counter
int *mol_cnt;
mol_cnt = mmap(NULL, intSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*mol_cnt = 1;
//additional variable to accurately index molecules
int *mol_three;
mol_three = mmap(NULL, intSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*mol_three = 0;
//number of molecules to be created
int *max_mols;
max_mols = mmap(NULL, intSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
*max_mols = mol_count(oxygen, hydrogen);
//number of O and H atoms in queue
queue_t *queue;
queue = mmap(NULL, qSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
queue->o = 0;
queue->h = 0;
//semaphores
semaphores *semaphore;
semaphore = mmap(NULL, smphSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
sem_init(&(semaphore->mutex), 1, 1);
sem_init(&(semaphore->queue_o), 1, 0);
sem_init(&(semaphore->queue_h), 1, 0);
//barrier
barrier_t *barrier;
barrier = mmap(NULL, brrSIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
sem_init(&(barrier->mutex), 1, 1);
sem_init(&(barrier->turnstile1), 1, 0);
sem_init(&(barrier->turnstile2), 1, 1);
barrier->n = 3;
barrier->count = 0;
pid_t pid = getpid();
//creating oxygen processes
for (int i=0; i<oxygen; i++)
{
if (pid != 0)
{
pid = fork();
}
if (pid == 0)
{
oxygen_proc(semaphore, cnt, o_cnt, atom_time, molecule_time, queue, mol_cnt, mol_three, max_mols, barrier);
}
}
pid = getpid();
//creating hydrogen processes
for (int i=0; i<hydrogen; i++)
{
if (pid != 0)
{
pid = fork();
}
if (pid == 0)
{
hydrogen_proc(semaphore, cnt, h_cnt, atom_time, queue, mol_cnt, mol_three, max_mols, barrier);
}
}
//waiting for children to die
for (int i=0; i<hydrogen+oxygen; i++)
{
wait(NULL);
}
//cleanup
sem_destroy(&(semaphore->mutex));
sem_destroy(&(semaphore->queue_o));
sem_destroy(&(semaphore->queue_h));
sem_destroy(&(barrier->mutex));
sem_destroy(&(barrier->turnstile1));
sem_destroy(&(barrier->turnstile2));
munmap(cnt, intSIZE);
munmap(o_cnt, intSIZE);
munmap(h_cnt, intSIZE);
munmap(mol_cnt, intSIZE);
munmap(mol_three, intSIZE);
munmap(max_mols, intSIZE);
munmap(queue, qSIZE);
munmap(semaphore, smphSIZE);
munmap(barrier, brrSIZE);
fclose(file);
return 0;
}
//***********************