posts - 297,  comments - 15,  trackbacks - 0
一个进程在调用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] <defunct>
 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了!!!!!


《转自》http://blog.chinaunix.net/u2/76292/showart.php?id=2064840

posted on 2009-10-06 23:42 chatler 阅读(534) 评论(0)  编辑 收藏 引用 所属分类: Linux_Coding

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜