Software engineering notes

PHP Multi-Process

Example code:

echo "Start \n";

$pid = pcntl_fork();
echo "XD \n";
if ($pid)
{
    //$rtn = pcntl_waitpid($pid, $status);
    echo "father \n";
    $sum = 0;
    for ($i = 0; $i <= 1000000; $i++)
    {
        $sum += $i;
    }
}
else
{
    echo "child \n";
}

echo "End \n";

result :

Start
XD
father
XD
child
End
End

註 :

如果要等子程序做完主程序再做的話要在主程序裡加上

$rtn = pcntl_waitpid($pid, $status);

結果為 :

Start
XD
XD
child
End
father
End