-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbus_funcs.c
466 lines (331 loc) · 11.8 KB
/
bus_funcs.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shared_memory.h"
#include "bus_funcs.h"
char* destinations[] = {"ASK", "PEL", "VOR", NULL};
int get_bus_input(char* argv[], Bus* bus, key_t* shmid_key)
{
uint8_t flag_t = 0;
uint8_t flag_n = 0;
uint8_t flag_c = 0;
uint8_t flag_p = 0;
uint8_t flag_m = 0;
uint8_t flag_i = 0;
uint8_t flag_s = 0;
uint8_t error_input = 0;
int i = 1;
for (; i < BUS_ARGS; i += 2)
{
if (!strcmp(argv[i], "-t"))
{
if (!flag_t) {
flag_t = 1;
}
else {
printf("Error, parameter -t is given more than 1 time in bus.c. Exiting..\n");
error_input = 1;
break;
}
if (!strcmp(argv[i + 1], "ASK")) {
bus->type = ASK;
}
else if (!strcmp(argv[i + 1], "PEL")) {
bus->type = PEL;
}
else if (!strcmp(argv[i + 1], "VOR")) {
bus->type = VOR;
}
else {
printf("Wrong value given for bus type. Correct values are: -t ASK / PEL / VOR\n");
error_input = 1;
break;
}
}
else if (!strcmp(argv[i], "-n"))
{
if (!flag_n) {
flag_n = 1;
} else {
printf("Error, parameter -n is given more than 1 time in bus.c. Exiting..\n");
error_input = 1;
break;
}
int n = atoi(argv[i + 1]);
if (n < 0) {
printf("Wrong value given for -n parameter in bus.c, it should be >= 0. Exiting..\n");
error_input = 1;
break;
} else if (flag_c && bus->capacity < n) {
printf("Wrong value for -c and -n parameters. Number of current passengers (%d) can't exceed the capacity (%d) of the bus.\n", n, bus->capacity);
error_input = 1;
break;
}
bus->on_board_passengers = n;
}
else if (!strcmp(argv[i], "-c"))
{
if (!flag_c) {
flag_c = 1;
} else {
printf("Error, parameter -c is given more than 1 time in bus.c. Exiting..\n");
error_input = 1;
break;
}
int c = atoi(argv[i + 1]);
if (c <= 0) {
printf("Wrong value given for -c parameter in bus.c, it should be positive. Exiting..\n");
error_input = 1;
break;
} else if (flag_n && bus->on_board_passengers > c) {
printf("Wrong value for -c and -n parameters. Number of current passengers (%d) can't exceed the capacity (%d) of the bus.\n", bus->on_board_passengers, c);
error_input = 1;
break;
}
bus->capacity = c;
}
else if (!strcmp(argv[i], "-p"))
{
if (!flag_p) {
flag_p = 1;
} else {
printf("Error, parameter -p is given more than 1 time in bus.c. Exiting..\n");
error_input = 1;
break;
}
char* endptr = NULL;
double p = strtod(argv[i + 1], &endptr);
if (p <= 0) {
printf("Wrong value for -p parameter in bus.c, it should be positive. Exiting..\n");
error_input = 1;
break;
}
bus->parkperiod = p;
}
else if (!strcmp(argv[i], "-m"))
{
if (!flag_m) {
flag_m = 1;
} else {
printf("Error, parameter -m is given more than 1 time in bus.c. Exiting..\n");
error_input = 1;
break;
}
char* endptr = NULL;
double m = strtod(argv[i + 1], &endptr);
if (m <= 0) {
printf("Wrong value for -m parameter in bus.c, it should be positive. Exiting..\n");
error_input = 1;
break;
}
bus->mantime = m;
}
else if (!strcmp(argv[i], "-i"))
{
if (!flag_i) {
flag_i = 1;
} else {
printf("Error, parameter -i is given more than 1 time in bus.c. Exiting..\n");
error_input = 1;
break;
}
strcpy(bus->id, argv[i + 1]);
}
else if (!strcmp(argv[i], "-s"))
{
if (!flag_s) {
flag_s = 1;
} else {
printf("Error, parameter -s is given more than 1 time in bus.c. Exiting..\n");
error_input = 1;
break;
}
key_t s = atoi(argv[i + 1]);
if (s <= 0) {
printf("Wrong value for -s parameter in bus.c, it should be positive. Exiting..\n");
error_input = 1;
break;
}
*shmid_key = s;
}
else {
printf("No matching flags for this input: \"%s\" in bus.c. Available flags are -t, -n, -c, -p, -m, -s, -i.\n", argv[i]);
printf("Error in bus_funcs.c::get_bus_input()\n");
error_input = 1;
break;
}
}
return !error_input;
}
void bus_init(Bus* bus)
{
time_t arrival_time = time(NULL);
bus->arrival_time = *localtime(&arrival_time);
}
void park_to_spot(Bus* bus, shared_segment_t* shared_memory, Destination dest, uint8_t spot)
{
/* copy the bus information to the ledger so that the comptroller can see it */
memcpy(&(shared_memory->ledger.bays[dest].spots[spot].current_bus), bus, sizeof(Bus));
/* mark the spot as not availabe because the bus parked in it */
shared_memory->ledger.bays[dest].spots[spot].has_bus = TRUE;
/* increment the number of parked buses in the specific bay */
shared_memory->ledger.bays[dest].spots_taken++;
}
void Park(Bus* bus, shared_segment_t* shared_memory, Destination dest, uint8_t* spot)
{
// remove this if later
if (shared_memory->ledger.bays[dest].spots_taken == shared_memory->ledger.bays[dest].total_spots)
{
printf("SHOULD HAVE NEVER PRINTED THIS. ERROR IN bus_funcs::Park()\n");
printf("dest = %d\n", dest);
printf("spots_taken = %d, total_spots = %d\n", shared_memory->ledger.bays[dest].spots_taken,
shared_memory->ledger.bays[dest].total_spots);
printf("shared_memory->ledger.requesting_access_to_enter = %d && shared_memory->ledger.is_ready_to_enter = %d\n",
shared_memory->ledger.requesting_access_to_enter, shared_memory->ledger.is_ready_to_enter);
free(bus);
exit(EXIT_FAILURE);
}
/* find the spot that the bus will park */
int temp_spot = 0;
for (; temp_spot < SPOTS; temp_spot++)
{
if (shared_memory->ledger.bays[dest].spots[temp_spot].has_bus == FALSE)
{
sem_wait(&(shared_memory->bays_mutex[dest]));
/* critical section of the destination bay */
/* spot has been found, so park the bus, save the spot and return */
park_to_spot(bus, shared_memory, dest, temp_spot);
*spot = temp_spot;
/* sleep while the manuever happens */
sleep((int) bus->mantime);
/* get the park time and save it in the bus struct */
time_t park_time = time(NULL);
bus->park_time = *localtime(&park_time);
/* end of critical section of the destination bay */
sem_post(&(shared_memory->bays_mutex[dest]));
return;
}
}
}
int Drop_Passenger(Bus* bus, shared_segment_t* shared_memory, Destination islet, uint8_t spot)
{
/* define a random amount of passengers to drop */
uint16_t random_drop = rand() % (bus->on_board_passengers + 1);
/* decrease the amount of on board passengers in the bus* variable which has
scope inside the bus process */
bus->on_board_passengers -= random_drop;
/* do the same for the shared segment variable which is not restricted by
any scope, and therefore the compotroller can access it */
shared_memory->ledger.bays[islet].spots[spot].current_bus.on_board_passengers -= random_drop;
sem_wait(&(shared_memory->bays_mutex[islet]));
/* critical section for the bay */
shared_memory->ledger.bays[islet].total_passengers_dropped += random_drop;
/* end of critical section for the bay */
sem_post(&(shared_memory->bays_mutex[islet]));
return random_drop;
}
void Wait_Parked(Bus* bus)
{
uint8_t prob = (uint8_t) bus->parkperiod + 1;
uint8_t random_sleep_time = rand() % prob;
sleep(random_sleep_time);
}
int Pick_Passengers(Bus* bus, shared_segment_t* shared_memory, Destination islet, uint8_t spot)
{
/* define a random amount of passengers to pick up */
uint16_t random_pickup = rand() % (bus->capacity - bus->on_board_passengers + 1);
/* increase the amount of on board passengers in the bus* variable which has
scope inside the bus process */
bus->on_board_passengers += random_pickup;
/* do the same for the shared segment variable which is not restricted by
any scope, and therefore the compotroller can access it */
shared_memory->ledger.bays[islet].spots[spot].current_bus.on_board_passengers += random_pickup;
sem_wait(&(shared_memory->bays_mutex[islet]));
/* critical section for the bay */
shared_memory->ledger.bays[islet].total_passengers_picked += random_pickup;
/* end of critical section for the bay */
sem_post(&(shared_memory->bays_mutex[islet]));
return random_pickup;
}
void Leave(Bus* bus, shared_segment_t* shared_memory, Destination islet, uint8_t spot)
{
sem_wait(&(shared_memory->bays_mutex[islet]));
/* critical section of the current islet (bay) */
/* sleep while the manuever happens */
sleep((int) bus->mantime);
/* delete the bus information */
memset(&(shared_memory->ledger.bays[islet].spots[spot].current_bus), 0, sizeof(Bus));
/* mark the spot that the bus had, as availabe */
shared_memory->ledger.bays[islet].spots[spot].has_bus = FALSE;
shared_memory->ledger.bays[islet].spots_taken--;
/* end of critical section of the current islet (bay) */
sem_post(&(shared_memory->bays_mutex[islet]));
/* get the departure time and save it in the bus struct */
time_t departure_time = time(NULL);
bus->departure_time = *localtime(&departure_time);
}
/* pretty much self explanatory */
void write_status_to_logfile(shared_segment_t* shared_memory, Bus* bus, int bay, int status)
{
FILE* logfile = NULL;
logfile = fopen(shared_memory->logfile_name, "a");
if (status == ARRIVE)
{
fprintf(logfile, "Bus with id %s arrived at: %d:%d:%d.\n", bus->id, (bus->arrival_time).tm_hour, (bus->arrival_time).tm_min, (bus->arrival_time).tm_sec);
}
else if (status == PARK)
{
fprintf(logfile, "Bus with id %s parked at bay %s at: %d:%d:%d.\n", bus->id, destinations[bay], (bus->park_time).tm_hour, (bus->park_time).tm_min, (bus->park_time).tm_sec);
}
else
{
fprintf(logfile, "Bus with id %s departed from bay %s at: %d:%d:%d.\n", bus->id, destinations[bay], (bus->departure_time).tm_hour, (bus->departure_time).tm_min, (bus->departure_time).tm_sec);
}
fclose(logfile);
}
/* pretty much self explanatory */
void write_activity_to_logfile(shared_segment_t* shared_memory, Bus* bus, int activity, int amount, int bay)
{
FILE* logfile = NULL;
logfile = fopen(shared_memory->logfile_name, "a");
if (activity == DROP)
{
fprintf(logfile, "Bus with id %s dropped %d passengers at bay %s.\n", bus->id, amount, destinations[bay]);
}
else
{
fprintf(logfile, "Bus with id %s picked up %d passengers from bay %s.\n", bus->id, amount, destinations[bay]);
}
fclose(logfile);
}
void update_statistics(shared_segment_t* shared_memory, Bus* bus)
{
unsigned long waiting_to_park_time = waiting_time(bus->arrival_time, bus->park_time);
shared_memory->ledger.sum_of_waiting_to_park_times += waiting_to_park_time;
shared_memory->ledger.counter_waiting_to_park_times++;
unsigned long waiting_to_leave_time = waiting_time(bus->park_time, bus->departure_time);
shared_memory->ledger.sum_of_waiting_to_leave_times += waiting_to_leave_time;
shared_memory->ledger.counter_waiting_to_leave_times++;
}
/* pretty much self explanatory */
void print_bus_info(Bus* bus, int bay, int spot)
{
if (bus->type == ASK)
{
printf("Bus type: ASK,");
}
else if (bus->type == PEL)
{
printf("Bus type: PEL,");
}
else
{
printf("Bus type: VOR,");
}
printf(" parked in bay %s, in spot %d\n", destinations[bay], spot + 1);
printf("on_board_passengers: %d\n", bus->on_board_passengers);
printf("capacity: %d\n", bus->capacity);
printf("parkperiod: %lf\n", bus->parkperiod);
printf("mantime: %lf\n", bus->mantime);
printf("id: %s\n", bus->id);
}