In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "PHP7 multi-process introduction and example source code". In daily operation, I believe many people have doubts about PHP7 multi-process introduction and example source code. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "PHP7 multi-process introduction and example source code". Next, please follow the editor to study!
Prepare for
We all know that PHP is executed by a single process, and PHP mainly relies on multiple processes of server or PHP-FPM and their reuse, but it is also significant for PHP to implement multiple processes, especially when dealing with large amounts of data or running background DEMON daemons in background Cli mode, needless to say the advantages of multiple processes.
The multithreading of PHP has also been mentioned, but the problem of resource sharing and allocation of multithreading within the process is difficult to solve. PHP also has an extension pthreads that multiple threads want to turn off, but it is said to be unstable and requires a thread-safe environment that is not used much.
In the past, a great god in the PHP group once instructed that the background PHP must be multi-process if you want to advance, and it just so happens that the daemon process in the company also applied the multi-process of PHP, combined with Google's various materials and manuals, finally understood the multi-process, and wrote a small demo (realized on the linux system). Use this article to summarize, if there are mistakes or omissions, thank you for raising.
To implement the multiprocess of PHP, we need two extensions, pcntl and posix, and the installation method is not discussed here.
In php we use pcntl_fork () to create multiple processes (in the C language programming of * NIX systems, existing processes generate new processes by calling the fork function). When fork comes out, the new process 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
The child process has the data space of the parent process and a copy of the stack and stack. Note that it is a copy, not a shared one.
The parent and child processes will continue to execute the program code after fork
After fork, it is impossible to confirm whether the parent process executes first or the child process executes first, depending on the system scheduling (depending on belief)
It is said here that the child process has the parent process data space and copies of the heap and stack, which in fact is not a true complete copy in most implementations. What's more, the technology of COW (Copy On Write) is used to save storage space. To put it simply, if neither the parent process nor the child process modifies the data, stack, or stack, then the parent process and the child process temporarily share the same data, stack, and stack. A copy operation occurs only when the parent or child process attempts to modify the data, stack, or stack, which is called write-time replication.
After calling pcntl_fork (), the function returns two values. The process ID that returns the child process in the parent process returns the number 0 within the child process itself. Since multiple processes do not work properly in apache or fpm, you must execute code in php cli.
Create a child process
Creating a PHP child process is the beginning of multiple processes, and we need the pcntl_fork () function
Detailed explanation of fork function
Pcntl_fork ()-generates branches (child processes) at the current location of the current process. After this function creates a new child process, the child process will inherit the current context of the parent process and continue to execute downward from the pcntl_fork () function like the parent process, except that the return value of pcntl_fork () is different, so we can distinguish the parent process from the child process by judging the return value, and assign the parent process and the child process to do different logical processing.
When the pcntl_fork () function executes successfully, it returns the process id (pid) of the child process in the parent process, because the pid of the system's initial process init process is 1, and the pid of the later generated process will be larger than this process, so we can confirm that the current process is the parent process by judging that the return value of pcntl_fork () is greater than 1. In a child process, the return value of this function will be a fixed value of 0, and we can also determine the child process by judging that the return value of pcntl_fork () is 0; while when the pcntl_fork () function fails, it will return-1 in the parent process, and of course no child process will be generated.
Fork process instance
Fork child process
$ppid = posix_getpid (); $pid = pcntl_fork (); if ($pid = =-1) {throw new Exception ('fork child process fail');} elseif ($pid > 0) {cli_set_process_title ("I am the parent process,pid is: {$ppid}."); sleep (30);} else {$cpid = posix_getpid (); cli_set_process_title ("I am the process of {$ppid} son, my process pid is: {$cpid}."); sleep (30) }
Description:
Posix_getpid (): returns the current process id
Cli_set_process_title ('process name'): give the current process a loud name.
By running this example, we can see the current two PHP processes.
Www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ ps aux | grep-v grep | grep I www 18026 0.5 1.2 204068 25772 pts/0 S + 14:08 0:00 I am the father process,pid is: 18026.www 18027 18027 204068 6640 pts/0 S + 14:08 0:00 my 18026 son process, my process pid is: 18027.
In the first piece of code, after the program starts pcntl_fork (), the parent and child processes will each continue to execute the code:
$pid = pcntl_fork (); if ($pid > 0) {echo "I am the father" .PHP _ EOL;} else if (0 = $pid) {echo "I am the son" .PHP _ EOL;} else {echo "fork failed" .PHP _ EOL;}
Results:
Www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ php 123.php, I'm the father. I'm the son.
The second piece of code states that the child process has a copy of the data of the parent process, rather than sharing it:
/ / initialize a number variable with the value of 1$ number = 1 leading pid = pcntl_fork (); if ($pid > 0) {$number+ = 1; echo "I am the father, number+1: {$number}". PHP_EOL;} else if (0 = $pid) {$number+ = 2; echo "I am a son, number+2: {$number}". PHP_EOL;} else {echo "fork failed". PHP_EOL;}
Result
Www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ php 1234.php I am the father, number+1: {2} I am the son, number+2: {3} so far, on the "PHP7 multi-process introduction and example source code" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.