note 106860 modified in function.pcntl-fork by danbrown
| From: | danbrown@php.net | Date: | Tue, 20 Dec 2011 17:16:01 +0000 |
| Subject: | note 106860 modified in function.pcntl-fork by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185184@lists.php.net to get a copy of this message | ||
Fork in foreach:
<?php
foreach ($tasks as $v)
{if (($pid=pcntl_fork())===-1)
{//...
continue;
}
else if ($pid)
{pcntl_wait($status, WNOHANG); //protect against zombie children, one wait vs one
child
}
else if ($pid===0)
{ob_start();//prevent output to main process
register_shutdown_function(create_function('$pars',
'ob_end_clean();posix_kill(getmypid(), SIGKILL);'), array());//to kill self before
exit();, or else the resource shared with parent will be closed
//...
exit();//avoid foreach loop in child process
}
}
?>
--was--
Fork in foreach:
<?
foreach ($tasks as $v)
{if (($pid=pcntl_fork())===-1)
{//...
continue;
}
else if ($pid)
{pcntl_wait($status, WNOHANG); //protect against zombie children, one wait vs one
child
}
else if ($pid===0)
{ob_start();//prevent output to main process
register_shutdown_function(create_function('$pars',
'ob_end_clean();posix_kill(getmypid(), SIGKILL);'), array());//to kill self before
exit();, or else the resource shared with parent will be closed
//...
exit();//avoid foreach loop in child process
}
}
?>
http://php.net/manual/en/function.pcntl-fork.php