php pcntl 多进程学习

转:php pcntl 多进程学习

1、捕获子进程退出(监听SIGCHLD信号,然后调用 pcntl_wait 函数)

复制代码
declare(ticks=1);

pcntl_signal(SIGCHLD, "sig_handler");
function sig_handler($signo)
{
    switch ($signo) {
        case SIGCHLD:
            $status = 0;
            $child_id = pcntl_wait($status);
            echo sprintf("child exit id: {$child_id} 
");
            exit(0);
            break;

        default:
            echo 'uncaugh signal !';
    }

}

$pid = pcntl_fork();
if($pid>0)
{
    echo sprintf("fork child id: {$pid} 
");
    
    while(1){
        sleep(1);
    }

}else{
    echo "child exit 
";
}
复制代码

2、捕获子进程退出(直接调用 pcntl_wai* 函数)

3、捕获子进程退出 (io复用监控进程间的管道可读)

原文地址:https://www.cnblogs.com/lishuaige/p/5429096.html