SIGCHILD not catching signal when child process dies

戏子无情 提交于 2019-12-24 14:19:04

问题


I'm trying to create a daemon process that handles several child threads. But the child thread doesn't seem to send the signal back to the parent to call the function. i have tried to take it out of the class and make it a standard function but that doesn't seem to help either.

class Daemon {
    public function __construct() {

        $set = pcntl_signal(SIGCHLD, array($this, 'childSignalHandler'));
        $pid = pcntl_fork();
        if ($pid == -1) {
            echo 'could not fork';
        } elseif ($pid) {
            // parent
            sleep(20);
            // this would keep running and spawn other children from time to time
        } else {
            // child
            sleep(5);
            // should call childSignalHandler() in parent
        }
    }

    public function childSignalHandler($pid) {
         echo 'child is dead';
    }
}

new Daemon();

回答1:


apparently it works if I add declare(ticks = 1); what's confusing is that this is deprecated as of 5.3 but i can't find any info on what's supposed to replace it.



来源:https://stackoverflow.com/questions/27071051/sigchild-not-catching-signal-when-child-process-dies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!