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

WBOY
Release: 2016-06-13 12:38:50
Original
806 people have browsed it

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" ;
    
?>
Copy after login
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" ;
    
?>
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template