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} \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复用监控进程间的管道可读)

posted @ 2016-04-25 00:52  小'帅  阅读(169)  评论(0编辑  收藏  举报