/
trafficmgr.c
185 lines (156 loc) · 4.25 KB
/
trafficmgr.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
#include "trafficmgr.h"
/*
* Deletes every direction queue and exits the program.
*/
void cleanexit() {
q_delete('n');
q_delete('s');
q_delete('e');
q_delete('w');
q_shutdown;
exit(EXIT_FAILURE);
}
/*
* Called on invalid command line args.
* Prints correct usage and cleanexits the program.
*/
void argerror() {
printf("Correct Usage: ./trafficmgr <d1d2d3d4...>\n Where dn E [n,s,e,w] is the direction of the nth cart.\n");
cleanexit();
}
/*
* Checks if the given string matches the defined synax for command line args
* arg - string to check
*/
int check_match(char *arg, char *reg) {
regex_t regex;
int reti;
char msgbuf[100];
/* Compile regular expression */
reti = regcomp(®ex, reg, 0);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
/* Execute regular expression */
reti = regexec(®ex, arg, 0, NULL, 0);
if (!reti) {
/* match found */
regfree(®ex);
return 1;
} else if (reti == REG_NOMATCH) {
/* no match found */
printf("Expression not matched: %s\n", arg);
regfree(®ex);
return -1;
} else {
/* expression match failed */
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
regfree(®ex);
exit(EXIT_FAILURE);
}
}
void init(char* arg) {
int i;
q_init;
gl_env.n = 0;
gl_env.s = 0;
gl_env.e = 0;
gl_env.w = 0;
for(i = 0; arg[i] != '\0'; i++){
switch (arg[i]) {
case 'n':
gl_env.n = 1;
q_putCart('n');
break;
case 's':
gl_env.s = 1;
q_putCart('s');
break;
case 'e':
gl_env.e = 1;
q_putCart('e');
break;
case 'w':
gl_env.w = 1;
q_putCart('w');
break;
}
}
}
/*
* Function that each direction thread runs.
* Gets the next cart from that direction queue if possible and passes it to the monitor.
*/
void *cart(void* args) {
arg_t* actual;
char direction;
actual = (arg_t*) args;
direction = actual->direction;
struct cart_t *cart;
fprintf(stderr, "\n** Direction %c thread starts **\n", direction);
cart = q_getCart(direction);
while (cart != NULL) {
fprintf(stderr, "\nDirection %c thread gets cart %i\n", direction, cart->num);
monitor_arrive(cart);
monitor_cross(cart);
monitor_leave(cart);
cart = q_getCart(direction);
}
fprintf(stderr, "\n** Direction %c thread exits **\n", direction);
}
int main(int argc, char** argv) {
int rc;
arg_t n,s,e,w;
pthread_t north, south, east, west;
if (argc == 2 && check_match(argv[1],"^[nsew]*$") > 0) {
init(argv[1]);
monitor_init();
if (gl_env.n) {
n.direction = 'n';
rc = pthread_create(&north, NULL, cart, (void*)&n);
if(rc){
perror("Error creating North thread");
cleanexit();
}
}
if (gl_env.s) {
s.direction = 's';
rc = pthread_create(&south, NULL, cart, (void*)&s);
if (rc) {
perror("Error creating South thread");
cleanexit();
}
}
if (gl_env.e) {
e.direction = 'e';
rc = pthread_create(&east, NULL, cart, (void*)&e);
if(rc){
perror("Error creating East thread");
cleanexit();
}
}
if (gl_env.w) {
w.direction = 'w';
rc = pthread_create(&west, NULL, cart, (void*)&w);
if (rc) {
perror("Error creating West thread");
cleanexit();
}
}
if(gl_env.n)
pthread_join(north, NULL);
if(gl_env.s)
pthread_join(south, NULL);
if(gl_env.e)
pthread_join(east, NULL);
if(gl_env.w)
pthread_join(west, NULL);
monitor_shutdown();
} else {
/* invalid arguements entered */
argerror();
}
cleanexit();
}