Example of multi process control demo implemented by PHP

  • 2021-12-12 08:22:07
  • OfStack

This paper describes the multi-process control realized by PHP. Share it for your reference, as follows:

I wrote a framework code of multi-process control and kept it for future reference


declare(ticks=1);
function sigHandler($signal)
{
  echo "a child exited\n";
}
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;

For more readers interested in PHP related content, please check the topics on this site: "Summary of PHP Process and Thread Operation Skills", "Summary of PHP Network Programming Skills", "Introduction to PHP Basic Syntax", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: