1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<sys/types.h>
5 #include<sys/wait.h>
6 #include<signal.h>
7
8 void catchSig(int sig)
9 {
10 do{
11
12 pid_t ret = waitpid(-1,NULL,WNOHANG);
13 if(ret > 0){
14 printf("wait success: %d\n",ret);
15 }else{
16 printf("wait failed!\n");
17 break;
18 }
19 }while(1);
20 }
21
22
23
24 int main()
25 {
26 signal(SIGCHLD,catchSig);
27 pid_t id = fork();
28 if(id == 0){//child
29 printf("i am child,quit!,pid:%d\n",getpid());
30 sleep(4);
31 exit(1);
32 }else{//father
33 while(1){
34 printf("do father thing!\n");
35 sleep(1);
36 }
37 }
38 return 0;
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<sys/types.h>
5 #include<sys/wait.h>
6 #include<signal.h>
7
8 void catchSig(int sig)
9 {
10 do{
11
12 pid_t ret = waitpid(-1,NULL,WNOHANG);
13 if(ret > 0){
14 printf("wait success: %d\n",ret);
15 }else{
16 printf("wait failed!\n");
17 break;
18 }
19 }while(1);
20 }
21
22
23
24 int main()
25 {
26 signal(SIGCHLD,catchSig);
27 pid_t id = fork();
28 if(id == 0){//child
29 printf("i am child,quit!,pid:%d\n",getpid());
30 sleep(4);
31 exit(1);
32 }else{//father
33 while(1){
34 printf("do father thing!\n");
35 sleep(1);
36 }
37 }
38 return 0;
39 }
结果图