-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmystation_funcs.c
551 lines (418 loc) · 12.9 KB
/
mystation_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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mystation_funcs.h"
#include "utilities.h"
int get_mystation_input(char* argv[], char** file_name)
{
uint8_t flag_l = 0;
uint8_t error_input = 0;
int i = 1;
for (; i < MYSTATION_ARGS; i += 2)
{
if (!strcmp(argv[i], "-l"))
{
if (!flag_l) {
flag_l = 1;
} else {
printf("Error, parameter -l is given more than 1 time in mystation.c. Exiting..\n");
error_input = 1;
break;
}
*file_name = malloc(sizeof(char) * (strlen(argv[i + 1]) + 1));
strcpy(*file_name, argv[i + 1]);
}
else {
printf("No matching flags for this input: \"%s\" in mystation.c. Available flags are -l.\n", argv[i]);
printf("Error in mystation_funcs.c::get_mystation_input()\n");
error_input = 1;
break;
}
}
if (error_input) {
if (flag_l) {
free(*file_name);
}
}
return !error_input;
}
int get_configfile_input(char* file_name, uint8_t* islets, uint8_t* spots_per_islet,
uint16_t* max_capacity, double* max_park_period,
double* max_man_time, uint32_t* total_buses,
double* state_time, double* statistics_time)
{
char myflag[MAX_BUFFER_SIZE];
char myattribute[MAX_BUFFER_SIZE];
char* buffer = NULL;
FILE* configfile = fopen(file_name, "r");
if (!configfile)
{
free(buffer);
perror("fopen() error in mystation_funcs.c::get_configfile_input()");
return 0;
}
/* keep flags so that we make sure we don't read an attribute twice, in case
the configuration file has an error */
uint8_t flag_islets = 0;
uint8_t flag_spots_per_islet = 0;
uint8_t flag_max_capacity = 0;
uint8_t flag_max_park_period = 0;
uint8_t flag_max_man_time = 0;
uint8_t flag_state_time = 0;
uint8_t flag_statistics_time = 0;
uint8_t flag_total_buses = 0;
/* variable used to indicate errors that may have occured */
uint8_t error = 0;
int i = 0;
for (; i < CONFIGFILE_ARGS; i++)
{
/* if for some reason we reached the end of the file when we shouldn't
have, return with error indication */
if (feof(configfile))
{
printf("Not enough information in the configuration file.\n");
error = 1;
break;
}
/* read a line from the configuration file of the form:
-parameter attribute */
size_t line_buf_size = 0;
if (getline(&buffer, &line_buf_size, configfile) == -1)
{
perror("Error when reading from the configuration file.");
error = 1;
break;
}
/* extract the parameter from the line we just read */
if (get_nth_string(myflag, buffer, 1) == -1)
{
printf("Error when extracting a parameter from the buffer line.\n");
free(buffer);
error = 1;
break;
}
/* extract the attribute from the line we just read */
if (get_nth_string(myattribute, buffer, 2) == -1)
{
printf("Error when extracting an attribute from the buffer line.\n");
free(buffer);
error = 1;
break;
}
/* we don't need the buffer anymore, so free it */
free(buffer);
/* get the parameters from the configuration file, while also checking for
any mistaked that the input may have. If a mistake is spotted, exit and
return an indication of an error */
if (!strcmp(myflag, "-islets"))
{
if (flag_islets)
{
printf("Error, islets has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_islets = 1;
}
int temp = atoi(myattribute);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: islets can't be negative.\n");
error = 1;
break;
}
*islets = temp;
}
else if (!strcmp(myflag, "-spots_per_islet"))
{
if (flag_spots_per_islet)
{
printf("Error, spots_per_islet has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_spots_per_islet = 1;
}
int temp = atoi(myattribute);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: spots_per_islet can't be negative.\n");
error = 1;
break;
}
*spots_per_islet = temp;
}
else if (!strcmp(myflag, "-max_capacity_of_bus"))
{
if (flag_max_capacity)
{
printf("Error, max_capacity has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_max_capacity = 1;
}
int temp = atoi(myattribute);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: max_capacity can't be negative.\n");
error = 1;
break;
}
*max_capacity = temp;
}
else if (!strcmp(myflag, "-max_park_period"))
{
if (flag_max_park_period)
{
printf("Error, max_park_period has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_max_park_period = 1;
}
char* endptr = NULL;
double temp = strtod(myattribute, &endptr);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: max_park_period can't be negative.\n");
error = 1;
break;
}
*max_park_period = temp;
}
else if (!strcmp(myflag, "-max_man_time"))
{
if (flag_max_man_time)
{
printf("Error, max_man_time has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_max_man_time = 1;
}
char* endptr = NULL;
double temp = strtod(myattribute, &endptr);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: max_man_time can't be negative.\n");
error = 1;
break;
}
*max_man_time = temp;
}
else if (!strcmp(myflag, "-state_time"))
{
if (flag_state_time)
{
printf("Error, state_time has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_state_time = 1;
}
char* endptr = NULL;
double temp = strtod(myattribute, &endptr);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: state_time can't be negative.\n");
error = 1;
break;
}
*state_time = temp;
}
else if (!strcmp(myflag, "-statistics_time"))
{
if (flag_statistics_time)
{
printf("Error, statistics_time has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_statistics_time = 1;
}
char* endptr = NULL;
double temp = strtod(myattribute, &endptr);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: statistics_time can't be negative.\n");
error = 1;
break;
}
*statistics_time = temp;
}
else if (!strcmp(myflag, "-number_of_buses"))
{
if (flag_total_buses)
{
printf("Error, total_buses has already been read from the configuration file.\n");
error = 1;
break;
}
else
{
flag_total_buses = 1;
}
int temp = atoi(myattribute);
if (temp < 0)
{
printf("Error, wrong value read from configuration file: number_of_buses can't be negative.\n");
error = 1;
break;
}
*total_buses = temp;
}
else
{
printf("The flag %s read from the configuration file is wrong. Exiting..\n", myflag);
error = 1;
break;
}
}
/* close the configuration file */
fclose(configfile);
return !error;
}
pid_t create_station_manager(key_t key)
{
pid_t station_manager_pid = fork();
if (station_manager_pid < 0)
{
perror("Fork for station manager failed");
}
else if (station_manager_pid == 0)
{
/* convert parameters to strings */
char* key_as_string = MallocOrDie(sizeof(char) * (MAX_BUFFER_SIZE + 1));
sprintf(key_as_string, "%d", key);
/* create vector of parameters and execute */
char* args[] = {"./station-manager", "-s", key_as_string, NULL};
execvp(args[0], args);
perror("execvp() of station-manager failed");
}
return station_manager_pid;
}
pid_t create_compotroller(double state_time, double statistics_time, key_t key)
{
pid_t comptroller_pid = fork();
if (comptroller_pid < 0)
{
perror("Fork for comptroller failed");
}
else if (comptroller_pid == 0)
{
/* convert parameters to strings */
char* time_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(time_as_string, "%lf", state_time);
char* stattime_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(stattime_as_string, "%lf", statistics_time);
char* key_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(key_as_string, "%d", key);
/* create vector of parameters and execute */
char* args[] = {"./comptroller", "-d", time_as_string, "-t", stattime_as_string, "-s", key_as_string, NULL};
execvp(args[0], args);
perror("execvp() of comptroller failed");
}
return comptroller_pid;
}
int create_buses(pid_t bus_pids[], uint32_t total_buses, uint16_t max_capacity, double max_park_period,
double max_man_time, key_t key)
{
srand(time(NULL));
int i = 0;
for (; i < total_buses; i++)
{
bus_pids[i] = fork();
if (bus_pids[i] < 0)
{
perror("Fork for bus failed");
printf("i = %d\n", i);
int j = 0;
for (j = 0; j < i; j++)
{
kill(bus_pids[j], SIGKILL);
}
return TRUE;
}
else if (bus_pids[i] == 0)
{
/* calculate random parameters for the bus */
char* type[4] = {"ASK", "PEL", "VOR", NULL};
uint8_t random_type = rand() % 3;
uint16_t random_capacity = rand() % (max_capacity / 2) + (max_capacity / 2);
uint16_t random_incpassengers = rand() % (random_capacity + 1);
double random_parkperiod = (((double) rand()) / RAND_MAX) * max_park_period;
double random_mantime = (((double) rand()) / RAND_MAX) * max_man_time;
/* convert parameters to strings */
char* capacity_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(capacity_as_string, "%d", random_capacity);
char* incpassengers_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(incpassengers_as_string, "%d", random_incpassengers);
char* parkperiod_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(parkperiod_as_string, "%lf", random_parkperiod);
char* mantime_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(mantime_as_string, "%lf", random_mantime);
char* id_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(id_as_string, "%d", i);
char* key_as_string = MallocOrDie(sizeof(char) * MAX_BUFFER_SIZE);
sprintf(key_as_string, "%d", key);
/* create vector of parameters and execute */
char* args[] = {"./bus", "-t", type[random_type], "-n", incpassengers_as_string,
"-c", capacity_as_string, "-p", parkperiod_as_string,
"-m", mantime_as_string, "-i", id_as_string, "-s", key_as_string, NULL};
execvp(args[0], args);
perror("execvp() of a bus failed");
printf("i = %d\n", i);
}
/* random sleep time in between the creation of buses */
uint8_t random_sleep_time = rand() % (MAX_SLEEP_TIME + 1);
sleep(random_sleep_time);
}
return FALSE;
}
int wait_child_processes(pid_t station_manager_pid, pid_t comptroller_pid, pid_t bus_pids[], uint32_t total_buses)
{
int returnStatus = 0;
pid_t wait_pid;
while ((wait_pid = wait(&returnStatus)) > 0)
{
if (!WIFEXITED(returnStatus) || (WIFEXITED(returnStatus) && WEXITSTATUS(returnStatus) != EXIT_SUCCESS))
{
/* an error has occured, so kill all processes in order to exit normally */
printf("\nChild process bus with pid %d terminated with an error. Exiting..\n", wait_pid);
int j = 0;
/* kill the buses */
for (; j < total_buses; j++)
{
/* kill the bus process */
kill(bus_pids[j], SIGKILL);
/* harvest the zombie process */
waitpid(bus_pids[j], &returnStatus, 0);
}
/* kill the comptroller process */
kill(comptroller_pid, SIGKILL);
/* harvest the zombie */
waitpid(comptroller_pid, &returnStatus, 0);
/* kill the station manager process */
kill(station_manager_pid, SIGKILL);
/* harvest the zombie */
waitpid(station_manager_pid, &returnStatus, 0);
return TRUE;
}
}
return FALSE;
}