-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.c
More file actions
816 lines (736 loc) · 22.1 KB
/
simulation.c
File metadata and controls
816 lines (736 loc) · 22.1 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#include <errno.h>
#include <wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <curses.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h> // library for interprocessing communication
#include <sys/sem.h> // library for semaphores
#include <sys/shm.h> // library for shared memory
#include <sys/time.h>
#include <sys/resource.h>
/**
*Define semaphores to be placed in a single semaphore set
*Numbers indicate index in semaphore set for named semaphore
*/
#define SEM_COWSINGROUP 0
#define SEM_PCOWSINGROUP 1
#define SEM_SHEEPSINGROUP 2
#define SEM_PSHEEPSINGROUP 3
#define SEM_COWSWAITING 7
#define SEM_SHEEPSWAITING 8
#define SEM_PSHEEPSEATEN 9
#define SEM_SHEEPSEATEN 10
#define SEM_PCOWSEATEN 11
#define SEM_COWSEATEN 12
#define SEM_SHEEPSDEAD 15
#define SEM_COWSDEAD 16
#define SEM_PTERMINATE 17
#define SEM_DRAGONEATING 19
#define SEM_DRAGONFIGHTING 20
#define SEM_DRAGONSLEEPING 21
#define SEM_DRAGONSWIMMING 22
#define SEM_PMEALWAITINGFLAG 23
/**
*System constants used to control simulation termination
*/
#define MAX_SHEEPS_EATEN 14
#define MAX_COWS_EATEN 14
#define MAX_COWS_CREATED 80
#define MAX_DEFEATED_TREASURE 12
#define MAX_DEFEATED_THIEF 15
#define MAX_TREASURE_IN_HOARD 800
#define MIN_TREASURE_IN_HOARD 0
/**
*System constants for initialization
*/
#define INITIAL_TREASURE_IN_HOARD 400
/**
*System constants to specify size of groups of cows
*/
#define COWS_IN_GROUP 2
#define SHEEPS_IN_GROUP 2
// CREATING YOUR SEMAPHORES
int semID;
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
} seminfo;
struct timeval startTime;
/**
*Pointers and ids for shared memory segments
*/
int *terminateFlagp = NULL;
int *sheepCounterp = NULL;
int *sheepsEatenCounterp = NULL;
int *cowCounterp = NULL;
int *cowsEatenCounterp = NULL;
int *mealWaitingFlagp = NULL;
int terminateFlag = 0;
int sheepCounter = 0;
int sheepsEatenCounter = 0;
int cowCounter = 0;
int cowsEatenCounter = 0;
int mealWaitingFlag = 0;
/**
*Group IDs for managing/removing processes
*/
int smaugProcessID = -1;
int sheepProcessGID = -1;
int cowProcessGID = -1;
int parentProcessGID = -1;
/**
*Number in group semaphores
* -1 = decrement with Wait, 1 = increment with Signal
*/
struct sembuf WaitCowsInGroup = {SEM_COWSINGROUP, -1, 0};
struct sembuf SignalCowsInGroup = {SEM_COWSINGROUP, 1, 0};
struct sembuf WaitSheepsInGroup = {SEM_SHEEPSINGROUP, -1, 0};
struct sembuf SignalSheepsInGroup = {SEM_SHEEPSINGROUP, 1, 0};
struct sembuf WaitProtectCowsInGroup = {SEM_PCOWSINGROUP, -1, 0};
struct sembuf SignalProtectCowsInGroup = {SEM_PCOWSINGROUP, 1, 0};
struct sembuf WaitProtectSheepsInGroup = {SEM_PSHEEPSINGROUP, -1, 0};
struct sembuf SignalProtectSheepsInGroup = {SEM_PSHEEPSINGROUP, 1, 0};
struct sembuf WaitProtectMealWaitingFlag = {SEM_PMEALWAITINGFLAG, -1, 0};
struct sembuf SignalProtectMealWaitingFlag = {SEM_PMEALWAITINGFLAG, 1, 0};
struct sembuf WaitCowsWaiting = {SEM_COWSWAITING, -1, 0};
struct sembuf SignalCowsWaiting = {SEM_COWSWAITING, 1, 0};
struct sembuf WaitSheepsWaiting = {SEM_SHEEPSWAITING, -1, 0};
struct sembuf SignalSheepsWaiting = {SEM_SHEEPSWAITING, 1, 0};
struct sembuf WaitCowsEaten = {SEM_COWSEATEN, -1, 0};
struct sembuf SignalCowsEaten = {SEM_COWSEATEN, 1, 0};
struct sembuf WaitSheepsEaten = {SEM_SHEEPSEATEN, -1, 0};
struct sembuf SignalSheepsEaten = {SEM_SHEEPSEATEN, 1, 0};
struct sembuf WaitProtectCowsEaten = {SEM_PCOWSEATEN, -1, 0};
struct sembuf SignalProtectCowsEaten = {SEM_PCOWSEATEN, 1, 0};
struct sembuf WaitProtectSheepsEaten = {SEM_PSHEEPSEATEN, -1, 0};
struct sembuf SignalProtectSheepsEaten = {SEM_PSHEEPSEATEN, 1, 0};
struct sembuf WaitCowsDead = {SEM_COWSDEAD, -1, 0};
struct sembuf SignalCowsDead = {SEM_COWSDEAD, 1, 0};
struct sembuf WaitSheepsDead = {SEM_SHEEPSDEAD, -1, 0};
struct sembuf SignalSheepsDead = {SEM_SHEEPSDEAD, 1, 0};
struct sembuf WaitDragonEating = {SEM_DRAGONEATING, -1, 0};
struct sembuf SignalDragonEating = {SEM_DRAGONEATING, 1, 0};
struct sembuf WaitDragonFighting = {SEM_DRAGONFIGHTING, -1, 0};
struct sembuf SignalDragonFighting = {SEM_DRAGONFIGHTING, 1, 0};
struct sembuf WaitDragonSleeping = {SEM_DRAGONSLEEPING, -1, 0};
struct sembuf SignalDragonSleeping = {SEM_DRAGONSLEEPING, 1, 0};
struct sembuf WaitDragonSwimming = {SEM_DRAGONSWIMMING, -1, 0};
struct sembuf SignalDragonSwimming = {SEM_DRAGONSWIMMING, -1, 0};
struct sembuf WaitProtectTerminate = {SEM_PTERMINATE, -1, 0};
struct sembuf SignalProtectTerminate = {SEM_PTERMINATE, 1, 0};
double timeChange(struct timeval starttime);
void initialize();
void smaug();
void cow(int startTimeN);
void sheep(int startTimeN);
void terminateSimulation();
void releaseSemandMem();
void semopChecked(int semaphoreID, struct sembuf *operation,
unsigned something);
void semctlChecked(int semaphoreID, int semNum, int flag, union semun seminfo);
void smaug()
{
int k;
int temp;
int localpid;
double elapsedTime;
/**
* local counters used only for smaug routine
*/
int cowsEatenTotal = 0;
int sheepsEatenTotal = 0;
int jewels = INITIAL_TREASURE_IN_HOARD;
int treasureDefeatedTotal = 0;
int thievesDefeatedTotal = 0;
int cowsEatenCurrent = 0;
int sheepsEatenCurrent = 0;
smaugProcessID = getpid();
printf("SMAUG PID is %d \n", smaugProcessID);
localpid = smaugProcessID;
printf("SMAUG Smaug has gone to sleep\n");
semopChecked(semID, &WaitDragonSleeping, 1);
printf("SMAUG Smaug has woken up \n" );
while (TRUE)
{
cowsEatenCurrent = 0;
sheepsEatenCurrent = 0;
semopChecked(semID, &WaitProtectMealWaitingFlag, 1);
while ( * mealWaitingFlagp >= 1 &&
(cowsEatenCurrent + sheepsEatenCurrent) < 2)
{
*mealWaitingFlagp = *mealWaitingFlagp - 1;
printf("SMAUG signal meal flag %d\n", *mealWaitingFlagp);
semopChecked(semID, &SignalProtectMealWaitingFlag, 1);
printf("SMAUG Smaug is eating a meal\n");
for (k = 0; k < SHEEPS_IN_GROUP; k++)
{
semopChecked(semID, &SignalSheepsWaiting, 1);
printf("SMAUG A sheep is ready to eat\n");
}
for (k = 0; k < COWS_IN_GROUP; k++)
{
semopChecked(semID, &SignalCowsWaiting, 1);
printf("SMAUG A cow is ready to eat\n");
}
semopChecked(semID, &WaitDragonEating, 1);
for (k = 0; k < COWS_IN_GROUP; k++)
{
semopChecked(semID, &SignalCowsDead, 1);
cowsEatenTotal++;
printf("SMAUG Smaug finished eating a cow\n");
}
for (k = 0; k < SHEEPS_IN_GROUP; k++)
{
semopChecked(semID, &SignalSheepsDead, 1);
sheepsEatenTotal++;
printf("SMAUG Smaug finished eating a sheep\n");
}
printf("SMAUG Smaug has finished a meal\n");
if (cowsEatenTotal >= MAX_COWS_EATEN)
{
printf("SMAUG Smaug has eaten the allowed number of cows\n");
*terminateFlagp = 1;
break;
}
if (sheepsEatenTotal >= MAX_SHEEPS_EATEN)
{
printf("SMAUG Smaug has eaten the allowed number of sheeps\n");
*terminateFlagp = 1;
break;
}
semopChecked(semID, &WaitProtectMealWaitingFlag, 1);
if ( *mealWaitingFlagp > 0)
{
printf("SMAUG Smaug eats again\n");
// printf("SMAUG Smaug eats again\n", localpid);
continue;
}
else
{
semopChecked(semID, &SignalProtectMealWaitingFlag, 1);
printf("SMAUG Smaug sleeps again\n");
// printf("SMAUG Smaug sleeps again\n", localpid);
semopChecked(semID, &WaitDragonSleeping, 1);
printf("SMAUG Smaug is awake again\n");
// printf("SMAUG Smaug is awake again\n", localpid);
break;
}
}
}
}
void initialize()
{
semID = semget(IPC_PRIVATE, 25, 0666 | IPC_CREAT);
seminfo.val = 0;
semctlChecked(semID, SEM_COWSINGROUP, SETVAL, seminfo);
semctlChecked(semID, SEM_COWSWAITING, SETVAL, seminfo);
semctlChecked(semID, SEM_COWSEATEN, SETVAL, seminfo);
semctlChecked(semID, SEM_COWSDEAD, SETVAL, seminfo);
semctlChecked(semID, SEM_SHEEPSINGROUP, SETVAL, seminfo);
semctlChecked(semID, SEM_SHEEPSWAITING, SETVAL, seminfo);
semctlChecked(semID, SEM_SHEEPSEATEN, SETVAL, seminfo);
semctlChecked(semID, SEM_COWSDEAD, SETVAL, seminfo);
semctlChecked(semID, SEM_DRAGONFIGHTING, SETVAL, seminfo);
semctlChecked(semID, SEM_DRAGONSLEEPING, SETVAL, seminfo);
semctlChecked(semID, SEM_DRAGONEATING, SETVAL, seminfo);
printf("INIT semaphores initiialized\n");
seminfo.val = 1;
semctlChecked(semID, SEM_PCOWSINGROUP, SETVAL, seminfo);
semctlChecked(semID, SEM_PSHEEPSINGROUP, SETVAL, seminfo);
semctlChecked(semID, SEM_PMEALWAITINGFLAG, SETVAL, seminfo);
semctlChecked(semID, SEM_PCOWSEATEN, SETVAL, seminfo);
semctlChecked(semID, SEM_PSHEEPSEATEN, SETVAL, seminfo);
semctlChecked(semID, SEM_PTERMINATE, SETVAL, seminfo);
printf("INIT mutexes initiialized\n");
if ((terminateFlag = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666)) < 0)
{
printf("INIT shm not created for terminateFlag\n");
exit(1);
}
else
{
printf("INIT shm created for terminateFlag\n");
}
if ((cowCounter = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666)) < 0)
{
printf("INIT shm not created for cowCounter\n");
exit(1);
}
else
{
printf("INIT shm created for cowCounter\n");
}
if ((sheepCounter = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666)) < 0
{
printf("INIT shm not created for sheepCounter\n");
exit(1);
}
else
{
printf("INIT shm created for sheepCounter\n");
}
if ((mealWaitingFlag = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666))
< 0)
{
printf("INIT shm not created for mealWaitingFlag\n");
exit(1);
}
else
{
printf("INIT shm created for mealWaitingFlag\n");
}
if ((cowsEatenCounter = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666))
< 0)
{
printf("INIT shm not created for cowsEatenCounter\n");
exit(1);
}
else
{
printf("INIT shm created for cowsEatenCounter\n");
}
if ((sheepsEatenCounter = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666))
< 0)
{
printf("INIT shm not created for sheepsEatenCounter\n");
exit(1);
}
else
{
printf("INIT shm created for sheepsEatenCounter\n");
}
if ((terminateFlagp = shmat(terminateFlag, NULL, 0)) == (int *) - 1)
{
printf("INIT shm not attached for terminateFlag\n");
exit(1);
}
else
{
printf("INIT shm attached for terminateFlag\n");
}
if ((cowCounterp = shmat(cowCounter, NULL, 0)) == (int *) - 1)
{
printf("INIT shm not attached for cowCounter\n");
exit(1);
}
else
{
printf("INIT shm attached for cowCounter\n");
}
if ((sheepCounterp = shmat(sheepCounter, NULL, 0)) == (int *) - 1)
{
printf("INIT shm not attached for sheepCounter\n");
exit(1);
}
else
{
printf("INIT shm attached for sheepCounter\n");
}
if ((mealWaitingFlagp = shmat(mealWaitingFlag, NULL, 0)) == (int *) - 1)
{
printf("INIT shm not attached for mealWaitingFlag\n");
exit(1);
}
else
{
printf("INIT shm attached for mealWaitingFlag\n");
}
if ((cowsEatenCounterp = shmat(cowsEatenCounter, NULL, 0)) == (int *) - 1)
{
printf("INIT shm not attached for cowsEatenCounter\n");
exit(1);
}
else
{
printf("INIT shm attached for cowsEatenCounter\n");
}
if ((sheepsEatenCounterp = shmat(sheepsEatenCounter, NULL, 0)) ==
(int *) - 1)
{
printf("INIT shm not attached for sheepsEatenCounter\n");
exit(1);
}
else
{
printf("INIT shm attached for sheepsEatenCounter\n");
}
printf("INIT initialize end\n");
}
void sheep(int startTimeN)
{
int localpid = getpid();
printf("SHEEP %8d SHEEP A sheep is born\n", localpid);
if ( startTimeN > 0)
{
if ( usleep( startTimeN) == -1) {
// exit when usleep interrupted by kill signal
if (errno == EINTR)exit(4);
}
}
printf("SHEEP %8d SHEEP sheep grazes for %f ms\n", localpid,
startTimeN / 1000.0);
semopChecked(semID, &WaitProtectSheepsInGroup, 1);
semopChecked(semID, &SignalSheepsInGroup, 1);
*sheepCounterp = *sheepCounterp + 1;
printf("SHEEP %8d SHEEP %d sheeps have been enchanted \n", localpid,
*sheepCounterp);
if (*sheepCounterp >= SHEEPS_IN_GROUP)
{ // if there is enough sheeps to be eaten by Smaug
for (int k = 0; k < SHEEPS_IN_GROUP; k++)
{
semopChecked(semID, &WaitSheepsInGroup, 1);
}
*sheepCounterp = *sheepCounterp - SHEEPS_IN_GROUP;
printf("SHEEP %8d SHEEP The last sheep is waiting\n", localpid);
semopChecked(semID, &SignalProtectSheepsInGroup, 1);
semopChecked(semID, &WaitProtectMealWaitingFlag, 1);
*mealWaitingFlagp = *mealWaitingFlagp + 1;
printf("SHEEP %d SHEEP signal meal flag %d\n", localpid,
*mealWaitingFlagp);
semopChecked(semID, &SignalProtectMealWaitingFlag, 1);
semopChecked(semID, &SignalDragonSleeping, 1);
printf("SHEEP %d SHEEP last sheep wakes the dragon \n", localpid);
}
else
{
semopChecked(semID, &SignalProtectSheepsInGroup, 1);
}
// this sheep is now waiting to be eaten
semopChecked(semID, &WaitSheepsWaiting, 1);
// once Smaug has signal SheepsWaiting and unblocked this sheep
semopChecked(semID, &WaitProtectSheepsEaten, 1);
semopChecked(semID, &SignalSheepsEaten, 1);
*sheepsEatenCounterp = *sheepsEatenCounterp + 1;
if (*sheepsEatenCounterp >= SHEEPS_IN_GROUP)
{
for (int k = 0; k < SHEEPS_IN_GROUP; k++)
{
semopChecked(semID, &WaitSheepsEaten, 1);
}
*sheepsEatenCounterp = *sheepsEatenCounterp - SHEEPS_IN_GROUP;
printf("SHEEP %d SHEEP The last sheep has been eaten\n", localpid);
semopChecked(semID, &SignalProtectSheepsEaten, 1);
semopChecked(semID, &SignalDragonEating, 1);
}
else
{
semopChecked(semID, &SignalProtectSheepsEaten, 1);
printf("SHEEP %8d SHEEP A sheep is waiting to be eaten\n", localpid);
}
semopChecked(semID, &WaitSheepsDead, 1);
printf("SHEEP %d SHEEP Sheep dies", localpid);
}
void cow(int startTimeN)
{
int localpid = getpid();
printf("COW %8d COW A cow is born\n", localpid);
if ( startTimeN > 0)
{
if ( usleep( startTimeN) == -1) {
// exit when usleep interrupted by kill signal
if (errno == EINTR)exit(4);
}
}
printf("COW %8d COW cow grazes for %f ms\n", localpid, startTimeN / 1000.0);
semopChecked(semID, &WaitProtectCowsInGroup, 1);
semopChecked(semID, &SignalCowsInGroup, 1);
*cowCounterp = *cowCounterp + 1;
printf("COW %8d COW %d cows have been enchanted \n", localpid, *cowCounterp);
if (*cowCounterp >= COWS_IN_GROUP)
{
for (int k = 0; k < COWS_IN_GROUP; k++) {
semopChecked(semID, &WaitCowsInGroup, 1);
}
*cowCounterp = *cowCounterp - COWS_IN_GROUP;
printf("COW %8d COW The last cow is waiting\n", localpid);
semopChecked(semID, &SignalProtectCowsInGroup, 1);
semopChecked(semID, &WaitProtectMealWaitingFlag, 1);
*mealWaitingFlagp = *mealWaitingFlagp + 1;
printf("COW %8d COW signal meal flag %d\n", localpid, *mealWaitingFlagp);
semopChecked(semID, &SignalProtectMealWaitingFlag, 1);
semopChecked(semID, &SignalDragonSleeping, 1);
printf("COW %8d COW last cow wakes the dragon \n", localpid);
}
else
{
semopChecked(semID, &SignalProtectCowsInGroup, 1);
}
semopChecked(semID, &WaitCowsWaiting, 1);
semopChecked(semID, &WaitProtectCowsEaten, 1);
semopChecked(semID, &SignalCowsEaten, 1);
*cowsEatenCounterp = *cowsEatenCounterp + 1;
if (*cowsEatenCounterp >= COWS_IN_GROUP)
{
for (int k = 0; k < COWS_IN_GROUP; k++)
{
semopChecked(semID, &WaitCowsEaten, 1);
}
*cowsEatenCounterp = *cowsEatenCounterp - COWS_IN_GROUP;
printf("COW %8d COW The last cow has been eaten\n", localpid);
semopChecked(semID, &SignalProtectCowsEaten, 1);
semopChecked(semID, &SignalDragonEating, 1);
}
else
{
semopChecked(semID, &SignalProtectCowsEaten, 1);
printf("COW %8d COW A cow is waiting to be eaten\n", localpid);
}
semopChecked(semID, &WaitCowsDead, 1);
printf("COW %8d COW cow dies\n", localpid);
}
void terminateSimulation()
{
pid_t localpgid;
pid_t localpid;
int w = 0;
int status;
localpid = getpid();
printf("RELEASESEMAPHORES Terminating Simulation from process %8d\n",
localpgid);
if (cowProcessGID != (int)localpgid )
{
if (killpg(cowProcessGID, SIGKILL) == -1 && errno == EPERM)
{
printf("TERMINATE COWS NOT KILLED\n");
}
printf("TERMINATE killed cows \n");
}
if (sheepProcessGID != (int)localpgid )
{
if (killpg(sheepProcessGID, SIGKILL) == -1 && errno == EPERM)
{
printf("TERMINATE SHEEPS NOT KILLED\n");
}
printf("TERMINATE killed sheeps \n");
}
if (smaugProcessID != (int)localpgid )
{
kill(smaugProcessID, SIGKILL);
printf("TERMINATE killed smaug\n");
}
/**
* -1 = special case (can put PID instead), any process that hasn't had
* their status checked; WNOHANG = not a blocking version of pid_t
*/
while ( (w = waitpid( -1, &status, WNOHANG)) > 1)
{
printf("REAPED process in terminate %d\n", w);
}
releaseSemandMem();
printf("GOODBYE from terminate\n");
}
void releaseSemandMem()
{
pid_t localpid;
int w = 0;
int status;
localpid = getpid();
//should check return values for clean termination
semctl(semID, 0, IPC_RMID, seminfo);
// wait for the semaphores
usleep(2000);
while ( (w = waitpid( -1, &status, WNOHANG)) > 1)
{
printf("REAPED process in terminate %d\n", w);
}
printf("\n");
if (shmdt(terminateFlagp) == -1)
{
printf("RELEASE terminateFlag share memory detach failed\n");
}
else
{
printf("RELEASE terminateFlag share memory detached\n");
}
if ( shmctl(terminateFlag, IPC_RMID, NULL ))
{
printf("RELEASE share memory delete failed %d\n", *terminateFlagp );
}
else {
printf("RELEASE share memory deleted\n");
}
if ( shmdt(sheepCounterp) == -1)
{
printf("RELEASE sheepCounterp memory detach failed\n");
}
else
{
printf("RELEASE sheepCounterp memory detached\n");
}
if ( shmctl(sheepCounter, IPC_RMID, NULL ))
{
printf("RELEASE sheepCounter memory delete failed\n");
}
else
{
printf("RELEASE sheepCounter memory deleted\n");
}
if ( shmdt(cowCounterp) == -1)
{
printf("RELEASE cowCounterp memory detach failed\n");
}
else
{
printf("RELEASE cowCounterp memory detached\n");
}
if ( shmctl(cowCounter, IPC_RMID, NULL ))
{
printf("RELEASE cowCounter memory delete failed \n");
}
else {
printf("RELEASE cowCounter memory deleted\n");
}
if ( shmdt(mealWaitingFlagp) == -1)
{
printf("RELEASE mealWaitingFlagp memory detach failed\n");
}
else {
printf("RELEASE mealWaitingFlagp memory detached\n");
}
if ( shmctl(mealWaitingFlag, IPC_RMID, NULL ))
{
printf("RELEASE mealWaitingFlag share memory delete failed \n");
}
else {
printf("RELEASE mealWaitingFlag share memory deleted\n");
}
if ( shmdt(sheepsEatenCounterp) == -1)
{
printf("RELEASE sheepsEatenCounterp memory detach failed\n");
}
else
{
printf("RELEASE sheepsEatenCounterp memory detached\n");
}
if ( shmctl(sheepsEatenCounter, IPC_RMID, NULL ))
{
printf("RELEASE sheepsEatenCounter memory delete failed\n");
}
else {
printf("RELEASE sheepsEatenCounter memory detached\n");
}
if ( shmdt(cowsEatenCounterp) == -1)
{
printf("RELEASE cowsEatenCounterp memory detach failed\n");
}
else {
printf("RELEASE cowsEatenCounterp memory detached\n");
}
if ( shmctl(cowsEatenCounter, IPC_RMID, NULL ))
{
printf("RELEASE cowsEatenCounter memory delete failed \n");
}
else {
printf("RELEASE cowsEatenCounter memory deleted\n");
}
}
void semctlChecked(int semaphoreID, int semNum, int flag, union semun seminfo) {
/**
*wrapper that checks if the semaphore control request has terminated
*successfully. If it has not the entire simulation is terminated
*/
if (semctl(semaphoreID, semNum, flag, seminfo) == -1 ) {
if (errno != EIDRM)
{
printf("semaphore control failed: simulation terminating\n");
printf("errno %8d \n", errno );
*terminateFlagp = 1;
releaseSemandMem();
exit(2);
}
else
{
exit(3);
}
}
}
void semopChecked(int semaphoreID, struct sembuf *operation,
unsigned something)
{
/**
*wrapper that checks if the semaphore operation request has terminated
*successfully. If it has not the entire simulation is terminated
*/
if (semop(semaphoreID, operation, something) == -1 )
{
if (errno != EIDRM)
{
printf("semaphore operation failed: simulation terminating\n");
*terminateFlagp = 1;
releaseSemandMem();
exit(2);
}
else
{
exit(3);
}
}
}
double timeChange( const struct timeval startTime )
{
struct timeval nowTime;
double elapsedTime;
gettimeofday(&nowTime, NULL);
elapsedTime = (nowTime.tv_sec - startTime.tv_sec) * 1000.0;
elapsedTime += (nowTime.tv_usec - startTime.tv_usec) / 1000.0;
return elapsedTime;
}
int main()
{
initialize();
double sheepTimer = 0;
double cowTimer = 0;
/* Prompt user to enter seed number*/
int seed;
printf("Please enter a random seed to start the simulation: ");
scanf("%d", &seed);
srand(seed);
// printf("You have entered in %d for seed \n", seed);
int maxCowInt;
printf("Please enter a maximum interval length for cow (ms): ");
scanf("%d", &maxCowInt);
// printf("You have entered in %d for maximum interval length \n", maxCowInt);
int maxSheepInt;
printf("Please enter a maximum interval length for sheep (ms): ");
scanf("%d", &maxSheepInt);
// printf("You have entered in %d for maximum interval length \n", maxSheepInt);
parentProcessGID = getpid();
smaugProcessID = -1;
sheepProcessGID = parentProcessGID - 1;
cowProcessGID = parentProcessGID - 2;
pid_t childPID = fork();
if (childPID < 0)
{
printf("FORK FAILED\n");
return 1;
} else if (childPID = 0)
{
smaug();
return 0;
}
smaugProcessID = childPID;
gettimeofday(&startTime, NULL);
while (*terminateFlagp == 0)
{
// while no termination conditions have been met
double duration = timeChange(startTime);
if (sheepTimer <= duration)
{
int childPID = fork();
if (childPID == 0)
{
// if child process, create a sheep
sheep(duration);
return 0;
}
}
}
terminateSimulation();
return 0;
}