-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal.c
46 lines (42 loc) · 920 Bytes
/
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
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sched.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
int wait_flag=1;
void stop(){
}
void stop2(){
wait_flag = 0;
}
int main(){
pid_t pid1,pid2;
signal(SIGINT,stop); /*键盘中断*/
pid1=fork(); /*返回*/
if(pid1>0){
pid2=fork();
if(pid2>0){
sleep(10);
kill(pid1,20);
wait(0);
kill(pid2,12);
wait(0);
printf("\nParent process is killed!\n");
exit(0);
}else{
signal(12,stop2);
while(wait_flag)
;
printf("Child Processl2 is killed by parent!\n");
exit(0);
}
}else if(pid1==0){
signal(20,stop2);
while(wait_flag)
;
printf("Child Processl1 is killed by parent!\n");
exit(0);
}
}