<?php header("Content-type:text/html;charset=utf-8"); $max = 800000; $workers = 20; $pids = array(); for($i = 0; $i < $workers; $i++){ $pids[$i] = pcntl_fork(); switch ($pids[$i]) { case -1: echo "fork error : {$i} \r\n"; exit; case 0: $param = array( 'lastid' => $max / $workers * $i, 'maxid' => $max / $workers * ($i+1), ); var_dump($pids[$i]); echo ": $i \r\n"; exit; default: break; } } foreach ($pids as $i => $pid) { if($pid) { pcntl_waitpid($pid, $status); } } echo "done";
运行结果:
int(0)
: 2
int(0)
: 0
int(0)
: 1
int(0)
: 3
int(0)
: 4
int(0)
: 5
int(0)
: 6
int(0)
: 7
int(0)
: 8
int(0)
: 9
int(0)
: 10
int(0)
: 11
int(0)
: 12
int(0)
: 13
int(0)
: 14
int(0)
: 15
int(0)
: 16
int(0)
: 18
int(0)
: 19
int(0)
: 17
done
最新评论: