父进程等待子进程的异步版

  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;

 39 }

结果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值