Where do Zombie processes go after their parent dies?

爷,独闯天下 提交于 2019-12-07 06:09:05

问题


A Zombie process is a process that has completed execution, but still has an entry in the process table (the parent hasn't read its exit code, or in other words, it hasn't been "reaped").

An Orphan process is a process whose parent has finished, though it remains running itself (its parent has "passed away" but it is still "alive"). in this case, init will adopt it and will wait for it.

So consider this:

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;
}

The child process being created here will be a zombie for 2 seconds, but what will be its status when the parent finishes? Orphan-zombie?

What happens to its entry in the process table?

Are "orphan-zombies" (such as the above) also adopted by init and being reaped by it?


回答1:


According to 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.

When the parent process finishes, the child process (even if it's a zombie process) will be adopted by init. Then, as you said, init will wait() for its exit status.

So, I don't think "orphan zombie" to be any special case.



来源:https://stackoverflow.com/questions/24346126/where-do-zombie-processes-go-after-their-parent-dies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!