Explore PHP multi-process and PHP process
Preparation
We all know that PHP is executed by a single process. PHP mainly depends on the multi-process of the server or PHP-FPM and the reuse of their processes, but PHP also has great significance in implementing multi-process, especially when processing a large amount of data in the background Cli mode or running the background DEMON daemon process, the advantages of multi-process are needless to say.
Multithreading in PHP has also been mentioned, but it is difficult to solve the problem of multi-threaded resource sharing and allocation in the process. PHP also has the extension pthreads to be turned off by multiple threads, But it is said to be unstable, and the environment is required to be thread-safe, which is not used much.
In the past, a great god in the PHP Group once instructed that PHP in the background would inevitably avoid the possibility of multi-process advancement, and the company's daemon process also applied PHP's multi-process, combined with various documents and manuals of Gu Ge, I finally understood the multi-process and wrote a small demo (implemented on linux). I will summarize it in this article. If there are any mistakes or omissions, thank you for your suggestion.
To implement PHP multi-process, we need two extensions: pcntl and posix. The installation method is not described here.
Create a sub-process
Creating a PHP sub-process is the beginning of a multi-process. We needpcntl_fork()Function;
Fork function details
Pcntl_fork ()-generate a branch (sub-process) at the current position of the current process ). After a new sub-process is created for this function, the sub-process inherits the current context of the parent process and continues to run down from the pcntl_fork () function like the parent process, only when the returned pcntl_fork () values are different, we can distinguish the Parent and Child processes from the returned values, and allocate the Parent and Child processes for different logical processing.
When the pcntl_fork () function is successfully executed, the child process id (pid) is returned to the parent process because the system's initial process init process pid is 1, later, the pid of the generated process will be greater than this process, so we can determine that the return value of pcntl_fork () is greater than 1 to ensure that the current process is a parent process;
In a sub-process, the return value of this function is a fixed value of 0. We can also determine the sub-process by judging that the return value of pcntl_fork () is 0;
When the execution of the pcntl_fork () function fails,-1 is returned to the parent process, and no child process is generated.
Fork process instance
The following is a simple example of a fork sub-process:
$ Ppid = posix_getpid (); $ pid = pcntl_fork (); if ($ pid =-1) {throw new Exception ('fork subprocess failed! ');} Elseif ($ pid> 0) {cli_set_process_title ("I am a parent process, and my process id is {$ ppid }.");
Sleep (30); // keep it for 30 seconds, ensure that it can be found by ps} else {$ cpid = posix_getpid (); cli_set_process_title ("I Am a subprocess of {$ ppid, my process id is {$ cpid }. "); sleep (30 );}
Here we will introduce two functions:
posix_getpid(): Obtain the pid of the current process;
Cli_set_process_title ('loud name '): Give the current process a loud name.
Run this example to see the current two PHP processes.
Manage Sub-Processes
After creating a process, how can we manage the child process? Use signals.
In computer science, signals are a restricted way of inter-process communication in Unix, Unix-like, and other POSIX-compatible operating systems. It is an asynchronous notification mechanism to remind the process that an event has occurred.
Distribution Signal Processor
By receiving signals from sub-processes in the parent process, we can determine the sub-process status and manage the sub-processes.
We need to usepcntl_signal()Functions andpcntl_signal_dispatch()Function to install a signal processor for each sub-process.
Pcntl_signal (int $ signo, callback $ handler) installs a signal processor; $ signo is a constant of the signal to be processed, and callback is its processing function pcntl_signal_dispatch () calls each wait signal through pcntl_signal () installed Processor
Common PHP signal constants include:
When the SIGCHLD sub-process exits as a zombie, it will send this signal to the parent process. The SIGHUP process suspends the SIGTEM process to terminate... // others can be found in the Manual.
After the signal processor is installed and called, once the child process returns the corresponding signal to the parent process, the parent process can call the corresponding callback function to process the child process;
Process sub-Processes
Sub-processes can be processed in the following ways:
posix_kill(): This function does not act as the name suggests. It sends a signal to the sub-process to operate the sub-process. When necessary, it can choose to send the process termination signal to the sub-process to stop the sub-process;
pcntl_waitpid(): Waits for or returns the fork sub-process status. If the specified sub-process has exited (also known as a zombie process) during this function call, this function will return immediately, and release all system resources of the sub-process. This process can avoid the sub-process from becoming a zombie process, resulting in a waste of system resources;
The following is a prototype of the two functions:
Bool posix_kill (int $ pid, int $ sig) // send $ sig signals to processes with a process id of $ pid. Common signals of $ sig are as follows: int pcntl_waitpid (int $ pid, int & $ status [, int $ options = 0]) // suspends the execution of the current process until the process with the process number $ pid exits (if $ pid is-1, wait for any sub-process );
Summary
This is the basic use of PHP multi-process. If you are interested, you can write a demo and try it out.
Finally, I will post the advantages of PHP multi-process mentioned by large:
By the way, there is another example, which can be uploaded to github. You are welcome to pay attention to> pillow book.
References:
Multi-process applications in php cli Mode
PHP multi-process programming-PureWeber-pure Internet
Write the daemon process in php-hu Chao blog