简体   繁体   English

使用kill函数创建Zombie进程

[英]Creating A Zombie Process Using the kill Function

I'm trying to create a zombie process with the kill function but it simply kills the child and returns 0 . 我正在尝试使用kill函数创建一个僵尸进程,但它只是杀死了孩子并返回0

int main ()
{
  pid_t child_pid;

  child_pid = fork ();

  if (child_pid > 0) {
    kill(getpid(),SIGKILL);
  }
  else {
    exit (0);
  }

  return 0;
}

When I check the status of the process there is no z in the status column. 当我检查进程的状态时,状态列中没有z

Here is a simple recipe which should create a zombie: 这是一个简单的食谱,应该创建一个僵尸:

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

int main()
{
    int pid = fork();
    if(pid == 0) {
        /* child */
        while(1) pause();
    } else {
        /* parent */
        sleep(1);
        kill(pid, SIGKILL);
        printf("pid %d should be a zombie\n", pid);
        while(1) pause();
    }
}

The key is that the parent -- ie this program -- keeps running but does not do a wait() on the dying child. 关键是父母 - 即这个程序 - 继续运行但不对垂死的孩子进行wait()

Zombies are dead children that have not been waited for. 僵尸是没有等待的死孩子。 If this program waited for its dead child, it would go away and not be a zombie. 如果这个程序等待它死去的孩子,它就会消失而不是僵尸。 If this program exited, the zombie child would be inherited by somebody else (probably init ), which would probably do the wait, and the child would go away and not be a zombie. 如果这个程序退出,僵尸孩子将被其他人(可能是init )继承,这可能会等待,孩子会离开而不是僵尸。

As far as I know, the whole reason for zombies is that the dead child exited with an exit status, which somebody might want. 据我所知,僵尸的全部原因是死亡的孩子退出了退出状态,有人可能会想要。 But where Unix stores the exit status is in the empty husk of the dead process, and how you fetch a dead child's exit status is by waiting for it. 但是,在Unix存储退出状态的地方是死亡进程的空壳,以及如何获取死亡孩子的退出状态是等待它。 So Unix is keeping the zombie around just to keep its exit status around just in case the parent wants it but hasn't gotten around to calling wait yet. 所以Unix正在保持僵尸只是为了保持其退出状态,以防父母想要它但尚未调用wait

So it's actually kind of poetic: Unix's philosophy here is basically that no child's death should go unnoticed. 所以它实际上有点诗意:Unix的哲学基本上就是不应该忽视孩子的死亡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM