Detailed Explanation of pcntl_fork Example of PHP Multi process

  • 2021-08-10 06:58:02
  • OfStack

Detailed Explanation of the Example of pcntl_fork Compiled by PHP Multi-process

In fact, PHP supports concurrency, but it is rarely used at ordinary times. Usually the most used should be the use of PHP-FMP scheduling php process.

However, the use of PHP is not limited to doing Web, and we can also use PHP for programming, monitoring or operation and maintenance of system tools. When using these directions, we can use more features of PHP, such as concurrency (multi-process), socket programming and so on.

Then let's talk about the PHP multi-process programming I encountered. The use of this multi-process has a background, which is described vaguely below.

I need a monitoring system, of course, using PHP language, the monitoring system needs to monitor a lot of system indicators. In order to concentrate on doing their own things as much as possible between each monitoring indicator, it is necessary to use a separate process to monitor one indicator, and there is also a process to read the configuration. After getting the configuration, start each process according to the configuration.

Then, this requires what I call multi-process.

First, start a main process, which is used to read configuration information. For example, I read that I need to monitor 5 metrics Next, the main process starts five sub-processes to monitor these five indicators respectively. After creating 5 indicators to monitor the process, the main process performs monitoring configuration. 1 Once the configuration changes, kill the previous process and recreate the process.

Relatively clear logic. Then let's simplify 1 operation: Simply put, 1 main process creates 5 sub-processes.

First of all, the creation process needs to use a function pcntl_fork () of php. Some students may not be familiar with this function, but those who have come into contact with Linux C know that there is a function called fork () under Linux to create sub-processes. This function and this function under Linux mean 1. Note that this function can only be used under Linux, and the extension of pcntl needs to be installed.

For how to use this function, we can refer to the official document: http://php.net/manual/zh/function.pcntl-fork.php

The official document says this:

The pcntl_fork () function creates a child process that differs from its parent process only PID (process number) and PPID (parent process number). For more information on how fork works on your system, please refer to the fork (2) manual for your system.

On success, the PID of the generated child process is returned in the parent process execution thread, and 0 in the child process execution thread. On failure, returns-1 in the parent process context, does not create a child process, and raises an PHP error.

This allows you to create a child process, which executes the method after pcntl_fork (). So how do we understand the return value of this function?

Well, when we call a function to create a process, there is time for the function to execute, and the new process is created just between the beginning and the end of the function execution, so the new process also executes the function, so the function also needs to have a return value. Then, after the function is executed once, both the parent process and the child process will receive the return value of the function. Because the parent process created the child process and the child process did not create a new process, the child process did not return the result of this function, so it was assigned a 0. The parent process creates a child process, and the child process has an pid, so you get the pid of that process.

We can write a program to understand 1:


$pid = pcntl_fork();
var_dump($pid);

This call will output two values, but we can only see one value if we directly print, that is, pid of the child process, but with var_dump, we can see two values, which are 0 and pid of the child process. The value 0 is returned by the child process.

Then how to create the process after understanding clearly, can start to create the process, we need to create 5 processes, then I will loop 5 times to create the process. Get the following code:


 $i=0;
 while($i!=5){
  $pid = pcntl_fork();
  echo $pid."---------hahah".$i++.PHP_EOL;
 }

That's it, so run it for 1 time. Huh? Found is not 5 processes ah, found that there are many processes, and the last one hahah4 this output has 32, why 32? Let's count one. 2 ^ 5 = 32, why did the last number of threads increase exponentially?

It is not difficult to find this, because every one of our subsequent entries executes the while loop, which eventually becomes an exponential growth of the process-that is to say, the fork also brings in the while loop. But we just need five processes. What shall I do?

From the previous study of the function, we can see that the child process will return a value of 0, so we can know that 0 is the flag of the child process. We can end process execution by marking child processes. So we can modify our code to the following form:


$i=0;
while($i!=5){
 $pid = pcntl_fork();
 echo $pid."---------hahah".$i++.PHP_EOL;
 if ($pid == 0) {
  echo " Child process ".PHP_EOL;
  return;
 }
}

Because 0 is actually a token for a child process, Then the variable pid is actually 0 in the sub-process, so when we find that the value of pid is 0, we can conclude that our current process is a sub-process, and we don't need to let him execute while and create a sub-process of the sub-process, so it is good to exit the execution of return or exit after executing our contents. This ensures that we have created 5 processes instead of 32.

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: