PHP实现的多进程控制demo示例

2020-10-20 10:30:45 浏览数 (1)

本文实例讲述了PHP实现的多进程控制。分享给大家供大家参考,具体如下:

自己写了个多进程控制的框架代码,留着备查

代码语言:javascript复制
declare(ticks=1);
function sigHandler($signal)
{
  echo "a child exitedn";
}
pcntl_signal(SIGCHLD, sigHandler, false);
echo "this is " . posix_getpid() . PHP_EOL;
for($i=0; $i<3; $i  )
{
  $pid = pcntl_fork();
  if($pid == -1)
  {
    echo 'fork failed ' . PHP_EOL;
  }
  else if($pid)
  {
  }
  else
  {
    $pid = posix_getpid();
    echo 'child ' . $pid . ' ' . time() . PHP_EOL;
    sleep(rand(2,5));
    echo 'child ' . $pid . ' done ' . time() . PHP_EOL;
    exit(0);
  }
}
do
{
  $pid = pcntl_wait($status);
  echo 'child quit ' . $pid . PHP_EOL;
}while($pid   0);
echo 'parent done' . PHP_EOL;

0 人点赞