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

标签 php mysql pear pcntl

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

所以Daemon fork了一堆进程 然后 fork 另一串进程

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

父进程(守护进程)做这样的事情:

while(true)
{
SQL QUERY EXECUTED

   while(mysql_fetch_array)
   {
       Fork children
   }
}

问题是,如果父进程除了 fork 子进程还需要做其他工作,并且子进程需要很长时间才能退出,我该如何等待子进程退出。

我正在使用系统守护进程 PEAR 函数创建守护进程,并使用 pcntl_fork 函数创建进程。

最佳答案

我不记得我在哪里看到的:

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

诀窍在于,当 Granchild 死亡时,其 parent (您的 child 之一)已经死亡。但是必须通知某人死亡。看来在 Linux 系统中,通知的不是祖 parent ,而是所有人的曾祖 parent 。而且因为该进程知道自己的工作,所以它会定期检查死去的 child ,并且不允许他们变成僵尸。

这是一个带有解释的链接: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.

关于php - 在父进程中杀死僵尸子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8086230/

相关文章:

php - 将 if 设置为多个变量

php - Laravel 队列进程超时错误

php - 在 Codeigniter/PHP 中显示类别

php - Pear 错误已弃用

php - 验证表单而不重定向

php - 无法在 ListView 项中显示图像

mysql - 如何对表的选定列进行 mysql 转储

MySQL 查询使用 'AND' 子句将值设置为 0

PHP PEAR 电子表格 Excel Writer 数据限制

php - 使用Mail_Mime发送附件到GMail,收到 "noname"附件