Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4745065
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-10-06 15:58:30

   一个进程在调用exit命令结束自己的生命的时候,其实它并没有真正的被销毁,而是留下一个称为僵尸进程(Zombie)的数据结构(系统调用exit,它的作用是使进程退出,但也仅仅限于将一个正常的进程变成一个僵尸进程,并不能将其完全销毁)。在Linux进程的状态中,僵尸进程是非常特殊的一种,它已经放弃了几乎所有内存空间,没有任何可执行代码,也不能被调度,仅仅在进程列表中保留一个位置,记载该进程的退出状态等信息供其他进程收集,除此之外,僵尸进程不再占有任何内存空间。它需要它的父进程来为它收尸,如果他的父进程没安装SIGCHLD信号处理函数调用wait或waitpid()等待子进程结束,又没有显式忽略该信号,那么它就一直保持僵尸状态,如果这时父进程结束了,那么init进程自动
会接手这个子进程,为它收尸,它还是能被清除的。但是如果如果父进程是一个循环,不会结束,那么子进程就会一直保持僵尸状态,这就是为什么系统中有时会有很多的僵尸进程。
怎么查看僵尸进程:
利用命令ps,可以看到有标记为Z的进程就是僵尸进程。
 
   先看段代码

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv)
{
  int num = 6;
  pid_t pid;
 
  if((pid=fork())<0)
    {
     printf("fork error\n");
     return -1;
    }
  else if(pid==0)
   {
        num += 2;
     printf("this is child %d,parent %d, num is %d\n", getpid(), getppid(), num);
     exit(0);
   }
 

 sleep(1);
  printf("this is %d,parent %d, num is %d\n", getpid(), getppid(), num);
 system("ps -o pid,ppid,state,tty,command");
 return 0;
}

输出

this is child 3647,parent 3646, num is 8
  PID  PPID S TT       COMMAND
 3077  3028 S pts/2    bash
 3646  3077 S pts/2    ./zombie
 3647  3646 Z pts/2    [zombie]
 3649  3646 R pts/2    ps -o pid,ppid,state,tty,command
this is 3646,parent 3077, num is 6

看到了,这就是个zombie

 

怎样来避免僵尸进程:
1.改写父进程,在子进程死后要为它收尸。具体做法是接管SIGCHLD信号。子进程死后,会发送SIGCHLD信号给父进程,父进程收到此信号后,执行waitpid()函数为子进程收尸。这是基于这样的原理:就算父进程没有调用wait,内核也会向它发送SIGCHLD消息,尽管对的默认处理是忽略,如果想响应这个消息,可以设置一个处理函数。
2.把父进程杀掉。父进程死后,僵尸进程成为"孤儿进程",过继给1号进程init,init始终会负责清理僵尸进程.它产生的所有僵尸进程也跟着消失。


 

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv)
{
  int num = 6;
  pid_t pid;
 
  if((pid=fork())<0)
    {
     printf("fork error\n");
     return -1;
    }
  else if(pid==0)
   {
    #if 1
    if((pid=fork())<0)
     {
     printf("fork error\n");
     return -1;
     }
    else if(pid>0)
     exit(0);
     
     sleep(1);
    #endif
     num += 2;
     printf("this is child %d,parent %d, num is %d\n", getpid(), getppid(), num);
     exit(0);
   }
  #if 1
  if(waitpid(pid, NULL, 0)!=pid)
    {
      printf("waitpid error\n");
      return -1;
    }
  #endif
  sleep(1);
  printf("this is %d,parent %d, num is %d\n", getpid(), getppid(), num);
  
//while(1)

    
//;

  
 system("ps -o pid,ppid,state,tty,command");
 return 0;
}

输出

this is child 3629,parent 1, num is 8
  PID  PPID S TT       COMMAND
 3077  3028 S pts/2    bash
 3627  3077 S pts/2    ./zombie
 3630  3627 R pts/2    ps -o pid,ppid,state,tty,command
this is 3627,parent 3077, num is 6

 

waitpid为第一个子进程收死,避免第一个子进程为zombie,而孙进程则由交给init了,

this is child 3629,parent 1, num is 8
于是就没有state为Z的zombie了!!!!!

阅读(2002) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~