Where do Zombie processes go after their parent dies?
Asked Answered
B

1

8

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?

Buxton answered 21/6, 2014 at 21:44 Comment(3)
Checking this yourself in a simple "run this code and look at the process table" would seem simple enough. Are you looking for the reasoning? (Its status will simply be completed by the way, it will be a finished process)Theatricalize
How can I see the process table?Buxton
unixhelp.ed.ac.uk/CGI/man-cgi?psTheatricalize
C
15

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.

Cascarilla answered 21/6, 2014 at 22:6 Comment(4)
So to explicitly state this: There are no orphaned zombies, as becoming an orphan, init jumps in and takes over the role of the parent for the zombie, wait()s for its new child and lets it pass away, end.Weimer
But, there are zombies that can become orphans.Inexpensive
@SivaPrakash People invents the definitions, such as zombie, orphan, to describe what will happen and what the result is. So I think the word orphan zombie is useless. Orphan process is a running process, whose parent is init. Zombie process is a terminated process, whose parent hasn't waited for it. If the parent of a zombie process has gone, the zombie will be adopted by init and will be waited soon because init will periodically execute wait. That's it.Soutine
It's interesting that some sources claim that the zombie gets re-assigned to a sibling. In particular, "Linux Kernel Development (3rd Edition)" by Robert Love says it's reassigned to a process in the same thread group (at the end of Chapter 3), and only failing that it gets assigned to init. And it even shows source code that does the search. I think, either something changed or there's some misunderstanding, because man 2 wait clearly states the target should be either a "subreaper" process explicitly set via prctl() or the init.Mistrust

© 2022 - 2024 — McMap. All rights reserved.