子进程死亡时SIGCHILD不能捕获信号 [英] SIGCHILD not catching signal when child process dies

查看:339
本文介绍了子进程死亡时SIGCHILD不能捕获信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个处理几个子线程的守护进程。但是子线程似乎没有将信号发送回父级以调用该函数。我试图将其从类中删除并使其成为标准函数,但这似乎也无济于事。

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();


推荐答案

显然,如果我添加clarify(ticks = 1 );令人困惑的是,自5.3版起已弃用该功能,但我找不到有关应替换该功能的任何信息。

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.

这篇关于子进程死亡时SIGCHILD不能捕获信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆