C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远程连接

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 on 2015-05-03 23:51  思齐_  阅读(563)  评论(0编辑  收藏  举报