在父进程中杀死僵尸子进程

发布于 2024-12-14 15:51:49 字数 401 浏览 0 评论 0原文

所以我想做以下事情:
设置一个派生一堆进程的守护进程。

所以守护进程分叉了一堆进程 然后分叉另一组进程,

问题是子进程可能需要很长时间才能退出。如果父进程尽管分叉子进程还有其他工作要做,如何防止僵尸子进程?

父进程(守护进程)执行类似以下操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城已无爱 2024-12-21 15:51:49

我不记得我在哪里看到过这个:

Parent forks child
  Waits until child is dead  (this won't take long, see ahead)
  Goes on

Child does only 2 things:
  Forks a grandchild
  Exits

Grandchild does whatever work is needed
  Exits

诀窍是,当孙子去世时,它的父母(你的孩子之一)已经死了。但必须通知有人死亡。看起来,在 Linux 系统中,收到通知的不是祖父母,而是所有的祖父母。因为该进程知道自己的工作,所以它会定期检查死去的孩子,并不允许他们成为僵尸。

这是一个带有解释的链接: http://fixunix.com/unix/533215 -how-avoid-zombie-processes.html

当一个进程的父进程终止时,“init”进程将接管它的进程
父母。所以当子进程退出时,孙进程就失去了它的
父级,并被 init采用。 Init 总是收获它死去的孩子,所以
他们不会变成僵尸。

I don't remember where I saw this:

Parent forks child
  Waits until child is dead  (this won't take long, see ahead)
  Goes on

Child does only 2 things:
  Forks a grandchild
  Exits

Grandchild does whatever work is needed
  Exits

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

When a process's parent terminates, the "init" process takes over as its
parent. So when the child process exits, the grandchild loses its
parent, and is adopted by init. Init always reaps its dead children, so
they don't become zombies.

ㄟ。诗瑗 2024-12-21 15:51:49

您应该考虑让父母除了等待孩子之外什么也不做。如果父母因任何原因死亡,那么孩子们就会变成僵尸。然而,如果父母不做任何事情,那么它意外死亡的机会就很小。

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.

傲性难收 2024-12-21 15:51:49

如果您将 SIGCHLD 处理程序显式设置为 SIG_IGN(而不是 SIG_DFL),这将阻止您的子进程成为僵尸进程(假设您不感兴趣)在他们的退出代码中。或者,在较新的 Linux 上,您应该使用 sigactionSA_NOCLDWAIT 标志。

If you explicitly set your SIGCHLD handler to SIG_IGN (instead of SIG_DFL) this will stop your children becoming zombie processes, assuming you're not interested in their exit codes. Alternatively on newer Linuxes you should use sigaction's SA_NOCLDWAIT flag instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文