SIGCHILD not catching signal when child process di

2019-07-30 23:28发布

问题:

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.