-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj2.c
74 lines (60 loc) · 1.67 KB
/
proj2.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
/*
*IOS proj2
*author: Patrik Korytar, xkoryt04
*school: FIT VUT
*date: 24.04.2021
*module description: main program
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include "resources.h"
#include "params.h"
#include "error.h"
// SIGTERM signal handler
void term_func(int pid);
int main(int argc, char **argv)
{
// load parameters in params_t structure
if(load_params(¶ms, argc, argv) == RET_ERR)
goto end;
// get resources - file, shared mem, elf and rd PID private shared mem, semaphores
if(init_resources() == RET_ERR)
goto end;
// change SIGTERM signal handler - used to kill subprocesses - need to close file
signal(SIGTERM, term_func);
// create santa, elf and rd processes
if(create_santa() == RET_ERR)
goto del_sems;
if(create_elves() == RET_ERR)
goto kill_elves;
if(create_rds() == RET_ERR)
goto kill_rds;
while(wait(NULL) > 0); // wait for subprocesses to end
goto del_sems; // subprocesses ended, skip killing them
// free resources
kill_rds: kill_processes(rd_pids, params.rd_cnt);
kill_elves: kill_processes(elf_pids, params.elf_cnt);
kill(santa_pid, SIGTERM);
del_sems: destroy_sems();
munmap(rd_pids, sizeof(pid_t) * params.rd_cnt);
munmap(elf_pids, sizeof(pid_t) * params.elf_cnt);
munmap(shm_p, sizeof(shared_t));
fclose(file);
end:
if(err_var != ERR_NONE) // if error detected
{
print_error(); // print error message
return RET_ERR; // return 1
}
return RET_NOERR; // return 0
}
// SIGTERM signal handler
void term_func(int pid)
{
(void)pid;
fclose(file);
exit(1);
}