当前位置:网站首页>php pcntl_fork 创建多个子进程解析
php pcntl_fork 创建多个子进程解析
2022-06-11 20:34:00 【天下皆白_唯我独黑】
pcntl_fork()函数是php-pcntl模块中用于创建进程的函数。(不支持windows)
pcntl_fork — 在当前进程当前位置产生分支(子进程)。译注:fork是创建了一个子进程,父进程和子进程 都从fork的位置开始向下继续执行,不同的是父进程执行过程中,得到的fork返回值为子进程 号,而子进程得到的是0。
值得注意的是,并不是pcntl_fork在子进程调用时会返回0,而是当程序执行到pcntl_fork时会产生一个新的分支,主进程继续执行主进程的代码,同时会接收到pcntl_fork的返回值为新分支的pid,同时新的分支也会从当前分支的位置继续执行同样的代码,区别就是他得到的返回值是0,可以以此判断是否是新产生的分支。
官网示例:
<?php
$pid = pcntl_fork();
//父进程和子进程都会执行下面代码
if ($pid == -1) {
//错误处理:创建子进程失败时返回-1.
die('could not fork');
} else if ($pid) {
//父进程会得到子进程号,所以这里是父进程执行的逻辑
pcntl_wait($status); //等待子进程中断,防止子进程成为僵尸进程。
} else {
//子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
}
?>fork运行逻辑演示:
function text()
{
for ($i = 0; $i < 3; $i++) {
$pid = pcntl_fork();
echo "\$i($i)" . ' >> 主进程:' . posix_getpid() . " >> fork创建子进程:" . $pid . PHP_EOL;
}
}
思维导图
通过上面两个图可以很清晰的看到pcntl_fork的流程, 那么:可以看出上面的for循环,实际会产生多少个子进程?答案是7个,一共8个进程(1个父进程,7个子进程),像不像电影盗梦空间里的嵌套梦境?看到这里可以发现及时是子进程也可以进行 pcntl_fork 操作,所以一般为了使程序正常的运转会通过以下的写法来完成多进程操作,当执行完逻辑,需要使用die/return/exit 来结束子进程继续嵌套
function text1()
{
$child_process =[];
for ($i = 0; $i < 3; $i++) {
$pid = pcntl_fork();
//父进程和子进程都会执行下面代码
if ($pid == -1) {
//错误处理:创建子进程失败时返回-1.
die('could not fork');
} else if ($pid) {
//父进程会得到子进程号,所以这里是父进程执行的逻辑
$child_process[] = $pid;
} else {
//子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
echo '1'.PHP_EOL;
return; //通过使用die | return | exit 来限制程序继续执行
}
}
while (count($child_process) > 0) {
foreach ($child_process as $key => $pid) {
$res = pcntl_waitpid($pid, $status, WNOHANG);
//-1代表error, 大于0代表子进程已退出,返回的是子进程的pid,非阻塞时0代表没取到退出子进程
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 "子进程".PHP_EOL;
echo time() . "-" . PHP_EOL;
sleep(10);
echo 1;
return;
}
}
}最后分享几篇好文章:
边栏推荐
- Date of SQL optimization_ Format() function
- [Err] 1045 - Access denied for user ‘root‘@‘%‘ (using password: YES)
- Chrome V8 source code 48 The secret of weak type addition,'+'source code analysis
- Oracle case: ora-00600: internal error code, arguments: [4187]
- In 2021, the global revenue of flexible fireproof sealant is about 755.2 million dollars, and it is expected to reach 1211.7 million dollars in 2028
- Tree Basics
- Interface isolation principle
- Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
- 2022-2028 global and Chinese thermopile and microbolometer infrared detector Market Status and future development trend
- Première formation sur les largeurs modernes
猜你喜欢

moderlarts第一次培训

The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)

桌面以及文件资源管理器无限重启的解决

12 date and time in R

Modelarts second job

Object storage of CEPH distributed storage

Full list! 2022 Times Asia University Rankings announced!

Modify appid of local wechat applet

7905 and TL431 negative voltage regulator circuit - regulator and floating circuit relative to the positive pole of the power supply

9 r remove missing values
随机推荐
Chrome V8 source code 48 The secret of weak type addition,'+'source code analysis
What is the essence and process of SCM development? Chengdu Automation Development Undertaking
Jmeter安装
Modify appid of local wechat applet
MySQL installation free configuration tutorial under Windows mysql-5.6.51-winx64 Zip version
unity里面使用opencv,qt建立plugin(一)
UDP、TCP
Edit the project steps to run QT and opencv in the clion
c语言程序设计知识点总结 01
11 r create random number
ICML 2022 | 基于结构化数据的异常检测再思考:我们究竟需要怎样的图神经网络?...
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
Capriccio in the Internet Age
[Sichuan University] information sharing of the first and second examinations
Implementation of SQL online editor based on Vue + codemirror
vectorDrawable使用报错
2022-2028 global and Chinese thermal ionization isotope mass spectrometer (TIMS) market status and future development trend
Current situation and future development trend of thermoelectric generator Market in the world and China from 2022 to 2028
Modelarts second job