Home  >  Article  >  Backend Development  >  php 多进程中的信号有关问题

php 多进程中的信号有关问题

WBOY
WBOYOriginal
2016-06-13 11:33:07828browse

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" ;    ?>
2.原因:sleep过程中被子进程返回的信号中断。

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" ;    ?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn