-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal.c
68 lines (58 loc) · 1.07 KB
/
Signal.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
int waitingFlag = 1;
void Stop1()
{
printf("Interruption\n");
}
void Stop2()
{
waitingFlag = 0;
printf("Child Interruption\n");
}
void Solve()
{
pid_t pid1, pid2;
signal(SIGINT, Stop1);
sleep(1);
pid1 = fork();
if (pid1 > 0)
{
pid2 = fork();
if (pid2 > 0)
{
sleep(5);
kill(pid1, SIGUSR1);
wait(0);
kill(pid2, SIGUSR2);
wait(0);
printf("Parent process is killed\n");
//exit(0);
}
else
{
signal(SIGUSR2, Stop2);
signal(SIGINT, SIG_IGN);
while (waitingFlag)
;
printf("Child process 2 is killed\n");
}
}
else
{
signal(SIGUSR1, Stop2);
signal(SIGINT, SIG_IGN);
while (waitingFlag)
;
printf("Child process 1 is killed\n");
}
}
int main()
{
Solve();
return 0;
}