系统相关
首页 > 系统相关> > 僵尸进程在他们的父母去世后去了哪里?

僵尸进程在他们的父母去世后去了哪里?

作者:互联网

Zombie进程是一个已完成执行的进程,但在进程表中仍有一个条目(父进程没有读取其退出代码,换句话说,它还没有被“收获”).

孤立进程是一个父进程已经完成的进程,虽然它仍在运行(它的父进程已“消失”,但它仍处于“活着状态”).在这种情况下,init将采用它并等待它.

所以考虑一下:

int main(int argv, char *argc[]) {

    pid_t p=fork();

    if (p<0) {
        perror("fork");
    }

    // child
    if (p==0) {
        exit(2);
    }

    // parent sleeps for 2 seconds
    sleep(2);
    return 1;
}

在这里创建的子进程将是一个2秒的僵尸,但是当父进程完成时它的状态是什么?孤儿的僵尸?

它在流程表中的条目会发生什么变化?

“孤儿僵尸”(如上所述)是否也被init采用并被它收获?

解决方法:

根据man 2 wait

A child that terminates, but has not been waited for becomes a
“zombie”. The kernel maintains a minimal set of information about the
zombie process (PID, termination status, resource usage information)
in order to allow the parent to later perform a wait to obtain
information about the child. As long as a zombie is not removed from
the system via a wait, it will consume a slot in the kernel process
table, and if this table fills, it will not be possible to create
further processes. If a parent process terminates, then its “zombie”
children (if any) are adopted by init(8), which automatically performs
a wait to remove the zombies.

当父进程完成时,init将采用子进程(即使它是一个僵尸进程).然后,如你所说,init将等待()退出状态.

所以,我不认为“孤儿僵尸”是任何特例.

标签:c-3,linux,linux-kernel,wait,zombie-process
来源: https://codeday.me/bug/20190612/1225224.html