Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX Zombies and Daemons

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?

like image 818
John Humphreys Avatar asked Sep 02 '11 14:09

John Humphreys


1 Answers

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.

like image 125
Blagovest Buyukliev Avatar answered Oct 25 '22 00:10

Blagovest Buyukliev