Example analysis of blocking and non blocking of parent process in PHP multiprocess programming

Time:2021-6-12

This paper introduces the blocking and non blocking of parent process in PHP multiprocess programming. The details are as follows:

PHP process blocking, mainly waiting for the parent process to exit the child process.

1. PHP code is as follows:

<?php

//Define the number of processes
define('FORK_NUMS', 5);

//Used to save process PID
$pids = array();

//We create five subprocesses
for ($i = 0; $i < FORK_NUMS; ++$i) {
  $pids[$i] = pcntl_fork();
  if ($pids[$i] == -1) {
    die('fork error');
  } else if ($pids[$i]) {
    //This is the parent process space, which is the main process
    //The first time our for loop got here, pcntl_ Wait will suspend the current main process, wait for the first child process to finish executing and exit
    //Note that the code of the for loop is in the main process. Suspending the main process means that the current for loop is blocked here
    //After the first subprocess exits, create the second subprocess, hang up here, wait for the second subprocess to exit, continue to create the third, and so on..
    pcntl_wait($status);
  } else {
    //This is the subprocess space
    Echo "parent process ID:", POSIX_ Getppid (), "process ID":, POSIX_ getpid(), " {$i} \r\n";
    //We let the child process wait for 3 seconds before exiting
    sleep(3);
    exit;
  }
}

The result of the above code is as follows:

If we put pcntl_ What’s the result of waiting outside the for loop?

2. PHP code is as follows:

<?php

//Define the number of processes
define('FORK_NUMS', 5);

//Used to save process PID
$pids = array();

//We create five subprocesses
for ($i = 0; $i < FORK_NUMS; ++$i) {
  $pids[$i] = pcntl_fork();
  if ($pids[$i] == -1) {
    die('fork error');
  } else if ($pids[$i]) {

  } else {
    //This is the subprocess space
    Echo "parent process ID:", POSIX_ Getppid (), "process ID":, POSIX_ getpid(), " {$i} \r\n";
    //We let the child process wait for 3 seconds before exiting
    sleep(3);
    exit;
  }
}

//We put pcntl_ Waitpid is placed outside the for loop so that the creation of child processes in the for loop will not block
//However, it will still block here. The main process will wait for all five subprocesses to exit before exiting.
foreach ($pids as $pid) {
  pcntl_waitpid($pid, $status);
}

The result of the above code is as follows:

pcntl_ The second parameter of wait can be used to set the main process to continue to execute the subsequent code without waiting for the child process to exit.

3. PHP code is as follows:

<?php

//Define the number of processes
define('FORK_NUMS', 5);

//Used to save process PID
$pids = array();

//We create five subprocesses
for ($i = 0; $i < FORK_NUMS; ++$i) {
  $pids[$i] = pcntl_fork();
  if ($pids[$i] == -1) {
    die('fork error');
  } else if ($pids[$i]) {
    //This is the parent process space, which is the main process
    //There is only one difference between the code here and 1. PHP, that is, the second parameter wnohang is added
    //When the for loop reaches this point, the main process will not be suspended, but the subsequent code will continue to execute
    pcntl_wait($status, WNOHANG);
  } else {
    //This is the subprocess space
    Echo "parent process ID:", POSIX_ Getppid (), "process ID":, POSIX_ getpid(), " {$i} \r\n";
    //We let the child process wait for 3 seconds before exiting
    sleep(3);
    exit;
  }
}

//Here we wait for 10 seconds, otherwise the sub process has not finished executing, the main process will exit, and no effect can be seen
sleep(10);

The result of the above code is as follows:

Code 3. PHP has the same effect as 2. PHP, pcntl_ The wait() function is only used to make the parent process wait for the child process to exit. By default, the main process will be blocked.

For more information about PHP, readers interested in this site can see the following topics: summary of PHP process and thread operation skills, summary of PHP network programming skills, introductory course of PHP basic syntax, complete collection of PHP array operation skills, summary of PHP string usage “PHP + MySQL database operation tutorial” and “PHP common database operation skills summary”

I hope this article is helpful for PHP programming.