search
HomeBackend DevelopmentPHP ProblemDetailed explanation of pcntl_fork in php

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();

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);

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment