-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomptroller.c
executable file
·152 lines (123 loc) · 3.71 KB
/
comptroller.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "shm_data.h"
void comptroller_snapshot(FILE* fp,shm_data* data);
void comptroller_stat(FILE* fp, shm_data* data);
int main(int argc, char const *argv[]){
int time;
int stat_time;
int shm_id;
int total_time;
shm_data* data;
FILE* fp;
while(--argc){
if(strcmp(*argv, "-s") == 0){
shm_id = atoi(*(argv + 1));
} else if(strcmp(*argv, "-d") == 0) {
time = atoi(*(argv + 1));
} else if(strcmp(*argv, "-t") == 0) {
stat_time = atoi(*(argv + 1));
}
argv++;
}
total_time = 0;
fp = fopen("log.txt", "w");
data = (shm_data*)shmat(shm_id, 0, 0);
// Fix the offsets for the auxiliary array bays.
intitiaze_bays(data);
fprintf(fp, "Bus status: M(Maneuver) P(Parked) F(Free)\nTime in mins\n");
while(data->buses_served < data->max_buses){
sleep(time);
fprintf(fp, "-------Station Snapshot-------\n");
sem_wait(&(data->mutex));
comptroller_snapshot(fp, data);
sem_post(&(data->mutex));
sleep(stat_time);
fprintf(fp, "-------Station Statistics-------\n");
sem_wait(&(data->mutex));
comptroller_stat(fp, data);
sem_post(&(data->mutex));
fprintf(fp, "\n");
}
fclose(fp);
shmdt(data);
return 0;
}
void comptroller_snapshot(FILE* fp,shm_data* data){
int parked_buses = 0;
for(int i=0;i<3;i++){
fprintf(fp, "Bay ");
switch (i) {
case 0:
fprintf(fp, "VOR\n");
break;
case 1:
fprintf(fp, "ASK\n");
break;
case 2:
fprintf(fp, "PEL\n");
break;
default:
break;
}
for(int j = 0;j<data->bays[i].max_num;j++){
fprintf(fp, "\tBus %d Status: %c ", j, bays[i].buses[j].status);
if(bays[i].buses[j].status == 'P'){
fprintf(fp, "Passengers: %d\n", bays[i].buses[j].passengers);
parked_buses++;
} else {
fprintf(fp, "\n");
}
}
fprintf(fp, "Free spots: ");
for(int j = 0;j<data->bays[i].max_num;j++){
if(bays[i].buses[j].status == 'F'){
fprintf(fp, "%d ", j);
}
}
fprintf(fp, "\n");
}
fprintf(fp, "Parked buses: %d\n", parked_buses);
}
void comptroller_stat(FILE* fp, shm_data* data){
int total_wait_time = 0;
int wait_time;
int parked_buses = 0;
fprintf(fp, "Wait time for.\n");
for(int i=0;i<3;i++){
fprintf(fp, "Bay ");
switch (i) {
case 0:
fprintf(fp, "VOR\n");
break;
case 1:
fprintf(fp, "ASK\n");
break;
case 2:
fprintf(fp, "PEL\n");
break;
default:
break;
}
for(int j = 0;j<data->bays[i].max_num;j++){
if(bays[i].buses[j].status == 'P'){
parked_buses++;
wait_time = bays[i].buses[j].park_time - bays[i].buses[j].arrival_time;
total_wait_time += wait_time;
fprintf(fp, "\tBus %d %dm\n", j, wait_time);
}
}
}
if(parked_buses != 0){
total_wait_time = total_wait_time/parked_buses;
} else {
total_wait_time = 0;
}
fprintf(fp, "Mean wait time: %dm\n", total_wait_time);
fprintf(fp, "Total buses served until now: %d\n", data->buses_served);
fprintf(fp, "Total passengers arrived: %d\n", data->total_people_in);
fprintf(fp, "Total passengers left: %d\n", data->total_people_out);
return;
}