I have some code like this, and I want to understand how does fork work, but I'm confused with declare(ticks=1)
.
when I put it in the first line, after the child process finished, the signal handler will be called, which is what I want; but when I remove it, the signal handler will never be called! So, I want to know how does the ticks influence the signal processing.
<?php
declare(ticks=1);
function sigHandler($signal)
{
echo "a child exited\n";
}
pcntl_signal(SIGCHLD, sigHandler, false);
echo "this is " . posix_getpid() . PHP_EOL;
for($i=0; $i<3; $i++)
{
$pid = pcntl_fork();
if($pid == -1)
{
echo 'fork failed ' . PHP_EOL;
}
else if($pid)
{
}
else
{
$pid = posix_getpid();
echo 'child ' . $pid . ' ' . time() . PHP_EOL;
sleep(rand(2,5));
echo 'child ' . $pid . ' done ' . time() . PHP_EOL;
exit(0);
}
}
do
{
$pid = pcntl_wait($status);
echo 'child quit ' . $pid . PHP_EOL;
}while($pid > 0);
echo 'parent done' . PHP_EOL;
?>
Minor observation (quote the function name pls.):
pcntl_signal(SIGCHLD, 'sigHandler', false);
There are two different APIs involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With