我想写一个 php 脚本在后台运行。这需要pcntl_fork。我使用这个函数来分叉一个孩子:
function launchchild($programexe,$programvars)
{
//foreach ($tasks as $key => $v){
switch ($pid = pcntl_fork()) {
case -1:
// @fail
die('Fork failed');
break;
case 0:
// @child: Include() misbehaving code here
pcntl_exec($programexe,$programvars);
break;
default:
// @parent
//pcntl_waitpid($pid, $status);
break;
}
//print "Done! :^)\\n\\n";
}
我在一分钟内用下面这样的片段来称呼它:
$parameters = array('/Applications/XAMPP/xamppfiles/htdocs/ping/senario2/master/gather_checks.php', $serialize_triagger, $server_list[$server_choose]);
launchchild('/Applications/XAMPP/xamppfiles/bin/php',$parameters); // fork a child for parallel processing.
所以,它只能优雅地工作几分钟。我用 'ps -ax | 检查了这个过程 grep php'。我看到有这么多的过程永远不会结束,直到代码给出 35 错误,这意味着我们没有更多的内存来运行它!这是 'ps -ax | 的结果 grep php'
ps -ax | grep php
12020 ttys001 0:00.07 /Applications/XAMPP/xamppfiles/bin/php /Applications/XAMPP/xamppfiles/htdocs/ping/senario2/master/main.php
12021 ttys001 0:00.00 (php-5.3.1)
12022 ttys001 0:00.00 (php-5.3.1)
12025 ttys001 0:00.00 (php-5.3.1)
12028 ttys001 0:00.00 (php-5.3.1)
12037 ttys001 0:00.00 (php-5.3.1)
12040 ttys001 0:00.00 (php-5.3.1)
12043 ttys001 0:00.00 (php-5.3.1)
当我取消注释 //pcntl_waitpid($pid, $status); 它就像一个魅力,没有任何问题,永远。但我不希望父母等待孩子的工作。任何事情都会有所帮助。