Skip to content

Instantly share code, notes, and snippets.

@gom
Created August 20, 2010 11:07
Show Gist options
  • Save gom/540085 to your computer and use it in GitHub Desktop.
Save gom/540085 to your computer and use it in GitHub Desktop.
<?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