-
Notifications
You must be signed in to change notification settings - Fork 0
/
parent.c
484 lines (350 loc) · 11.4 KB
/
parent.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
#include "includes/include.h"
#include "includes/std.h"
pid_t process_pid[2*PLAYERS_PER_TEAM];
unsigned player_energy[2*PLAYERS_PER_TEAM];
unsigned next_player; /* each process will have a different next player pid,
this is used to index the pids array and send the pid to the process when we use execlp. arg[3] */
int status, i;
int number_of_processes_waiting_for_read = 0;
//create structs
team team1;
team team2;
//init the two structs to 0 balls and 0 score
union sigval value;
// FUNCTION PROTOTYPES
void create_FIFOs();
void assign_initial_energy();
void fork_children();
void fork_gui();
void init_teams();
void read_parameters(int argc,char* argv[]);
void create_shared_mem();
void doOneRound();
int gui_pid = -1;
// GLOBAL VARIABLES
unsigned int round_duration;
unsigned int number_of_rounds;
int current_round_number = 0;
struct sigaction sa_chld, sa_io, sa_alarm, sa_usr1, sa_usr2;;
int fd_shm;
// Define a structure to hold the flag
struct shared_data {
int ignore_signals; // 0 means not ignore, 1 means ignore
};
struct shared_data *shared_mem;
// SIGNAL HANDLERS
void signal_handler(int signum) {
if (signum == SIGUSR1) {
team1.number_of_balls++;
team2.number_of_balls--;
// if team2 has no balls, send a signal to the team2 leader to get a ball.
if (team2.number_of_balls == 0) {
green_stdout();
printf("Team 2 has no balls, sending a signal to team2 leader to get a ball\n");
reset_stdout();
team2.number_of_balls++;
kill(process_pid[11], SIGUSR1);
#ifdef __GUI__
value.sival_int = -2;
sigqueue(gui_pid, SIGUSR1, value);
#endif
}
}
if (signum == SIGUSR2) {
team2.number_of_balls++;
team1.number_of_balls--;
// if team1 has no balls, send a signal to the team1 leader to get a ball.
if (team1.number_of_balls == 0) {
green_stdout();
printf("Team 1 has no balls, sending a signal to team1 leader to get a ball\n");
reset_stdout();
team1.number_of_balls++;
kill(process_pid[5], SIGUSR1);
#ifdef __GUI__
value.sival_int = -1;
sigqueue(gui_pid, SIGUSR1, value);
#endif
}
}
if (signum == SIGIO) {
printf("entrered SIGIO from parent\n");
}
fflush(stdout);
}
void alarm_handler(int signum) {
shared_mem->ignore_signals = 1;
my_pause(4);
if (team1.number_of_balls < team2.number_of_balls) {
team1.total_score++;
}
else if (team1.number_of_balls > team2.number_of_balls) {
team2.total_score++;
}
cyan_stdout();
printf("Team 1 score: %d, and number of balls: %d\n", team1.total_score, team1.number_of_balls);
printf("Team 2 score: %d, and number of balls: %d\n", team2.total_score, team2.number_of_balls);
yellow_stdout();
printf("Round %d finished\n", current_round_number);
reset_stdout();
my_pause(4);
if (current_round_number < number_of_rounds) {
//reset the gui
#ifdef __GUI__
value.sival_int = -1;
sigqueue(gui_pid, SIGUI, value);
#endif
doOneRound();
}
else {
// print the final scores and the winner
if (team1.total_score > team2.total_score) {
green_stdout();
printf("Team 1 wins\n");
reset_stdout();
}
else if (team1.total_score < team2.total_score) {
red_stdout();
printf("Team 2 wins\n");
reset_stdout();
}
else {
blue_stdout();
printf("It's a tie\n");
reset_stdout();
}
// remove the FIFOs
unlink(FIFO1);
unlink(FIFO2);
for (int i = 0; i < 2*PLAYERS_PER_TEAM; i++) {
kill(process_pid[i], SIGKILL);
}
#ifdef __GUI__
kill(gui_pid, SIGKILL);
#endif
exit(0);
}
}
int main(int argc, char *argv[]) {
#ifdef __GUI__
gui_pid = fork();
if (gui_pid == 0) {
execlp("./ballpass", "ballpass.o", (const char*)NULL);
}
else if (gui_pid == -1) {
perror("GUI failed.\n");
}
else {
sleep(1);
}
#endif
read_parameters(argc, argv);
create_shared_mem();
fork_children();
init_teams();
// Set up SIGCHLD handler
sa_chld.sa_handler = signal_handler;
sigemptyset(&sa_chld.sa_mask);
sa_chld.sa_flags = 0;
if (sigaction(SIGCHLD, &sa_chld, NULL) == -1) {
perror("sigaction for SIGCHLD");
exit(EXIT_FAILURE);
}
// Set up SIGIO handler
sa_io.sa_handler = signal_handler;
sigemptyset(&sa_io.sa_mask);
sa_io.sa_flags = 0;
if (sigaction(SIGIO, &sa_io, NULL) == -1) {
perror("sigaction for SIGIO");
exit(EXIT_FAILURE);
}
// add the alarm handler
sa_alarm.sa_handler = alarm_handler;
sigemptyset(&sa_alarm.sa_mask);
sa_alarm.sa_flags = 0;
if (sigaction(SIGALRM, &sa_alarm, NULL) == -1) {
perror("sigaction for SIGALRM");
exit(EXIT_FAILURE);
}
sa_usr1.sa_handler = signal_handler;
sigemptyset(&sa_usr1.sa_mask);
sa_usr1.sa_flags = 0;
if (sigaction(SIGUSR1, &sa_usr1, NULL) == -1) {
perror("sigaction for SIGUSR1");
exit(EXIT_FAILURE);
}
sa_usr2.sa_handler = signal_handler;
sigemptyset(&sa_usr2.sa_mask);
sa_usr2.sa_flags = 0;
if (sigaction(SIGUSR2, &sa_usr2, NULL) == -1) {
perror("sigaction for SIGUSR2");
exit(EXIT_FAILURE);
}
sleep(1);
union sigval value;
value.sival_int = process_pid[0];
sigqueue(process_pid[5], SIGBUS, value);
usleep(300);
value.sival_int = process_pid[6];
sigqueue(process_pid[11], SIGBUS, value);
usleep(300);
value.sival_int = process_pid[5];
sigqueue(process_pid[11], SIGBUS, value);
usleep(500);
doOneRound();
// wait for all children to finish
for (int i = 0; i < 2*PLAYERS_PER_TEAM; i++) {
waitpid(process_pid[i], &status, 0);
}
waitpid(gui_pid, &status, 0);
munmap(shared_mem, sizeof(struct shared_data));
close(fd_shm);
return 0;
}
/* FUNCTIONS */
void assign_initial_energy() {
for (int i = 0; i < 2*PLAYERS_PER_TEAM; i++) {
player_energy[i] = MIN_PLAYER_ENERGY + (rand() % (abs(MAX_PLAYER_ENERGY - MIN_PLAYER_ENERGY)));
}
}
void init_teams() {
team1.number_of_balls = 0;
team1.total_score = 0;
team2.number_of_balls = 0;
team2.total_score = 0;
}
void doOneRound() {
shared_mem->ignore_signals = 0;
// Start new round
magenta_stdout();
printf("Starting new round...\n");
reset_stdout();
team1.number_of_balls = 0;
team2.number_of_balls = 0;
current_round_number++;
alarm(round_duration); // set the alarm for the round duration.
// send a ball to team1 leader (send a signal to the team1 leader)
team1.number_of_balls++;
kill(process_pid[5], SIGUSR1);
usleep(300);
#ifdef __GUI__
value.sival_int = -1;
sigqueue(gui_pid, SIGUSR1, value);
#endif
// send a ball to team2 leader (send a signal to the team2 leader)
team2.number_of_balls++;
kill(process_pid[11], SIGUSR1);
usleep(400);
#ifdef __GUI__
value.sival_int = -2;
sigqueue(gui_pid, SIGUSR1, value);
#endif
}
void fork_children(){
pid_t team1_lead_pid;
pid_t team2_lead_pid;
pid_t next_player_pid;
char team1_leader_pid_arg[10]="-1";
char team2_leader_pid_arg[10]="-1";
char next_player_pid_arg[10]="-1";
char next_pid_arg[10] = "-1";
for (i = 2*PLAYERS_PER_TEAM-1; i >= 0; i--) {
//we will fork a child and execute the child program
int current_pid;
if ((current_pid = fork()) == -1) {
perror("Fork failed.\n");
exit(1);
}
char player_number_arg[4];
char energy_arg[20];
char next_player_arg[20];
char gui_pid_arg[20];
srand(time(NULL));
assign_initial_energy();
if (i == 5) {
team1_lead_pid = current_pid;
sprintf(team1_leader_pid_arg, "%d", team1_lead_pid);
}
else if (i == 11) {
team2_lead_pid = current_pid;
sprintf(team2_leader_pid_arg, "%d", team2_lead_pid);
}
if (current_pid == 0) {
sprintf(player_number_arg, "%d", i);
sprintf(energy_arg, "%d", player_energy[i]);
// this is unlikely to be incorrect, but feel free to triple check it, since it can cause serious damage.
next_player = (i < PLAYERS_PER_TEAM) ? ((i+1) % PLAYERS_PER_TEAM) : 6 + ((i+1) % PLAYERS_PER_TEAM);
sprintf(next_player_arg, "%d",next_player);
sprintf(next_player_pid_arg, "%d", process_pid[next_player]);
sprintf(gui_pid_arg, "%d", gui_pid);
execlp("./child" ,"child.o",player_number_arg, energy_arg, next_player_arg, team1_leader_pid_arg, team2_leader_pid_arg,next_player_pid_arg, gui_pid_arg ,(const char*)NULL);
perror("execvp failed.\n");
// To kill all child processes (from stack-overflow), might need to be tested.
/*
One way to accomplish this is to deliver some signal that can be caught (not
SIGKILL). Then, install a signal handler that detects if the current process is
the parent process or not, and calls _exit() if it is not the parent process.(ibid, et al).
*/
signal(SIGQUIT, SIG_IGN);
kill(0, SIGQUIT);
exit(2);
} else {
process_pid[i] = current_pid;
}
}
}
void create_shared_mem() {
// Create a shared memory segment
fd_shm = shm_open("/my_shared_memory", O_CREAT | O_RDWR, 0666);
if (fd_shm == -1) {
red_stderr();
perror("shm_open");
reset_stderr();
exit(EXIT_FAILURE);
}
// Set the size of the shared memory segment
if (ftruncate(fd_shm, sizeof(struct shared_data)) == -1) {
red_stderr();
perror("ftruncate");
reset_stderr();
exit(EXIT_FAILURE);
}
// Map the shared memory segment into the address space
shared_mem = mmap(NULL, sizeof(struct shared_data), PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm, 0);
if (shared_mem == MAP_FAILED) {
red_stderr();
perror("mmap");
reset_stderr();
exit(EXIT_FAILURE);
}
close(fd_shm);
}
void read_parameters(int argc,char* argv[]) {
FILE *file;
char line[100]; // Assuming the line won't exceed 100 characters
// Open the file for reading
file = fopen("settings.txt", "r");
if (file == NULL) {
red_stderr();
perror("Error opening file");
reset_stderr();
return ;
}
// Read the line from the file
fgets(line, sizeof(line), file);
// Close the file
fclose(file);
// Use sscanf to extract the numbers
sscanf(line, "round_duration=%d, number_of_rounds=%d", &round_duration, &number_of_rounds);
}
void fork_gui(){
pid_t gui_pid;
if ((gui_pid = fork()) == -1) {
perror("Fork failed.\n");
exit(1);
}
if (gui_pid == 0) {
execlp("./ballPass", "ballPass.o", (const char*)NULL);
perror("execvp failed.\n");
exit(2);
}
}