防止僵尸进程的三种方法

僵尸进程:

 

第一种,在父进程中通过调用waitpid;

第二种,在父进程中将子进程结束时产生SIGCHLD信号忽略;

第三种,在子进程中再次创建孙子进程,然后子进程退出,孙进程被init进程收养,它退出后,init进程将其回收,但子进程还得自己回收。

 

第一种

   1:  /*
   2:   * =====================================================================================
   3:   *
   4:   *       Filename:  p15.c
   5:   *
   6:   *    Description:  
   7:   *
   8:   *        Version:  1.0
   9:   *        Created:  03/16/2013 01:19:15 PM
  10:   *       Revision:  none
  11:   *       Compiler:  gcc
  12:   *
  13:   *         Author:  YOUR NAME (), 
  14:   *        Company:  
  15:   *
  16:   * =====================================================================================
  17:   */
  18:  #include <stdio.h>
  19:  #include <signal.h>
  20:  #include <unistd.h>
  21:  #include <stdlib.h>
  22:   
  23:  pid_t pid;
  24:   
  25:  void handler(int arg)
  26:  {
  27:     waitpid(pid, NULL, 0); 
  28:  }
  29:   
  30:   
  31:  int main(void)
  32:  {
  33:      signal(SIGCHLD, handler);
  34:   
  35:      if((pid = fork()) < 0)
  36:      {
  37:          perror("fail to fork");
  38:          exit(-1);
  39:      }
  40:   
  41:      if(pid == 0)
  42:      {
  43:          printf("child close\n");
  44:          exit(0);
  45:      }
  46:      else
  47:      {
  48:          while(1);
  49:      }
  50:      
  51:  }

 

第二种:

   1:  /*
   2:   * =====================================================================================
   3:   *
   4:   *       Filename:  p15.c
   5:   *
   6:   *    Description:  
   7:   *
   8:   *        Version:  1.0
   9:   *        Created:  03/16/2013 01:19:15 PM
  10:   *       Revision:  none
  11:   *       Compiler:  gcc
  12:   *
  13:   *         Author:  YOUR NAME (), 
  14:   *        Company:  
  15:   *
  16:   * =====================================================================================
  17:   */
  18:  #include <stdio.h>
  19:  #include <signal.h>
  20:  #include <unistd.h>
  21:  #include <stdlib.h>
  22:   
  23:  pid_t pid;
  24:   
  25:   
  26:  int main(void)
  27:  {
  28:      signal(SIGCHLD, SIG_IGN);
  29:   
  30:      if((pid = fork()) < 0)
  31:      {
  32:          perror("fail to fork");
  33:          exit(-1);
  34:      }
  35:   
  36:      if(pid == 0)
  37:      {
  38:          printf("child close\n");
  39:          exit(0);
  40:      }
  41:      else
  42:      {
  43:          while(1);
  44:      }
  45:      
  46:  }

 

第三种

   1:  /*
   2:   * =====================================================================================
   3:   *
   4:   *       Filename:  p15.c
   5:   *
   6:   *    Description:  
   7:   *
   8:   *        Version:  1.0
   9:   *        Created:  03/16/2013 01:19:15 PM
  10:   *       Revision:  none
  11:   *       Compiler:  gcc
  12:   *
  13:   *         Author:  YOUR NAME (), 
  14:   *        Company:  
  15:   *
  16:   * =====================================================================================
  17:   */
  18:  #include <stdio.h>
  19:  #include <signal.h>
  20:  #include <unistd.h>
  21:  #include <stdlib.h>
  22:   
  23:  pid_t pid;
  24:   
  25:   
  26:  int main(void)
  27:  {
  28:      signal(SIGCHLD, SIG_IGN);
  29:   
  30:      if((pid = fork()) < 0)
  31:      {
  32:          perror("fail to fork");
  33:          exit(-1);
  34:      }
  35:   
  36:      if(pid == 0)
  37:      {
  38:         if(fork() == 0)
  39:         {
  40:             printf("grandchild closed");
  41:             exit(0);
  42:         }
  43:          printf("child close\n");
  44:          exit(0);
  45:      }
  46:      else
  47:      {
  48:          while(1);
  49:      }
  50:      
  51:  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值