note 91790 added to function.pcntl-waitpid
| From: | tunderzone at gmail dot com | Date: | Thu, 25 Jun 2009 13:26:18 +0000 |
| Subject: | note 91790 added to function.pcntl-waitpid | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157497@lists.php.net to get a copy of this message | ||
A better way to do this and not end up having zombie processes until all child processes ends is
like this:
<?php
$i = 0;
$starttime = microtime(TRUE);
$pid_arr = array();
while ($i < intval($argv[1]))
{
$pid = pcntl_fork();
if ($pid == -1)
{
die('could not fork');
}
else
{
if ($pid) // parent
{
$pid_arr[$i] = $pid;
}
else // child
{
performSomeFunction($i+1);
}
}
$i++;
}
while(count($pid_arr) > 0)
{
$myId = pcntl_waitpid(-1, $status, WNOHANG);
foreach($pid_arr as $key => $pid)
{
if($myId == $pid) unset($pid_arr[$key]);
}
usleep(100);
}
$elapsed = microtime(TRUE) - $starttime;
print "\n==> total elapsed: " . sprintf("%f secs.\n", $elapsed);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 217.156.103.4
----
Manual Page -- http://www.php.net/manual/en/function.pcntl-waitpid.php
Edit -- https://master.php.net/note/edit/91790
Del: integrated -- https://master.php.net/note/delete/91790/integrated
Del: useless -- https://master.php.net/note/delete/91790/useless
Del: bad code -- https://master.php.net/note/delete/91790/bad+code
Del: spam -- https://master.php.net/note/delete/91790/spam
Del: non-english -- https://master.php.net/note/delete/91790/non-english
Del: in docs -- https://master.php.net/note/delete/91790/in+docs
Del: other reasons-- https://master.php.net/note/delete/91790
Reject -- https://master.php.net/note/reject/91790
Search -- https://master.php.net/manage/user-notes.php