当前位置:网站首页>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 :
边栏推荐
- [Err] 1045 - Access denied for user ‘root‘@‘%‘ (using password: YES)
- In 2021, the global revenue of minoxidil will be about 1035million US dollars, and it is expected to reach 1372.6 million US dollars in 2028
- A Mechanics-Informed Artificial Neural Network Approach in Data-Driven Constitutive Modeling 学习
- 浅谈UGUI中Canvas RectTransform的Scale
- Oracle case: ora-00600: internal error code, arguments: [4187]
- Research and Analysis on the market status of polybutene-1 in China from 2021 to 2027 and forecast report on its development prospect
- 周刊02|不瞒你说,我其实是MIT的学生
- Object storage of CEPH distributed storage
- 模拟Oracle锁等待与手工解锁
- Final examination of Dialectics of nature 1
猜你喜欢

Flutter Doctor affiche les solutions que xcode n'a pas installées

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

Richter's substitution principle

Modify appid of local wechat applet

周刊02|不瞒你说,我其实是MIT的学生

Redis第四话 -- redis高性能原理(多路复用)和高可用分析(备份、主从)

MySQL installation free configuration tutorial under Windows mysql-5.6.51-winx64 Zip version

Yintai department store and Taobao tmall jointly create a green fashion show to help "carbon neutrality"

修改本地微信小程序的AppID

Text to speech small software
随机推荐
unity package manager starting server stuck(Unity啟動卡在starting server,然後報錯)
Chrome V8 source code 48 The secret of weak type addition,'+'source code analysis
Title does not display after toolbar replaces actionbar
Force buckle 6 Zigzag transformation
Première formation sur les largeurs modernes
UDP、TCP
Interpretation of OCP function of oceanbase Community Edition
What is the essence and process of SCM development? Chengdu Automation Development Undertaking
周刊02|不瞒你说,我其实是MIT的学生
Toolbar替换ActionBar后Title不显示
C deep copy
[Err] 1045 - Access denied for user ‘root‘@‘%‘ (using password: YES)
A brief talk on shutter button
2022-2028 global and Chinese thermocouple sensor market status and future development trend
Rule engine mode Net open source project case
8 r subset
Implementation of SQL online editor based on Vue + codemirror
Current situation and future development trend of thermoelectric generator Market in the world and China from 2022 to 2028
输入值“18-20000hz”错误,设置信息不完整,请选择单位
ubantu1804 两个opencv版本共存