8

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?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • 2
    Anyone want to give a comment for the anonymous downvote and close request? I feel like it's a pretty solid question. – John Humphreys Sep 02 '11 at 14:55
  • 2
    It could be argued that this is more a serverfault question, but process management is relevant to *nix programming so I don't see much issue with it. – Neil Aitken Sep 02 '11 at 15:49
  • 1
    Processes whose parent has died are orphans, not zombies. Zombies are the 'living dead'; orphans can be productive members of society. – Jonathan Leffler Sep 02 '11 at 15:53
  • The question makes 2 wrong assumptions. The first one is about the nature of zombie processes, and this is addressed by @blagovest-buyukliev 's answer. The other is about the nature of daemons, which was not addressed, so here goes: daemons are the UNIX/Linux equivalent of services. These are just processes that run in the background. They are usually managed (started/stopped/monitored, etc.) by the init system, like SysV init or systemd. – ivant Nov 22 '22 at 14:58

2 Answers2

15

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.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • Picking nits: all processes except process 1 are child processes, and a zombie is simply one of the 'living dead', a process that has died but whose parent has not yet waited to collect the status of the corpse. A zombie is a problem because it occupies a slot in the process table that can't be reused until the corpse has been cleaned up (by the original parent process or by the system if the parent dies without waiting). In extreme cases (200,000 zombies), they can seriously slow up a system. I had that problem earlier this year: three zombies a minute from a system process. O/S update! – Jonathan Leffler Sep 02 '11 at 15:52
  • 2
    Just one more addition to this nice answer: One reason to keep the zombies is that the PID should not reused until the parent has processed the termination message. – Drunix Jan 17 '13 at 09:24
0

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.

sumana
  • 149
  • 2
  • 8
  • 1
    In earlier versions of Solaris only parent process is authorized to clean child process but from Solaris 11 init process clean all zombie process itself if parent dies. – sumana Jan 17 '13 at 09:17