php 多进程中的信号问题
1.以下代码sleep时间远小于20
<?php // 当子进程退出时,会触发该函数function sig_handler($sig) { switch($sig) { case SIGCHLD: echo 'SIGCHLD received'."n"; }} pcntl_signal(SIGCHLD, "sig_handler");// 注册子进程退出时调用的函数$start = time(); $sub_process_cnt = 20;for($i=0;$i<$sub_process_cnt;$i++) { sleep(1); $pid = pcntl_fork(); if ($pid == 0) { exit(-1); }}$status = 0; for ($k=0; $k<$sub_process_cnt; $k++) { pcntl_waitpid(-1, $status );}$end = time();$usage = $end - $start;print "End, use: ".$usage." seconds" ; ?>
3.解决办法:
pcntl_signal(SIGCHLD, SIG_IGN ); //忽略子进程返回信号
全部代码如下
<?php // 当子进程退出时,会触发该函数function sig_handler($sig) { switch($sig) { case SIGCHLD: echo 'SIGCHLD received'."n"; }} pcntl_signal(SIGCHLD, SIG_IGN );// 注册子进程退出时调用的函数$start = time(); $sub_process_cnt = 20;for($i=0;$i<$sub_process_cnt;$i++) { sleep(1); $pid = pcntl_fork(); if ($pid == 0) { exit(-1); }}$status = 0; for ($k=0; $k<$sub_process_cnt; $k++) { pcntl_waitpid(-1, $status );}$end = time();$usage = $end - $start;print "End, use: ".$usage." seconds" ; ?>