My main process spawns a child process. If the main process is killed, the child would be assigned ppid of 1. When the child exits, it would become a zombie as init has not called wait() on this child. Is there a way to avoid this situation?
Share
initwill callwait()on processes that it inherits. Zombies should only exist where the child has exited but the parent is still around, but is yet to reap the exit code. From theinitmanpage:You should make a distinction between orphans (they’re still alive, their parent is dead and hence they’ve been adopted by
init), and zombies (they’re dead but their parent is alive and has not yet reaped them).Orphans will become zombies for a very short period of time after they exit but before
initreaps them but this period should be small enough that no-one notices. In fact, all exiting processes (except possiblyinititself) go through this short zombie phase, it’s only those cases where the parent doesn’t reap quickly enough that you notice.The actual code in the
initchild death (SIGCHLD) handler goes something like this (my comments):And then, later on in the main loop (not signal handler), any processes in the process table marked as
ZOMBIEare cleaned up: