I understand that a zombie is created when a process doesn’t clean-up well (its resources aren’t reclaimed/reaped). After calling fork() to create a new process, the parent should always call waitpid on that process to clean it up.
I also have learned that a daemon is created by forking a child that was itself created by fork, and then letting the child die. Apparently the init process (pid #1) in UNIX would take custody of the process once you do this.
What I want to know is – as far as I know, when a parent dies it cleans up the child automatically – so how does a zombie get created in the first place?
Secondly, the parent of a daemonized process dies off, so why isn’t the daemonized process considered a zombie?
No, the parent does not clean up the children automatically. Whenever a process terminates, all of its children (running or zombie) are adopted by the
initprocess.Zombies are child processes which have already terminated, and exist when their parent is still alive but has not yet called
waitto obtain their exit status. If the parent dies (and has not calledwait), all of the zombie children are adopted by theinitprocess and it eventually callswaiton all of them to reap them, so they disappear out of the process table.The idea behind keeping a zombie process is to keep the appropriate data structures about the termination of the process in case the parent ever gets interested via a
wait.The parents of daemonized processes die off, but the daemonized process detaches from the controlling terminal and becomes a process group leader via the
setsidsystem call.