26

As I understand it, a zombie process has died but still exists as a placeholder in the process table until its parent (or init if the zombie is itself an orphan) checks its exit status.

And my understanding of orphan processes is they are processes that are still alive and running but whose parent has died.

Since a zombie is already dead, its children would be considered orphans, wouldn't they? Would they be affected be reaping the zombie? Specifically, would init adopt them as its children only once the zombie was reaped, or would they be adopted as soon as the parent became a zombie?

5
  • It makes sense for child processes to get reparented to init as soon as their parent dies (and that includes its becoming a zombie). A zombie won't take care of its children. It won't do anything. It's just a death status information floating around in a pid spot. Dec 11, 2015 at 20:13
  • Does that mean that a zombie's orphans can't become zombies, because they are immediately cared for by init?
    – Wildcard
    Dec 11, 2015 at 20:14
  • Correct. Or if they do become zombies, it'll be only for a minuscule period of time until init reaps them. Dec 11, 2015 at 20:20
  • 6
    I can't wait until this question's title shows up in the "Hot Network Questions" section... Dec 12, 2015 at 23:39
  • @NathanOsman, that would have been wonderful but it looks like a no-go. :(
    – Wildcard
    Dec 18, 2015 at 20:33

2 Answers 2

15

As I understand it, a zombie process has died but still exists as a placeholder in the process table until its parent (or init if the zombie is itself an orphan) checks its exit status.

Correct.

And my understanding of orphan processes is they are processes that are still alive and running but whose parent has died.

Correct.

Since a zombie is already dead, its children would be considered orphans, wouldn't they?

Yes. When the parent dies, it's dead. With respect to its children, it doesn't matter whether the parent stays on as a zombie: the children become orphans at the time the parent dies, and then they lose any connection with their parent.

Would they be affected be reaping the zombie? Specifically, would init adopt them as its children only once the zombie was reaped, or would they be adopted as soon as the parent became a zombie?

No, and the latter, as per above.

7

If experimental results will do, it appears at least systemd init will reap the zombie's orphans as soon as it can:

foo.c:

#include <unistd.h>

int main(void)
{
    pid_t child = fork();
    if (child < 0)
        return -1;
    if (child == 0)
    {
        pid_t grand_child = fork();
        if (grand_child < 0)
            return -1;
        if (grand_child == 0)
            sleep (1000);
        else
            return 0;
    }
    else
        sleep (1000);
    return 0;
}

In one terminal:

$ gcc -o foo foo.c
$ ./foo

In another terminal:

$ pgrep foo                         
25548
25549
25550
$ pstree -pa 25548
foo,25548
  └─(foo,25549)
$ pstree -psa 25550
systemd,1
  └─foo,25550
$ ps -o stat,pid -p $(pgrep -d, foo)
STAT   PID
S+   25548
Z+   25549
S+   25550
6
  • And well it should, that's its job. Dec 12, 2015 at 0:14
  • @Gilles sure, collecting orphans is init's job, but is there some standard which specifies when it should be collected? Otherwise I'd imagine the implementation could have some leeway.
    – muru
    Dec 12, 2015 at 0:22
  • I don't think there's a written standard for that. POSIX only states that “The parent process ID of all of the existing child processes and zombie processes of the calling process shall be set to the process ID of an implementation-defined system process.”, it doesn't say what that system process is supposed to do, or even how that system process is informed. Under Unix systems, that system process is PID 1, it receives SIGCLD, and it reaps processes as soon as it gets around to it. Dec 12, 2015 at 0:38
  • @Gilles meaning there could be some (probably kinemortophobic) init somewhere that decides to defer reaping a zombie's kids until the zombie goes away?
    – muru
    Dec 12, 2015 at 0:40
  • You might also want to remind Gilles of unix.stackexchange.com/a/177361/5132 . It's almost 4 years, now. ☺
    – JdeBP
    Dec 12, 2015 at 2:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .