Created
August 20, 2010 11:07
-
-
Save gom/540085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Test { | |
private $_num; | |
private $_max_process_num = 5; | |
public function __construct($num) { | |
$this->_num = $num; | |
} | |
public function run() { | |
$pids = array(); | |
for ($i = 0; $i < $this->_num; $i++) { | |
$pid = pcntl_fork(); | |
var_dump($pid); | |
if ($pid == -1) { | |
throw new Exception('failed'); | |
} else if ($pid) { | |
// parent | |
$pids[$pid] = true; | |
if (count($pids) > $this->_max_process_num) { | |
$wait_pid = pcntl_waitpid(-1, $status, WUNTRACED); | |
unset($pids[$wait_pid]); | |
} | |
} else { | |
// child | |
// working for child | |
var_dump(array($i, $pid)); | |
exit; | |
} | |
} | |
while (count($pids) > 0) { | |
$wait_pid = pcntl_waitpid(-1, $status, WUNTRACED); | |
unset($pids[$wait_pid]); | |
} | |
} | |
} | |
$t = new Test(5); | |
$t->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment