
The pcntl_fork() function is a function used in the php pcntl module to create processes. This article will lead you into the mysterious world of PHP, let’s go together!!
The PCNTL_fork() function is a function used in the PHP PCNTL module to create processes. (does not support Windows)
As for how to install and enable the PHPpcntl extension, it will not be introduced here, only the analysis of the pcntl_fork() function itself.

$ one = 123;$ one ++;$ two = time();$ pid = [];$ pid = pcntl_fork();$ three = time();
When the pcntl_fork() function is executed, a child process is created. The child process will copy all the data, code, and state of the current process, which is the parent process.
1. After the creation of a child process by pcntl_fork() is successful, it returns the child process number within the parent process and 0 within the child process. If it fails, it returns -1
2. Child processes will copy the code and data of the parent process. It means that the code and data owned by the child and the parent process will be identical.
3. Key point: The child process will copy the state of the parent process, so there is the example code above: if pcntl_fork is executed on the fifth line, the created child process will also start executing from the fifth line. The child process copied the data and code again. So, within the child process, there are also variables such as $one, $two, etc
For ($i=0; $i<3; $i++){
$ pid = pcntl_fork(); }sleep(30);
/*!
Topic Name: RiPro-V5
Theme URI: https://ritheme.com /
Author: RiTheme.com
Author URI: https://ritheme.com /
Introduction: The new V5 version of RiPro theme is an excellent, powerful, easy to manage, and modern WordPress virtual resource mall theme. Support modular layout of homepage and WP native small tool modular homepage with drag and drop settings, making your website design experience more comfortable. It also supports advanced filtering, built-in member ecosystem, super full payment interface and many other functions, which can be implemented without relying on plugins. In addition, the theme also supports various functions such as card encryption, recharge, internal currency, membership, promotion commission, statistics, custom currency, custom member identification, and support for custom SEO information, providing a more complete solution for your website.
Version: 5.0.0
*/
So: How many child processes will actually be generated in the above loop? The answer is 7. Under Linux, using the ps command will show 8 processes (1 parent process, 7 child processes)
Reason: When i=0, the parent process creates a child process 0, which will continue to execute the loop. Create your own child process. Similarly, when i=0, a child process 0 is created, and at this time, the child process will continue to execute the loop. Create your own child process. Similarly, when i=0, a child process 0 is created, and at this time, the child process will continue to execute the loop. Create your own child process. Similarly, when i=1, it will also be like this

Comments (0)