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} \n");
            exit(0);
            break;

        default:
            echo 'uncaugh signal !';
    }

}

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

}else{
    echo "child exit \n";
}

 

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

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

 

你可能感兴趣的:(PHP)