<?php
if (! function_exists('pcntl_fork')) die('PCNTL functions not available on this PHP installation');
for ($i = 1; $i <= 5; ++$i) {
$pid = pcntl_fork();
if (!$pid) {
sleep(1);
print "In child $i\n";
for($b=0; $b<2000; $b++) {
sleep(1);
echo 'we are the child' . "$i\n";
}
exit($i);
}
}
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
echo "Child $status completed\n";
}
/*for($i = 0; $i<3; $i++) {
echo "-----------------------$i-------------------\n";
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
var_dump($pid);
// we are the parent
for($a = 0; $a<2000; $a++) {
sleep(1);
echo 'we are the parent' . "$a\n";
}
pcntl_wait($status); //Protect against Zombie children
} else {
var_dump($pid);
for($b=0; $b<2000; $b++) {
sleep(1);
echo 'we are the child' . "$b\n";
}
exit(0);
// we are the child
}
} */
PHP之pcntl_fork多进程
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 概述 最近在学习Replugin源码时,遇到了其中的多进程部分。由于太久没使用,有点生疏,刚好重拾总结下。AIDL...
- 无论在哪种编程语言中,多线程都是重中之重。所以说掌握多线程并发编程是一个优秀的程序员所必须的一项技能。虽然平时都有...