-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_proc_signal.c
More file actions
69 lines (65 loc) · 1.92 KB
/
Copy pathmulti_proc_signal.c
File metadata and controls
69 lines (65 loc) · 1.92 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/wait.h>
int conc_amnt = 0;
void pa_handle_sigchld(int signum){
//if (signum == SIGCHLD){
pid_t pid;
int status;
printf("[pid](%d): SIGCHLD is handled in pa_handle_sigchld()\n", getpid());
while ((pid = waitpid(-1, &status, WNOHANG)) > 0){
if (!WIFSIGNALED(status)){
printf("[pid](%d): [cpid](%d) is waited in pa_handle_sigchld()\n", getpid(), pid);
conc_amnt --;
}
}
//}
}
void pa_handle_sigint(int signum){
if (signum == SIGINT){
printf("[pid](%d): SIGINT is handled in pa_handle_sigint()\n", getpid());
printf("[pid](%d): [func](exit) with [conc_amnt](%d)\n", getpid(), conc_amnt);
exit(1);
}
}
int main (int argc, char* argv[]){
conc_amnt = atoi(argv[2]);
printf("[pid](%d): start\n", getpid());
pid_t children[conc_amnt-1];
struct sigaction act1, act2;
memset(&act1, 0, sizeof(struct sigaction));
memset(&act2, 0, sizeof(struct sigaction));
sigemptyset(&act1.sa_mask);
sigemptyset(&act2.sa_mask);
act1.sa_handler = pa_handle_sigint;
act2.sa_handler = pa_handle_sigchld;
act1.sa_flags = 0;
act2.sa_flags = 0;
sigaction(SIGINT, &act1, NULL);
sigaction(SIGCHLD, &act2, NULL);
//derive child process
int i;
for (i = 0; i < conc_amnt-1; i ++){
if ((children[i] = fork()) == 0) break;
}
if (children[i] == 0){
printf("[pid](%d): start\n", getpid());
pause();
} else {
sleep(1);
for (int j = 0; j < conc_amnt-1; j ++){
kill(children[j], 2);
}
while (conc_amnt > 1){
printf("[pid](%d): [func](pause) with [conc_amnt](%d)\n", getpid(), conc_amnt);
pause();
}
pause();
}
return 0;
}