$pids = array(); $child_pid = pcntl_fork(); if ($child_pid == -1) { throw new Exception( __METHOD__ . "|" . __LINE__ . ": fork() error"); } else if ($child_pid) { //parent exit(0); } else { //child for($i=0;$i<3;$i++) { $child_pid = pcntl_fork(); if($child_pid) { //parent $pids[] = $child_pid; sleep(5); print_r($pids);echo " "; }else{ //child break; } } } while(1) { //your code sleep(1); }
理解:每执行一个fork,相当于把代码复制了两分,分别去分析代码的执行情况
for循环中进程fork参考 http://blog.csdn.net/jason314/article/details/5640969