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?
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?
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 init
process.
Zombies are child processes which have already terminated, and exist when their parent is still alive but has not yet called wait
to obtain their exit status. If the parent dies (and has not called wait
), all of the zombie children are adopted by the init
process and it eventually calls wait
on 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
.
Secondly, the parent of a daemonized process dies off, so why isn't
the daemonized process considered a zombie?
The parents of daemonized processes die off, but the daemonized process detaches from the controlling terminal and becomes a process group leader via the setsid
system call.
Well, when a child process started, there is entry created at kernel level along with its parent process id. Due to whatever reasons (server hand, parent process killed from application end, etc.,) parent process killed and child process left. Kernel cannot clean such process. Only parent process authorized to do so. Because such process is still lying in a table at kernel so it is eating resources too but doing nothing. So, its called zombie.