forked from liquiddandruff/cmpt300ass4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathferryMSG.c
More file actions
509 lines (443 loc) · 19.8 KB
/
ferryMSG.c
File metadata and controls
509 lines (443 loc) · 19.8 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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <wait.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// Definitions used to control simulation termination
#define MIN_VEHICLE_ARRIVAL_INTERVAL 100
#define MAX_LOADS 11
#define MAX_SPACES_ON_FERRY 6
#define MAX_TRUCKS_ON_FERRY 2
// Definitions used for msgs
#define MSG_TYPE_CAR 1
#define MSG_TYPE_TRUCK 2
#define MSG_TYPE_ONBOARD 3
#define MSG_TYPE_TRAVELING 4
#define MSG_TYPE_ARRIVED 5
#define MSG_TYPE_UNLOAD 9
#define MSG_TYPE_UNLOAD_ACK 10
#define MSG_VEHICLE_IS_WAITING 11
#define MSG_VEHICLE_IS_LATE 12
#define MSG_SWITCH_TO_WAITING 13
#define SIZE_OF_CAR 1
#define SIZE_OF_TRUCK 2
typedef struct mymsgbuf
{
long mtype; // the type of msg
int data; // data pertaining to msg
int pid; // used as process (vehicle) identifier
} mess_t;
// We will use the same buffer format (and size) for each msg, no matter the queue
mess_t msg;
int msgSize;
// Queue IDS
int queueCaptainToVehicles;
// Only msgs from vehicles notifying the captain that they are waiting to board the ferry are in this queue
int queueVehiclesToCaptain;
int queueVehiclesWaiting;
int queueVehiclesLate;
int queueVehiclesBoarding;
int queueVehiclesOnboard;
// Process IDS
int mainProcessID;
int captainProcessPID;
int vehicleCreationProcessPID;
int vehicleProcessGID;
int truckArrivalProb;
int maxTimeToNextVehicleArrival;
// Forward declarations
int timeChange( const struct timeval startTime );
void init();
void cleanup();
int carProcess() {
int localpid = getpid();
setpgid(localpid, vehicleProcessGID);
printf("CARCAR CARCAR CARCAR Car %d arrives at ferry dock\n", localpid);
while(1) {
msg.mtype = MSG_TYPE_CAR; msg.pid = localpid;
// msgsnd sends a _copy_ of the msg to the queue, so no need to instantiate msgs for each snd
// Since same msg structure is used, we can use same msgSize for each msgsnd
// Exit process with failure code upon msgsnd failure
if(msgsnd(queueVehiclesToCaptain, &msg, msgSize, 0) == -1) { return -1; }
// Wait for signal from captain telling this vehicle that the ferry is about to load
// Blocks when flag=0. Only looks at the mtypes concerning the PID of this process.
// Checks msg.data to determine if this vehicle is waiting or late
// Exit process with failure code upon msgsnd failure
if(msgrcv(queueCaptainToVehicles, &msg, msgSize, localpid, 0) == -1) { return -1; }
msg.mtype = MSG_TYPE_CAR; msg.pid = localpid;
if(msg.data == MSG_VEHICLE_IS_LATE) {
printf("CARCAR CARCAR CARCAR Car %d acks that it is %s\n", localpid, "late");
if(msgsnd(queueVehiclesLate, &msg, msgSize, 0) == -1) { return -1; }
} else {
printf("CARCAR CARCAR CARCAR Car %d acks that it is %s\n", localpid, "waiting");
if(msgsnd(queueVehiclesWaiting, &msg, msgSize, 0) == -1) { return -1; }
}
// Block until vehicle receives signal to leave its current queue and start boarding the ferry
if(msgrcv(queueVehiclesBoarding, &msg, msgSize, localpid, 0) == -1) { return -1; }
// Break out of while only if not told to switch to waiting queue
if(msg.data != MSG_SWITCH_TO_WAITING)
break;
else
printf("CARCAR CARCAR CARCAR Car %d was late from last load, but won't be late this time\n", localpid);
}
printf("CARCAR CARCAR CARCAR Car %d acks Captain's signal to leave its queue\n", localpid);
printf("CARCAR CARCAR CARCAR Car %d is onboard the ferry\n", localpid);
msg.mtype = MSG_TYPE_ONBOARD; msg.data = MSG_TYPE_CAR; msg.pid = localpid;
if(msgsnd(queueVehiclesOnboard, &msg, msgSize, 0) == -1) { return -1; }
// Block until vehicle receives signal to sail
if(msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_TRAVELING, 0) == -1) { return -1; }
printf("CARCAR CARCAR CARCAR Car %d acks Captain's signal that it is sailing\n", localpid);
// Block until vehicle receives signal that it has arrived to destination
if(msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_ARRIVED, 0) == -1) { return -1; }
printf("CARCAR CARCAR CARCAR Car %d acks Captain's signal that it has arrived\n", localpid);
// Block until vehicle receives signal to unload
if(msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_UNLOAD, 0) == -1) { return -1; }
printf("CARCAR CARCAR CARCAR Car %d acks Captain's signal to unload. Car unloads from ferry and exits.\n", localpid);
// Tell Captain that this vehicle has succesfully unloaded
printf("CARCAR CARCAR CARCAR Car %d tells Captain that it has unloaded. Car exits.\n", localpid);
msg.mtype = MSG_TYPE_UNLOAD_ACK; msg.data = MSG_TYPE_CAR; msg.pid = localpid;
if(msgsnd(queueVehiclesOnboard, &msg, msgSize, 0) == -1) { return -1; }
return 0;
}
int truckProcess() {
int localpid = getpid();
setpgid(localpid, vehicleProcessGID);
printf("TRUCKTRUCK TRUCKTRUC Truck %d arrives at ferry dock\n", localpid);
while(1) {
msg.mtype = MSG_TYPE_TRUCK; msg.pid = localpid;
if(msgsnd(queueVehiclesToCaptain, &msg, msgSize, 0) == -1) { return -1; }
if(msgrcv(queueCaptainToVehicles, &msg, msgSize, localpid, 0) == -1) { return -1; }
msg.mtype = MSG_TYPE_TRUCK; msg.pid = localpid;
if(msg.data == MSG_VEHICLE_IS_LATE) {
printf("TRUCKTRUCK TRUCKTRUC Truck %d acks that it is %s\n", localpid, "late");
if(msgsnd(queueVehiclesLate, &msg, msgSize, 0) == -1) { return -1; }
} else {
printf("TRUCKTRUCK TRUCKTRUC Truck %d acks that it is %s\n", localpid, "waiting");
if(msgsnd(queueVehiclesWaiting, &msg, msgSize, 0) == -1) { return -1; }
}
if(msgrcv(queueVehiclesBoarding, &msg, msgSize, localpid, 0) == -1) { return -1; }
if(msg.data != MSG_SWITCH_TO_WAITING)
break;
else
printf("TRUCKTRUCK TRUCKTRUC Truck %d was late from last load, but won't be late this time\n", localpid);
}
printf("TRUCKTRUCK TRUCKTRUC Truck %d acks Captain's signal to leave its queue\n", localpid);
printf("TRUCKTRUCK TRUCKTRUC Truck %d is onboard the ferry\n", localpid);
msg.mtype = MSG_TYPE_ONBOARD; msg.data = MSG_TYPE_TRUCK; msg.pid = localpid;
if(msgsnd(queueVehiclesOnboard, &msg, msgSize, 0) == -1) { return -1; }
if(msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_TRAVELING, 0) == -1) { return -1; }
printf("TRUCKTRUCK TRUCKTRUC Truck %d acks Captain's signal that it is sailing\n", localpid);
if(msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_ARRIVED, 0) == -1) { return -1; }
printf("TRUCKTRUCK TRUCKTRUC Truck %d acks Captain's signal that it has arrived\n", localpid);
if(msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_UNLOAD, 0) == -1) { return -1; }
printf("TRUCKTRUCK TRUCKTRUC Truck %d acks Captain's signal to unload. Truck unloads from ferry\n", localpid);
printf("TRUCKTRUCK TRUCKTRUC Truck %d tells Captain that it has unloaded. Truck exits.\n", localpid);
msg.mtype = MSG_TYPE_UNLOAD_ACK; msg.data = MSG_TYPE_TRUCK; msg.pid = localpid;
if(msgsnd(queueVehiclesOnboard, &msg, msgSize, 0) == -1) { return -1; }
return 0;
}
int captainProcess() {
char *vehicleType = "";
int currentLoad = 1;
int numberOfCarsLoaded = 0;
int numberOfTrucksLoaded = 0;
int numberOfSpacesFilled = 0;
int counter = 0;
printf("CAPTAIN CAPTAIN CAPT CaptainProcess created. PID is: %d\n", getpid());
while(currentLoad < MAX_LOADS) {
numberOfTrucksLoaded = 0;
numberOfCarsLoaded = 0;
numberOfSpacesFilled = 0;
printf("CAPTAIN CAPTAIN CAPT load %d/%d started\n", currentLoad, MAX_LOADS);
printf("CAPTAIN CAPTAIN CAPT The ferry is about to load!\n");
// Receive all msgs from queue sent from vehicles, break until queue is empty
// Tell these vehicles that they are in the main lane (they are waiting vehicles)
while(msgrcv(queueVehiclesToCaptain, &msg, msgSize, 0, IPC_NOWAIT) != -1) {
vehicleType = msg.mtype == MSG_TYPE_CAR ? "Car" : "Truck";
printf("CAPTAIN CAPTAIN CAPT Captain tells %s %d that it is waiting\n", vehicleType, msg.pid);
msg.mtype = msg.pid; msg.data = MSG_VEHICLE_IS_WAITING;
if(msgsnd(queueCaptainToVehicles, &msg, msgSize, 0) == -1) {
return -1;
}
}
while(numberOfSpacesFilled < MAX_SPACES_ON_FERRY) {
// Tell all vehicles that are just now arriving that they are late
while(msgrcv(queueVehiclesToCaptain, &msg, msgSize, 0, IPC_NOWAIT) != -1) {
vehicleType = msg.mtype == MSG_TYPE_CAR ? "Car" : "Truck";
printf("CAPTAIN CAPTAIN CAPT Captain tells %s %d that it is late\n", vehicleType, msg.pid);
msg.mtype = msg.pid; msg.data = MSG_VEHICLE_IS_LATE;
if(msgsnd(queueCaptainToVehicles, &msg, msgSize, 0) == -1) {
return -1;
}
}
// Signal first two waiting trucks, if conditions are satisfied
while(numberOfTrucksLoaded < MAX_TRUCKS_ON_FERRY &&
numberOfSpacesFilled + SIZE_OF_TRUCK <= MAX_SPACES_ON_FERRY &&
msgrcv(queueVehiclesWaiting, &msg, msgSize, MSG_TYPE_TRUCK, IPC_NOWAIT) != -1) {
printf("CAPTAIN CAPTAIN CAPT Captain tells Truck %d to leave waiting queue\n", msg.pid);
msg.mtype = msg.pid;
if(msgsnd(queueVehiclesBoarding, &msg, msgSize, 0) == -1) {
return -1;
}
numberOfTrucksLoaded++;
numberOfSpacesFilled += SIZE_OF_TRUCK;
}
// Signal the rest of the waiting cars
while(numberOfSpacesFilled + SIZE_OF_CAR <= MAX_SPACES_ON_FERRY &&
msgrcv(queueVehiclesWaiting, &msg, msgSize, MSG_TYPE_CAR, IPC_NOWAIT) != -1) {
printf("CAPTAIN CAPTAIN CAPT Captain tells Car %d to leave waiting queue\n", msg.pid);
msg.mtype = msg.pid;
if(msgsnd(queueVehiclesBoarding, &msg, msgSize, 0) == -1) {
return -1;
}
numberOfCarsLoaded++;
numberOfSpacesFilled += SIZE_OF_CAR;
}
// Do the same with late vehicles
while(numberOfTrucksLoaded < MAX_TRUCKS_ON_FERRY &&
numberOfSpacesFilled + SIZE_OF_TRUCK <= MAX_SPACES_ON_FERRY &&
msgrcv(queueVehiclesLate, &msg, msgSize, MSG_TYPE_TRUCK, IPC_NOWAIT) != -1) {
printf("CAPTAIN CAPTAIN CAPT Captain tells Truck %d to leave late queue\n", msg.pid);
msg.mtype = msg.pid;
if(msgsnd(queueVehiclesBoarding, &msg, msgSize, 0) == -1) {
return -1;
}
numberOfTrucksLoaded++;
numberOfSpacesFilled += SIZE_OF_TRUCK;
}
while(numberOfSpacesFilled + SIZE_OF_CAR <= MAX_SPACES_ON_FERRY &&
msgrcv(queueVehiclesLate, &msg, msgSize, MSG_TYPE_CAR, IPC_NOWAIT) != -1) {
printf("CAPTAIN CAPTAIN CAPT Captain tells Car %d to leave late queue\n", msg.pid);
msg.mtype = msg.pid;
if(msgsnd(queueVehiclesBoarding, &msg, msgSize, 0) == -1) {
return -1;
}
numberOfCarsLoaded++;
numberOfSpacesFilled += SIZE_OF_CAR;
}
// Check for number of vehicles onboard
while(1) {
int ret = msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_ONBOARD, IPC_NOWAIT);
if(ret != -1) {
vehicleType = msg.data == MSG_TYPE_CAR ? "Car" : "Truck";
printf("CAPTAIN CAPTAIN CAPT Captain acks that %s %d is onboard\n", vehicleType, msg.pid);
// Break from loop if queue is empty
} else if (errno == ENOMSG) {
break;
// Exit process with failure code -1 only if we get an actual error
} else { return -1; }
}
// If we are indeed at the maximum that we can carry, signal all vehicles to travel
if(numberOfCarsLoaded * SIZE_OF_CAR + numberOfTrucksLoaded * SIZE_OF_TRUCK == MAX_SPACES_ON_FERRY) {
printf("CAPTAIN CAPTAIN CAPT The ferry is full!\n");
printf("CAPTAIN CAPTAIN CAPT Captain tells the boarded vehicles that they are sailing\n");
msg.mtype = MSG_TYPE_TRAVELING;
for(counter = 0; counter < numberOfCarsLoaded + numberOfTrucksLoaded; counter++)
if(msgsnd(queueVehiclesOnboard, &msg, msgSize, 0) == -1) { return -1; }
printf("CAPTAIN CAPTAIN CAPT All vehicles ack'd they are sailing\n");
// Teleport!
printf("CAPTAIN CAPTAIN CAPT Arrived at destination and docked\n");
printf("CAPTAIN CAPTAIN CAPT Captain tells the boarded vehicles that they have arrived\n");
msg.mtype = MSG_TYPE_ARRIVED;
for(counter = 0; counter < numberOfCarsLoaded + numberOfTrucksLoaded; counter++)
if(msgsnd(queueVehiclesOnboard, &msg, msgSize, 0) == -1) { return -1; }
// Unload!
printf("CAPTAIN CAPTAIN CAPT Captain tells the boarded vehicles to unload\n");
msg.mtype = MSG_TYPE_UNLOAD;
for(counter = 0; counter < numberOfCarsLoaded + numberOfTrucksLoaded; counter++)
if(msgsnd(queueVehiclesOnboard, &msg, msgSize, 0) == -1) { return -1; }
// Ack that all vehicles have unloaded
// Blocks with a spin lock (as opposed to calling msgrcv with flag=0) so captain
// will stop blocking when it knows that all vehicles have undocked.
counter = 0;
while(counter < numberOfCarsLoaded + numberOfTrucksLoaded) {
int ret = msgrcv(queueVehiclesOnboard, &msg, msgSize, MSG_TYPE_UNLOAD_ACK, IPC_NOWAIT);
if(ret == msgSize) {
vehicleType = msg.data == MSG_TYPE_CAR ? "Car" : "Truck";
printf("CAPTAIN CAPTAIN CAPT Captain acks that %s %d has unloaded\n", vehicleType, msg.pid);
counter++;
// Exit process with failure code -1 only if we get an actual error
} else if(ret == -1 && errno != ENOMSG) { return -1; }
}
printf("CAPTAIN CAPTAIN CAPT All vehicles have unloaded\n");
// Done, but we need to move all currently late vehicles to waiting
while(msgrcv(queueVehiclesLate, &msg, msgSize, 0, IPC_NOWAIT) != -1) {
msg.mtype = msg.pid; msg.data = MSG_SWITCH_TO_WAITING;
if(msgsnd(queueVehiclesBoarding, &msg, msgSize, 0) == -1) { return -1; }
}
printf("CAPTAIN CAPTAIN CAPT All late vehicles now waiting\n");
}
}
printf("\n\n\n");
printf("CAPTAIN CAPTAIN CAPT Arrived at main loading dock from destination\n");
currentLoad++;
if(currentLoad >= MAX_LOADS)
printf("CAPTAIN CAPTAIN CAPT load %d/%d started. Terminating simulation!\n", currentLoad, MAX_LOADS);
}
return 0;
}
int vehicleCreationProcess() {
int localpid = getpid();
// Time at start of process creation
struct timeval startTime;
// Time from start of process creation to current time
int elapsed = 0;
// Time at which last vehicle arrived
int lastArrivalTime = 0;
printf("CREATE CREATE CREATE vehicleCreationProcess created. PID is: %d \n", localpid);
gettimeofday(&startTime, NULL);
elapsed = timeChange(startTime);
srand (elapsed*1000+44);
int zombieTick = 0;
while(1) {
/* If present time is later than arrival time of the next vehicle */
/* Determine if the vehicle is a car or truck */
/* Have the vehicle put a message in the queue indicating */
/* that it is in line */
/* Then determine the arrival time of the next vehicle */
elapsed = timeChange(startTime);
if(elapsed >= lastArrivalTime) {
printf("CREATE CREATE CREATE elapsed time %d arrival time %d\n", elapsed, lastArrivalTime);
if(lastArrivalTime > 0 ) {
if(rand() % 100 < truckArrivalProb ) {
/* This is a truck */
// If the fork fails for any reason, stop the simulation
printf("CREATE CREATE CREATE Created a truck process\n");
int childPID = fork();
if(childPID == 0) {
return truckProcess();
} else if(childPID == -1) {
return -1;
}
}
else {
/* This is a car */
printf("CREATE CREATE CREATE Created a car process\n");
int childPID = fork();
if(childPID == 0) {
return carProcess();
} else if(childPID == -1) {
return -1;
}
}
}
lastArrivalTime += rand()% maxTimeToNextVehicleArrival;
printf("CREATE CREATE CREATE present time %d, next arrival time %d\n", elapsed, lastArrivalTime);
}
zombieTick++;
// Purge all zombies every 10 iteratinos
if(zombieTick % 10 == 0) {
zombieTick -= 10;
// arg1 is -1 to wait for all child processes
int w = 0; int status = 0;
while( (w = waitpid( -1, &status, WNOHANG)) > 1){
printf(" REAPED zombie process %d from vehicle creation process\n", w);
}
}
}
printf("CREATE CREATE CREATE vehicleCreationProcess ended\n");
return 0;
}
int main() {
int status;
init();
printf("Please enter integer values for the following variables\n");
// Truck probability
printf("Enter the percent probability that the next vehicle is a truck (0..100): ");
scanf("%d", &truckArrivalProb);
while(truckArrivalProb < 0 || truckArrivalProb > 100) {
printf("Probability must be between 0 and 100: ");
scanf("%d", &truckArrivalProb);
}
// Vehicle interval
printf("Enter the maximum length of the interval between vehicles (%d..MAX_INT): ", MIN_VEHICLE_ARRIVAL_INTERVAL);
scanf("%d", &maxTimeToNextVehicleArrival);
while(maxTimeToNextVehicleArrival < MIN_VEHICLE_ARRIVAL_INTERVAL) {
printf("Interval must be greater than %d: ", MIN_VEHICLE_ARRIVAL_INTERVAL);
scanf("%d", &maxTimeToNextVehicleArrival);
}
if((captainProcessPID = fork()) == 0) {
return captainProcess();
}
if((vehicleCreationProcessPID = fork()) == 0) {
return vehicleCreationProcess();
}
// Wait for captainProcess to finish
waitpid(captainProcessPID, &status, 0);
printf("captainProcess returnValue: %d\n", WEXITSTATUS(status));
// Once process captainProcess returns, the simulation should end, so send sigkill to vehicleCreationProcess
kill(vehicleCreationProcessPID, SIGKILL);
// Wait for vehicleCreationProcess to finish
waitpid(vehicleCreationProcessPID, &status, 0);
printf("vehicleCreationProcess returnValue: %d\n", WEXITSTATUS(status));
cleanup();
return 0;
}
void init() {
// The mtype is not sent with our msg, so exclude this from the size of each msg
// Additionally, we will use the same buffer format (and size) for each queue
msgSize = sizeof(mess_t) - sizeof(long);
if((queueCaptainToVehicles = msgget(IPC_PRIVATE, IPC_CREAT | 0660)) == -1) {
printf("INITINITINITINITINIT failed to create queue queueCaptainToVehicles\n");
exit(1);
}
if((queueVehiclesToCaptain = msgget(IPC_PRIVATE, IPC_CREAT | 0660)) == -1) {
printf("INITINITINITINITINIT failed to create queue queueVehiclesToCaptain\n");
exit(1);
}
if((queueVehiclesWaiting = msgget(IPC_PRIVATE, IPC_CREAT | 0660)) == -1) {
printf("INITINITINITINITINIT failed to create queue queueVehiclesWaiting\n");
exit(1);
}
if((queueVehiclesLate = msgget(IPC_PRIVATE, IPC_CREAT | 0660)) == -1) {
printf("INITINITINITINITINIT failed to create queue queueVehiclesLate\n");
exit(1);
}
if((queueVehiclesBoarding = msgget(IPC_PRIVATE, IPC_CREAT | 0660)) == -1) {
printf("INITINITINITINITINIT failed to create queue queueVehiclesBoarding\n");
exit(1);
}
if((queueVehiclesOnboard = msgget(IPC_PRIVATE, IPC_CREAT | 0660)) == -1) {
printf("INITINITINITINITINIT failed to create queue queueVehiclesOnboard\n");
exit(1);
}
printf("INITINITINITINITINIT All queues successfully created\n");
mainProcessID = getpid();
vehicleProcessGID = mainProcessID - 1;
}
void destroyQueueChecked(int ret, char *semName) {
if(ret == 0) {
printf("XXXCLEANUP CLEANUPCL Successfully destroyed queue %s\n", semName);
} else {
printf("XXXCLEANUP CLEANUPCL Error destroying queue %s\n", semName);
}
}
void cleanup() {
int w, status;
destroyQueueChecked(msgctl(queueCaptainToVehicles , IPC_RMID , 0) , "queueCaptainToVehicles");
destroyQueueChecked(msgctl(queueVehiclesToCaptain , IPC_RMID , 0) , "queueVehiclesToCaptain");
destroyQueueChecked(msgctl(queueVehiclesWaiting , IPC_RMID , 0) , "queueVehiclesWaiting");
destroyQueueChecked(msgctl(queueVehiclesLate , IPC_RMID , 0) , "queueVehiclesLate");
destroyQueueChecked(msgctl(queueVehiclesBoarding , IPC_RMID , 0) , "queueVehiclesBoarding");
destroyQueueChecked(msgctl(queueVehiclesOnboard , IPC_RMID , 0) , "queueVehiclesOnboard");
if(killpg(vehicleProcessGID, SIGKILL) == -1 && errno == EPERM) {
printf("XXXCLEANUP CLEANUPCL vehicle processes not killed\n");
}
printf("XXXCLEANUP CLEANUPCL all vehicle processes killed\n");
while( (w = waitpid( -1, &status, WNOHANG)) > 1){
printf(" REAPED process in cleanup%d\n", w);
}
}
int timeChange( const struct timeval startTime ) {
struct timeval nowTime;
long int elapsed;
int elapsedTime;
gettimeofday(&nowTime, NULL);
elapsed = (nowTime.tv_sec - startTime.tv_sec) * 1000000 + (nowTime.tv_usec - startTime.tv_usec);
elapsedTime = elapsed / 1000;
return elapsedTime;
}