1 2 3 4 5 6 7 | <?php function test($max) { for($i = 0; $i < $max; $i++) { } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php class fork { private $children = array(); public function __construct($threads) { $this->timestart = microtime(true); $this->threads = $threads; } public function forker($worker, $data) { foreach($data as $row) { $pid = pcntl_fork(); if($pid == -1 ) { exit(1); } elseif($pid) { echo ' PARENT | launching thread (pid: '.$pid.')'."\n"; $this->children[] = $pid; if(count($this->children) >= $this->threads) { $pid = array_shift($this->children); pcntl_waitpid($pid, $status); } } else { echo ' CHILD | working on '.$worker.'('.$row.')'."\n"; $worker($row); echo ' CHILD | finish '.$worker.'('.$row.')'."\n"; exit(0); } } } public function waitchildrens() { foreach($this->children as $child) { pcntl_waitpid($child, $status); } } public function time() { $timeend = microtime(true); $time = $timeend - $this->timestart; echo ' TIME | '.$time.' seconde(s)'."\n"; } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $fork = new fork(4); $fork->forker('test', array('11111111', '22222222', '33333333', '44444444', '55555555', '66666666', '77777777', '88888888')); $fork->waitchildrens(); $fork->time(); ?> |