Home > Backend Development > PHP Problem > What is the function of pcntl_fork in php

What is the function of pcntl_fork in php

醉折花枝作酒筹
Release: 2023-03-09 19:42:01
forward
2217 people have browsed it

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

What is the function 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.

1. 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 if it fails

2. Child process The code and data of the parent process will be copied. Then it means: the code and data owned by the child and parent processes will be exactly the same.

3. Key point: The child process will copy the status of the parent process, so there is the above sample code: pcntl_fork is executed on the fifth line, then the code of the created child process 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 What is the function 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