父母分叉后不会收到信号



我在Symfony 1.4

的PHP中遇到了一个奇怪的问题

我有一个启动多个工人的任务,有时我需要停止所有工人(例如,在部署之后)。

我使用start-waemon启动任务,我想通过向其发送信号sigint来停止它。

所以,这是我的代码:

protected function execute($arguments = array(), $options = array())
{
    $pid_arr = array();
    $thread = $this->forkChildren($arguments, $options, $options['nb_children']);
    if ($this->iAmParent())
    {
        declare(ticks = 1);
        pcntl_signal(SIGINT, array($this, 'signalHandler'));
        // Retrieve list of children PIDs
        $pid_arr = $this->getChildrenPids();
        // While there are still children processes
        while(count($pid_arr) > 0)
        {
            $myId = pcntl_waitpid(-1, $status);
            foreach($pid_arr as $key => $pid)
            {
                // If the stopped process is indeed a children of the parent process
                if ($myId == $pid)
                {
                    $this->removeChildrenPid($key);
                    // Recreate a child
                    $this->createNewChildren($arguments, $options, 1, $pid_arr);
                }
            }
            usleep(1000000);
            $pid_arr = $this->getChildrenPids();
        }
    }
    else
        $thread->run();
}
public function signalHandler($signal)
{
    echo "HANDLED SIGNAL $signaln";
    foreach ($this->getChildrenPids() as $childrenPid)
    {
        echo "KILLING $childrenPidn";
        posix_kill($childrenPid, $signal);
    }
    exit();
}

我的工作非常简单:我分叉,创建n个孩子的过程,在父母中,我添加了一个pcntl_signal来捕获Sigint信号。SignalHanlder函数检索儿童PID列表,并将其发送与刚收到的相同信号(So Sigint)。

问题在于,当我向父进程发送int信号(通过杀死)时,信号手函数从未调用。我不明白为什么!

很奇怪的是,当我在CLI中启动任务并使用CTRL-C时,请调用SignalHandler功能并停止所有孩子。

那么,您知道为什么会发生这种情况吗?我做错了吗?

好吧,算了,我在提出问题之后就发现了问题:

我刚替换

$myId = pcntl_waitpid(-1, $status);

$myId = pcntl_waitpid(-1, $status, WNOHANG);

当然,这个过程被挂断了,等待一个孩子死亡。

最新更新