在学习一些unix的知识,就用php的pcntl扩展试试,我不知道我的理解是否有问题,因为php的参考代码较少,我对进程也是一知半解的水平。
我的目的是一个父进程生成一个子进程,然后再给子绑定信号SIGUSR1处理器test(),最终由父进程发送信号SIGUSR1给子,即可看到输出hello,world!!declare(ticks = 1);
function test($signo)
{
echo "hello,world!!!!!{$signo}!!!!";
}
echo "script start time was: ". microtime(true) ."\n";
$pid = pcntl_fork();
if($pid === -1)
exit("could not fork");
else if($pid === 0){ // child working
pcntl_signal(SIGUSR1, "test");
pcntl_sigwaitinfo(array(SIGUSR1), $info);
print_r($info);
echo "child's end time was: ". microtime(true) ."\n";
}else { //parent working...
sleep(1); // 如果不加这句,子就好像变僵尸了怎的,上面的那些输出就不会看见
posix_kill($pid, SIGUSR1);
echo "parent's end time was: ". microtime(true) ."\n";
}输出结果:
root@dell-desktop:~# /usr/local/php-cgi/bin/php -f /www/php/pcntl3.php
script start time was: 1330074854.7519
parent's end time was: 1330074855.7524
Array
(
[signo] => 10
[errno] => 0
[code] => 0
)
child's end time was: 1330074855.7528
我很费解,子进程确实是收到了父的信号(array( [signo] =>10)),但为什么信号处理器不执行呢。
而如果换成自己给自己发信号,就可以看到输出hello,world!!!!,例如
pcntl_signal(SIGUSR1, "test");
posix_kill(posix_getpid(), SIGUSR1);
我的目的是一个父进程生成一个子进程,然后再给子绑定信号SIGUSR1处理器test(),最终由父进程发送信号SIGUSR1给子,即可看到输出hello,world!!declare(ticks = 1);
function test($signo)
{
echo "hello,world!!!!!{$signo}!!!!";
}
echo "script start time was: ". microtime(true) ."\n";
$pid = pcntl_fork();
if($pid === -1)
exit("could not fork");
else if($pid === 0){ // child working
pcntl_signal(SIGUSR1, "test");
pcntl_sigwaitinfo(array(SIGUSR1), $info);
print_r($info);
echo "child's end time was: ". microtime(true) ."\n";
}else { //parent working...
sleep(1); // 如果不加这句,子就好像变僵尸了怎的,上面的那些输出就不会看见
posix_kill($pid, SIGUSR1);
echo "parent's end time was: ". microtime(true) ."\n";
}输出结果:
root@dell-desktop:~# /usr/local/php-cgi/bin/php -f /www/php/pcntl3.php
script start time was: 1330074854.7519
parent's end time was: 1330074855.7524
Array
(
[signo] => 10
[errno] => 0
[code] => 0
)
child's end time was: 1330074855.7528
我很费解,子进程确实是收到了父的信号(array( [signo] =>10)),但为什么信号处理器不执行呢。
而如果换成自己给自己发信号,就可以看到输出hello,world!!!!,例如
pcntl_signal(SIGUSR1, "test");
posix_kill(posix_getpid(), SIGUSR1);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货