A Preliminary Study of PHP Multiprocesses-Opening

  linux, Multi-process, php

[Original Address:https://blog.ti-node.com/blog …]

In fact, PHP is multi-threaded, but it is not commonly used by many people. Using PHP multithreading requires downloading PHP with a thread-safe version (ZTS version) before installing pecl’sPthread extension.

In fact, php has multiple processes, and some people reuse it. Generally speaking, PHP’s multiple processes are not bad, just open pcntl module when installing PHP (is it a bit like fcntl in UNIX …. Under *NIX, you can see whether pcntl module is turned on or not by using php -m at the terminal command line.

Therefore, we will only talk about php’s multi-processes, while php’s multi-threads will be put aside for the time being.

Note: Do not use php multiprocesses in apache or fpm environments, which will have unpredictable consequences.

A process is an instance of program executionFor example, there is a program called “virus. exe”. this program is usually stored on the hard disk in the form of a file. when you double-click it to run, it will form a process of the program. Each process will be assigned a unique non-negative integer to mark the process. This number is called the process ID. When the process is killed or terminated, its process ID will be recycled by the system and then allocated to the new remaining processes.

Having said so much, what’s the use of this ghost? I usually use CI and Yi to write CURD, which has nothing to do with this. In fact, if you know APACHE PHP MOD or FPM, you know that these things are implemented in multiple processes. Take FPM as an example. Generally, nginx acts as an http server to block the front. Static file requests are handled by nginx itself, while php dynamic requests are forwarded to the php-fpm process for processing. If your php-fpm configuration has only 5 processes running, and if it takes 1 second to process any user’s request, then 5 fpm processes can only handle 5 user’s requests in 1 second at most. So the conclusion is: if you want to work faster and more in a unit of time, you need more processes. In a word, multiple processes can speed up the task processing.

In php, we use pcntl_fork () to create multiple processes (in C language programming of *NIX system, existing processes generate new processes by calling fork function). The new process from fork becomes the child process, the original process becomes the parent process, and the child process has a copy of the parent process. Note here:

  • The child process shares the program body segment with the parent process
  • A child process has a copy of the parent process’s data space and heap, stack, note that it is a copy, not a share.
  • The parent process and the child process will continue to execute the program code after fork
  • After fork, whether the parent process executes first or the child process executes first cannot be confirmed, depending on system scheduling (depending on belief)

It is said here that the child process has the data space of the parent process and copies of the heap and stack. In fact, in most implementations, it is not really a complete copy. More importantly, COW(Copy On Write) is adopted to save storage space. In short, if neither the parent process nor the child process modifies these data, heap, and stack, then the parent process and child process temporarily share the same data, heap, and stack. Copying occurs only when a parent process or a child process attempts to modify data, heap, or stack. This is called copy-on-write operation.

After calling pcntl_fork (), the function returns two values. Returns the process ID of the child process in the parent process and the number 0 in the child process itself. Since multi-process cannot run normally in apache or fpm environment, we must execute the following php code in php cli environment.

The first section of code, let’s explain after the program from pcntl_fork (), the parent process and the child process will each continue to execute code down:

<?php
        $pid = pcntl_fork();
        if( $pid > 0 ){
          echo "我是父亲".PHP_EOL;
        } else if( 0 == $pid ) {
          echo "我是儿子".PHP_EOL;
        } else {
          echo "fork失败".PHP_EOL;
        }

Save the file as a test.php and execute it using cli. The result is shown in the following figure:

The second code is used to explain that the child process has a copy of the parent process’s data instead of sharing it:

<?php
        // 初始化一个 number变量 数值为1
        $number = 1;
        $pid = pcntl_fork();
        if( $pid > 0 ){
          $number += 1;
          echo "我是父亲,number+1 : { $number }".PHP_EOL;
        } else if( 0 == $pid ) {
          $number += 2;
          echo "我是父亲,number+2 : { $number }".PHP_EOL;
        } else {
          echo "fork失败".PHP_EOL;
        }

The third code is easy to confuse people. pcntl_fork () works with the for loop to do something. The question arises: How many times will “son” be displayed?

<?php
        for( $i = 1; $i <= 3 ; $i++ ){
            $pid = pcntl_fork();
            if( $pid > 0 ){
               // do nothing ...
            } else if( 0 == $pid ){
                echo "儿子".PHP_EOL;
            }
        }

The above code execution results are as follows:

A careful count turned out to show the “son” seven times. It’s strange, isn’t it three times? … …
Next, I will revise the code and combine it with the following code to think about why it was generated 7 times instead of 3 times.

<?php
        for( $i = 1; $i <= 3 ; $i++ ){
            $pid = pcntl_fork();
            if( $pid > 0 ){
               // do nothing ...
            } else if( 0 == $pid ){
                echo "儿子".PHP_EOL;
                exit;
            }
        }

The execution results are shown in the following figure:

As emphasized earlier:The parent process and the child process will continue to execute the program code after fork. I don’t want to explain it here. I really don’t understand it. I can draw and think about it myself.

In order to avoid writing smelly tail theory of Wen Er, here forced to break the article segmentation, the next chapter said zombie process and orphan process some grievances.