调用 fork() 两次以避免僵死进程

当我们只fork()一次后,存在父进程和子进程。这时有两种方法来避免产生僵尸进程:

  • 父进程调用waitpid()等函数来接收子进程退出状态。
  • 父进程先结束,子进程则自动托管到Init进程(pid = 1)。

      目前先考虑子进程先于父进程结束的情况:     

  • 若父进程未处理子进程退出状态,在父进程退出前,子进程一直处于僵尸进程状态。
  • 若父进程调用waitpid()(这里使用阻塞调用确保子进程先于父进程结束)来等待子进程结束,将会使父进程在调用waitpid()后进入睡眠状态,只有子进程结束父进程的waitpid()才会返回。 如果存在子进程结束,但父进程还未执行到waitpid()的情况,那么这段时期子进程也将处于僵尸进程状态。

      由此,可以看出父进程与子进程有父子关系,除非保证父进程先于子进程结束或者保证父进程在子进程结束前执行waitpid(),子进程均有机会成为僵尸进程。那么如何使父进程更方便地创建不会成为僵尸进程的子进程呢?这就要用两次fork()了。

      父进程一次fork()后产生一个子进程随后立即执行waitpid(子进程pid, NULL, 0)来等待子进程结束,然后子进程fork()后产生孙子进程随后立即exit(0)。这样子进程顺利终止(父进程仅仅给子进程收尸,并不需要子进程的返回值),然后父进程继续执行。这时的孙子进程由于失去了它的父进程(即是父进程的子进程),将被转交给Init进程托管。于是父进程与孙子进程无继承关系了,它们的父进程均为Init,Init进程在其子进程结束时会自动收尸,这样也就不会产生僵尸进程了。


  1. #include <stdio.h>    
  2. #include <sys/wait.h>    
  3. #include <sys/types.h>    
  4. #include <unistd.h>    
  5.     
  6. int main(void)       
  7. {       
  8.    pid_t pid;       
  9.       
  10.     if ((pid = fork()) < 0)       
  11.     {       
  12.         fprintf(stderr,"Fork error!/n");       
  13.         exit(-1);       
  14.     }       
  15.     else if (pid == 0) /* first child */      
  16.     {        
  17.         if ((pid = fork()) < 0)       
  18.         {        
  19.             fprintf(stderr,"Fork error!/n");       
  20.             exit(-1);       
  21.         }       
  22.         else if (pid > 0)       
  23.             exit(0); /* parent from second fork == first child */      
  24.         /*    
  25.          * We're the second child; our parent becomes init as soon    
  26.          * as our real parent calls exit() in the statement above.    
  27.          * Here's where we'd continue executing, knowing that when    
  28.          * we're done, init will reap our status.    
  29.          */      
  30.         sleep(2);       
  31.         printf("Second child, parent pid = %d/n", getppid());       
  32.         exit(0);       
  33.     }       
  34.            
  35.     if (waitpid(pid, NULL, 0) != pid) /* wait for first child */      
  36.     {       
  37.         fprintf(stderr,"Waitpid error!/n");       
  38.         exit(-1);       
  39.     }       
  40.       
  41.     /*    
  42.      * We're the parent (the original process); we continue executing,    
  43.      * knowing that we're not the parent of the second child.    
  44.      */      
  45.     exit(0);       
  46. }       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值