UNIX Zombies and Daemons

梦想的初衷 提交于 2019-12-22 04:26:16

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7285109/unix-zombies-and-daemons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!