I have just had a lecture that sums reaping as:
Reaping
Performed by parent on terminated child (using wait or waitpid)
Parent is given exit status information
Kernel then deletes zombie child process
So I understand that reaping is done by calling wait
or waitpid
from the parent process after which the kernel deletes the zombie process. If this actually is the case, that reaping is done only when calling wait
or waitpid
, why do the child processes actually go away after returning in the entry function - I mean that indeed does seem as if the child processes have been reaped and thus no resources are wasted even though the parent process may not be waiting.
So is "reaping" only possible when calling wait
or waitpid
? If processes are "reaped" as long as they return and exit from their entry function (which I assume all processes do) - what is the point of talking about "reaping" as if it was something special?
The child process does not fully "go away" when it exits. It ceases to exist as a running process, and most/all of its resources (memory, open files, etc.) are released, but it still remains in the process table. It remains in the process table because that's where its exit status is stored, so that the parent can retrieve it by calling one of the wait
variants. If the parent fails to call wait
, the process table entry sticks around — and that's what makes it a "zombie".
I said that most/all of its resources are released, but the one resource that's definitely still consumed is that process table slot.
As long as the (dead) child's parent exists, the kernel doesn't know that the parent isn't going to call wait
eventually, so the process table slot has to stay there, so that the eventual call to wait
(if there is one) can return the proper exit status.
If the parent eventually exits (without ever calling wait
), the child will be inherited by the grandparent, which is usually a "master" process like the shell, or init
, that does routinely call wait
and that will finally "reap" the poor young zombie.
So, yes, it really is true that the only way for the parent to properly "reap" the child is, just as was said in your lecture, to call one of the wait
functions. (Or to exit, but that's not an option if the parent is long-running.)
Footnote: I said "the child will be inherited by the grandparent", but I think I was wrong, there. Under Unix and Linux, orphaned processes are generally always inherited by pid 1, aka init
.