LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1153|回复: 6

两次fork之后产生孙子进程mplayer,用SIGKILL杀死后为什么还残留zombie进程?

[复制链接]
发表于 2006-5-9 20:59:28 | 显示全部楼层 |阅读模式
程序如下:
[php]
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int
main ()
{
  int fds[2];
  pid_t pid;
  pipe (fds);
  pid = fork ();
  if(pid==0)
  {
     pid=fork();
     if(pid==0)
     {
         printf("chld pid=%d\n",getpid());                                             
         execlp("mplayer","mplayer","http://localhost:9000",0);
     }
     else
     {
         close(fds[0]);
         dup2(fds[1],1);
         printf("%d ",pid);
     }
  }
  else
  {
      close(fds[1]);
      char aa[20];
      read(fds[0],aa,15);
      sleep(4);
      kill(atoi(aa),SIGKILL);
  }
}
[/php]
gcc编译成a.out,
首先运行
sp-sc sop://broker.sopcast.org:3919/official/shanghaiGsports 8000 9000
然后运行
./a.out
4秒后mplayer退出,用ps -aux查看进程,发现mplayer还在。按理说mplayer进程是由init管理的,它的僵尸进程应该由init自动处理的。
发表于 2006-5-9 22:41:45 | 显示全部楼层
你用pstree看看
一个进程的父进程如果不死的话,这个进程就算成了zombie也不会被init回收的,如果我记得没错的话
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-9 23:58:41 | 显示全部楼层
mplayer的父进程只有一段:
          else
          {
                  close(fds[0]);
                  dup2(fds[1],1);
                  printf("%d ",pid);
          }
应该立刻就结束了
回复 支持 反对

使用道具 举报

发表于 2006-5-10 00:44:36 | 显示全部楼层
对啊
我这里是正常的
我试验了一下,mplayer的父进程会退出,mplayer成为init的子进程
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-10 09:30:10 | 显示全部楼层
ps -aux当mplayer运行时显示有两个mplayer的进程,mplayer被SIGKILL后,还有一个进程。
回复 支持 反对

使用道具 举报

发表于 2006-5-10 09:33:40 | 显示全部楼层
比较奇怪
怎么会有两个呢
pstree了么
那个残留的mplayer的父进程是谁
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-10 09:37:57 | 显示全部楼层
结束后pstree显示:
     |-mplayer
     |-nmbd
     |-smbd---smbd
     `-udevd
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表