当前位置:网站首页>php pcntl_ Fork create multiple child process resolution
php pcntl_ Fork create multiple child process resolution
2022-06-11 20:37:00 【The world is white_ I am the only one】
pcntl_fork() The function is php-pcntl The function used to create the process in the module .( I won't support it windows)
pcntl_fork — Branch at the current position of the current process ( Subprocesses ). Translation notes :fork It's creating a subprocess , Parent and child processes From the fork Start from the position of... And continue down , The difference is that during the execution of the parent process , Got fork The return value is the child process Number , And what the subprocess gets is 0.
It is worth noting that , Not at all pcntl_fork When the subprocess is called, it will return 0, It's when the program runs to pcntl_fork Will generate a new branch , The main process continues to execute the code of the main process , Will also receive pcntl_fork The return value of the new branch is pid, At the same time, the new branch will continue to execute the same code from the location of the current branch , The difference is that the return value he gets is 0, This can be used to determine whether it is a new branch .
Official documents :PHP: pcntl_fork - Manual
Official website example :
<?php
$pid = pcntl_fork();
// Both the parent and child processes execute the following code
if ($pid == -1) {
// Error handling : Returns... When the creation of a child process fails -1.
die('could not fork');
} else if ($pid) {
// The parent process will get the child process number , So here is the logic of the parent process execution
pcntl_wait($status); // Wait for the subprocess to break , Prevent child processes from becoming zombie processes .
} else {
// Subprocesses get $pid by 0, So here's the logic of subprocess execution .
}
?>fork Operation logic demonstration :
function text()
{
for ($i = 0; $i < 3; $i++) {
$pid = pcntl_fork();
echo "\$i($i)" . ' >> The main process :' . posix_getpid() . " >> fork Create child process :" . $pid . PHP_EOL;
}
}
Mind mapping 
You can see clearly from the above two figures pcntl_fork The process of , that : You can see the above for loop , How many subprocesses will actually be generated ? The answer is 7 individual , altogether 8 A process (1 Parent process ,7 Subprocess ), Like the nested dreams in the movie inception ? You can find that sub processes can also be carried out in time pcntl_fork operation , Therefore, in order to make the program run normally, the following writing method will be used to complete the multi process operation , When the logic is executed , Need to use die/return/exit To end the child process and continue nesting
function text1()
{
$child_process =[];
for ($i = 0; $i < 3; $i++) {
$pid = pcntl_fork();
// Both the parent and child processes execute the following code
if ($pid == -1) {
// Error handling : Returns... When the creation of a child process fails -1.
die('could not fork');
} else if ($pid) {
// The parent process will get the child process number , So here is the logic of the parent process execution
$child_process[] = $pid;
} else {
// Subprocesses get $pid by 0, So here's the logic of subprocess execution .
echo '1'.PHP_EOL;
return; // By using die | return | exit To restrict program execution
}
}
while (count($child_process) > 0) {
foreach ($child_process as $key => $pid) {
$res = pcntl_waitpid($pid, $status, WNOHANG);
//-1 representative error, Greater than 0 Represents that the child process has exited , Returned by subprocess pid, When not blocked 0 Represents that the exit subprocess is not retrieved
if ($res == -1 || $res > 0)
unset($child_process[$key]);
}
sleep(1);
}
}
function main()
{
$i = 0;
while ($i < 3) {
$pid = pcntl_fork();
$i++;
if ($pid == 0) {
//echo " Subprocesses ".PHP_EOL;
echo time() . "-" . PHP_EOL;
sleep(10);
echo 1;
return;
}
}
}Finally, share some good articles :
边栏推荐
- Jmeter安装
- 15 r exercise
- 9 r remove missing values
- Ora-01089 ora-19809 ora-19815 exceeded the limit for recovering files
- UDP、TCP
- JSON introduction
- moderlarts第一次培训
- Richter's substitution principle
- Object storage of CEPH distributed storage
- Volcano engine, Alibaba cloud and Tencent cloud jointly released the 'ultra low delay' live broadcast technical standard
猜你喜欢

Role of RESNET residual block

Edit the project steps to run QT and opencv in the clion

Show your creativity and win the graphics card! Mmpose attitude estimation creative contest shocks

Modelarts second job

Unity package manager starting server stuck

ICML 2022 | 基于结构化数据的异常检测再思考:我们究竟需要怎样的图神经网络?...

Installation and performance test of Apache APIs IX on Amazon graviton3

27. this pointing problem

Flutter doctor 显示xcode没有安装的解决办法

moderlarts第一次培训
随机推荐
Calculation of Zeno paradox
R 16 basic exercises
2022-2028 global and Chinese thermocouple sensor market status and future development trend
模拟Oracle锁等待与手工解锁
Flutter doctor shows the solution that Xcode is not installed
Two end carry output character
Ora-01089 ora-19809 ora-19815 exceeded the limit for recovering files
秀创意,赢显卡!MMPose姿态估计创意大赛震撼来袭
Lanqi technology joins in, and dragon dragon dragon community welcomes leading chip design manufacturers again
11 r create random number
Introduction to jobrunr, a distributed background job scheduler - jaxenter
Shanghai internal promotion 𞓜 Yuyang teacher's research group of Shanghai Chizhi research institute recruits full-time researchers
Implementation of SQL online editor based on Vue + codemirror
12 date and time in R
Object storage of CEPH distributed storage
【数据可视化】Apache Superset 1.2.0教程 (三)—— 图表功能详解
15 r exercise
In 2021, the global revenue of Indian fragrant rice was about $12530 million, and it is expected to reach $21670 million in 2028
黑圆圈显示实现
桌面以及文件资源管理器无限重启的解决