function performSomeFunction($n, &$ret){
$pid = getmypid();
echo "this is in {$pid}.{$n}\n";
usleep(500000);
$ret[$pid] = array($pid);
exit(0);
}
$i = 0;
$starttime = microtime(TRUE);
$pid_arr = array();
$ret_arr = array();
$num = intval($argv[1]);
echo $num."\n";
while ($i < $num)
{
$pid = pcntl_fork();
if ($pid == -1)
{
die('could not fork');
}
else
{
if ($pid) // parent
{
$pid_arr[$i] = $pid;
}
else // child
{
performSomeFunction($i+1, $ret_arr);
}
}
$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);
?>
$num = intval($argv[1]);
$i = 0;
$retArr = array();
$generateNumberArr = array();
while ($i++ < $num) {
$generateNumberArr[$i] = popen("/home/users/gino/apps/php5/bin/php /home/users/gino/test/pcntl/one.php " .$i, 'r');
}
foreach ($generateNumberArr as $generateNumberArr_k =>$generateNumberArr_v) {
$str = '';
while (! feof($generateNumberArr_v)) {
$str .= fgets($generateNumberArr_v, 4096);
}
$retArr[] = $str;
pclose($generateNumberArr_v);
}
var_dump($retArr);
?>