Detailed explanation of pHP multi-process programming examples
In fact, PHP supports concurrency, but it is rarely used normally. The one who uses the most is probably to use PHP-FMP to schedule the php process.
However, the use of PHP is not limited to the Web. We can also use PHP to program system tool classes, monitor or operate and maintain. When using these directions, we can use more features of PHP, such as concurrency (multi-process), socket programming, etc.
Then let’s talk about the PHP multi-process programming I encountered. The use of this multi-process has a background, and the background is blurred below.
I need a monitoring system. Of course, using PHP language, the monitoring system needs to monitor many types of system indicators. In order to make each monitoring indicator concentrate on doing its own thing as much as possible, it is necessary to use a process to monitor an indicator, and another process to read the configuration. After getting the configuration, each process will be started according to the configuration.
Then, this requires what I've called multiple processes.
- 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 5 child processes and monitors these 5 indicators respectively.
- After creating 5 metric monitoring processes, the main process performs monitoring configuration.
- Once the configuration changes, kill the previous process and recreate the process.
Relatively clear logic. Then let’s simplify the operation: simply put, a main process creates 5 child processes.
First of all, when creating a process, you need to use a function pcntl_fork() in php. Some students may not be familiar with this function, but those who have been exposed to Linux C know that there is a function called fork() in Linux to create child processes. This function has the same meaning as this function under Linux. It should be noted that this function can only be used under Linux, and the pcntl extension needs to be installed.
For how to use this function, we can check the official documentation:/manual/zh/
The official documentation says this:
The pcntl_fork() function creates a child process, which only has PID (process number) and PPID (parent process number) and its parent process. Fork's details on how to work in your system, please refer to your system's fork (2) manual.
When successful, the PID of the generated child process is returned within the parent process execution thread and 0 is returned within the child process execution thread. On failure, return -1 in the parent process context, the child process is not created and a PHP error is thrown.
This way you can create a child process. After the child process is successfully created, the method after pcntl_fork() will be executed. So how do we understand the return value of this function?
It is like this. When we call a function to create a process, the function has time to execute, and the new process happens to be created between the beginning and end of the function execution. In this way, the new process also executes the function, so the function also needs to have a return value. After the function is executed once, the parent process and the child process will receive the return value of the function. Since the parent process creates the child process, and the child process does not create a new process, the child process does not return the function, so it is assigned a 0. The parent process creates a child process, and the child process has a pid, so the pid of that process is obtained.
We can write a program to learn about it:
$pid = pcntl_fork(); var_dump($pid);
This call will output two values, but if we print directly, we can only see one value, that is, the pid of the child process, but using var_dump we can see two values, which are 0 and the pid of the child process. The value 0 is returned by the child process.
So after understanding how to create a process, you can start creating a process. We need to create 5 processes, so I will create a process 5 times. Get the following code:
$i=0; while($i!=5){ $pid = pcntl_fork(); echo $pid."---------hahah".$i++.PHP_EOL; }
This is done, so run it. ah? I found that there are not 5 processes, and I found that there are many processes, and the last hahah4 output has 32. Why is it 32? Let's do it. 2^5=32, why does the last number of threads grow exponentially?
It is not difficult to find this, because each of us then executes a while loop, and finally becomes an exponential growth of the process - that is, we also bring the while loop in when we are fork. But we only need 5 processes. What to do?
Through previous research on functions, we can see that a value of 0 will be returned in the child process, so we can know that 0 is the mark of the child process. We can end process execution by marking the child process. 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 "Subprocess".PHP_EOL; return; } }
Because 0 is actually a marker for the child process, the pid variable is actually 0 in the child process. Therefore, when the value of pid is found to be 0, we can determine that our current process is a child process, and there is no need to let it execute while and create the child process of the child process. So after executing our content, just return or exit exit to exit. This ensures that we have created 5 processes instead of 32.
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!