UNIX 僵尸和守护进程
据我所知,当进程不能很好地清理(其资源没有被回收/收获)时,就会创建僵尸。调用 fork() 创建新进程后,父进程应始终对该进程调用 waitpid 来清理它。
我还了解到,守护进程是通过分叉一个本身由 fork 创建的子进程,然后让该子进程死亡来创建的。显然,一旦执行此操作,UNIX 中的 init 进程 (pid #1) 将接管该进程。
我想知道的是 - 据我所知,当父母去世时,它会自动清理孩子 - 那么僵尸是如何创建的呢?
其次,守护进程的父进程死亡,那么为什么守护进程不被视为僵尸进程呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,父级不会自动清理子级。每当一个进程终止时,其所有子进程(正在运行或僵尸)都会被 init 进程采用。
僵尸进程是已经终止的子进程,当其父进程仍处于活动状态但尚未调用
wait
来获取其退出状态时就存在。如果父进程死亡(并且未调用wait
),则所有僵尸子进程都会被init
进程采用,并最终调用wait
所有的人都去收获它们,所以它们从进程表中消失了。保留僵尸进程背后的想法是保留有关进程终止的适当数据结构,以防父进程通过
wait
感兴趣。守护进程的父进程会消失,但守护进程会从控制终端分离,并通过setsid系统调用成为进程组领导者。
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 calledwait
), all of the zombie children are adopted by theinit
process and it eventually callswait
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
.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.好吧,当子进程启动时,会在内核级别创建一个条目及其父进程 ID。由于某种原因(服务器手工、应用程序端杀死父进程等),父进程被杀死,子进程离开。内核无法清理此类进程。只有父进程有权这样做。因为这样的进程仍然位于内核的表中,所以它也在消耗资源,但什么也不做。所以,它被称为僵尸。
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.