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, the only way for the parent to properly "reap" the child is, as your lecture said, to call one of the wait
functions. (Or to exit, but that's poor if the parent is long-running.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…