在父进程中杀死僵尸子进程
所以我想做以下事情:
设置一个派生一堆进程的守护进程。
所以守护进程分叉了一堆进程 然后分叉另一组进程,
问题是子进程可能需要很长时间才能退出。如果父进程尽管分叉子进程还有其他工作要做,如何防止僵尸子进程?
父进程(守护进程)执行类似以下操作:
while(true)
{
SQL QUERY EXECUTED
while(mysql_fetch_array)
{
Fork children
}
}
问题是,如果父进程除了分叉子进程之外还必须执行其他工作,并且子进程需要很长时间才能退出,那么我如何等待子进程退出。
我使用系统守护程序 PEAR 函数来创建守护程序,并使用 pcntl_fork 函数来创建进程。
So I want to do the following:
Set up a daemon that forks a bunch of processes.
So the Daemon forks a bunch of processes
then forks another bunch of processes
the problem is the child processes might take a long time to exit. How do I prevent zombie children if the parent process has other work to do despite forking children?
The parent process (the daemon) does something like this:
while(true)
{
SQL QUERY EXECUTED
while(mysql_fetch_array)
{
Fork children
}
}
The problem is how can I wait for the children processes to exit if the parent process has to do other work besides forking children and if the children take a long time to exit.
I am using the System daemon PEAR function to create the daemon and the pcntl_fork
function to create the processes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不记得我在哪里看到过这个:
诀窍是,当孙子去世时,它的父母(你的孩子之一)已经死了。但必须通知有人死亡。看起来,在 Linux 系统中,收到通知的不是祖父母,而是所有的祖父母。因为该进程知道自己的工作,所以它会定期检查死去的孩子,并不允许他们成为僵尸。
这是一个带有解释的链接: http://fixunix.com/unix/533215 -how-avoid-zombie-processes.html
I don't remember where I saw this:
The trick is that when the Granchild dies, its parent (one of your Children) is already dead. But someone has to be notified for the death. It appears that in Linux systems, it's not the grandparent that is notified but the grand-grand-...-grandparent of all. And because that process knows its job, it periodically checks for dead children and does not allow them to become zombies.
Here's a link with explanation: http://fixunix.com/unix/533215-how-avoid-zombie-processes.html
您应该考虑让父母除了等待孩子之外什么也不做。如果父母因任何原因死亡,那么孩子们就会变成僵尸。然而,如果父母不做任何事情,那么它意外死亡的机会就很小。
You should consider having the parent do nothing other than wait for the children. If the parent dies for any reason, then the children will become zombies. If the parent however doesn't do anything, then there are very few chances for it to die unexpectedly.
如果您将
SIGCHLD
处理程序显式设置为SIG_IGN
(而不是SIG_DFL
),这将阻止您的子进程成为僵尸进程(假设您不感兴趣)在他们的退出代码中。或者,在较新的 Linux 上,您应该使用sigaction
的SA_NOCLDWAIT
标志。If you explicitly set your
SIGCHLD
handler toSIG_IGN
(instead ofSIG_DFL
) this will stop your children becoming zombie processes, assuming you're not interested in their exit codes. Alternatively on newer Linuxes you should usesigaction
'sSA_NOCLDWAIT
flag instead.