Home  >  Article  >  php教程  >  PHP多进程简单实例程序

PHP多进程简单实例程序

WBOY
WBOYOriginal
2016-06-13 09:47:331124browse

多进程可以说php在这方面比起java差得太多了,下面出现好玩找到一个关于PHP多进程实例,给大家看一下。

 代码如下 复制代码

$cmds=array(a,b,c,d);
foreach($cmds as $cmd){
 $pid = pcntl_fork();
 if($pid == -1) {
  exit("pid fork error");
 }  
 if($pid) { //主进程段,控制子进程数量
  static $max = 0;
  $max++;
  if($max >= 5) {
   pcntl_wait($status); //阻塞父进程,直到子进程结束
   $max--;
  }  
 } else { //子进程执行命令
  $pid = posix_getpid();
  echo "$pid----$cmdnn";
  exit(0);
 }  
}  
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn