Detailed explanation of pcntl_fork in php

醉折花枝作酒筹
Release: 2023-03-09 15:54:01
forward
2003 people have browsed it

This article will introduce to you "pcntl_fork" in php. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of pcntl_fork in php

The pcntl_fork() function is the function used to create a process in the php-pcntl module. (Windows is not supported)

As for how to install and enable the php_pcntl extension, I will not introduce it here. I will only analyze the pcntl_fork() function itself.

$one = 123;
$one++;
$two = time();
$pid = [];
$pid = pcntl_fork();
$three = time();
Copy after login

When: pcntl_fork() function is executed, a child process will be created. The child process will copy everything about the current process, that is, the parent process: data, code, and state.

When pcntl_fork() successfully creates a child process, the child process number is returned in the parent process, 0 is returned in the child process, and -1 is returned in case of failure

The child process will copy the parent process code, data. Then it means: the code and data owned by the child and parent processes will be exactly the same.

Key point: The child process will copy the status of the parent process, then there is the above sample code: In the fifth line, pcntl_fork is executed, then create The subprocess of the code is also executed from the fifth line. The child process copied the data and code. Therefore, in the sub-process, variables such as $one, $two and so on exist in the same way.

for ($i = 0; $i < 3; $i++) {
    $pid = pcntl_fork();
}
sleep(30);
Copy after login

So: How many sub-processes will actually be generated by the above for loop? The answer is 7. Under Linux, you can use the ps command to see 8 processes (1 parent process, 7 child processes)

Reason: The parent process creates a child process when i = 0 0, the child process at this time will continue to execute the loop. Create your own child process. In the same way: when i=0, a child process 0 is created. At this time, the child process will continue to execute the loop. Create your own child process. In the same way: when i=0, a child process 0 is created. At this time, the child process will continue to execute the loop. Create your own child process. In the same way: this will also happen when i=1...

Recommended learning: php video tutorial

The above is the detailed content of Detailed explanation of pcntl_fork in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!