<?php
$pid = pcntl_fork();
if($pid == -1){
//创建失败
die('could not fork');
}
else{
if($pid){
//从这里开始写的代码是父进程的
exit("parent!");
}
else{
//子进程代码,为防止不停的启用子进程造成系统资源被耗尽的情况,一般子进程代码运行完成后,加入exit来确保子进程正常退出。
exit("child");
}
}
?>
#如果获得的总数小于或等于0,等待60秒,并退出
if ($count <= 0)
{
sleep(60);
exit;
}
#如果大于1000,计算需要起的进程数
if ($count > 1000)
{
$cycleSize = ceil($count/1000);
}
else
{
$cycleSize = 1;
}
for ($i=0; $i<$cycleSize; $i++)
{
$pid = pcntl_fork();
if($pid == -1)
{
break;
}
else
{
if($pid)
{
#父进程获得子进程的pid,存入数组
$pidArr[] = $pid;
}
else
{
//开始发送,子进程执行完自己的任务后,退出。
exit;
}
}
}
while(count($pidArr) > 0)
{
$myId = pcntl_waitpid(-1, $status, WNOHANG);
foreach($pidArr as $key => $pid)
{
if($myId == $pid) unset($pidArr[$key]);
}
}
<?php
// example of multiple processes
date_default_timezone_set( 'Asia/Chongqing');
echo "parent start, pid ", getmypid(), "\n" ;
beep();
for ($i=0; $i<3; ++$i){
$pid = pcntl_fork();
if ($pid == -1){
die ("cannot fork" );
} else if ($pid > 0){
echo "parent continue \n";
for ($k=0; $k<2; ++$k){
beep();
}
} else if ($pid == 0){
echo "child start, pid ", getmypid(), "\n" ;
for ($j=0; $j<5; ++$j){
beep();
}
exit ;
}
}
// ***
function beep(){
echo getmypid(), "\t" , date( 'Y-m-d H:i:s', time()), "\n" ;
sleep(1);
}
?>
#php -f test.php
parent start, pid 1793 1793 2013-01-14 15:04:17 parent continue 1793 2013-01-14 15:04:18 child start, pid 1794 1794 2013-01-14 15:04:18 1794 2013-01-14 15:04:19 1793 2013-01-14 15:04:19 1794 2013-01-14 15:04:20 parent continue 1793 2013-01-14 15:04:20 child start, pid 1795 1795 2013-01-14 15:04:20 17931794 2013-01-14 15:04:212013-01-14 15:04:21 1795 2013-01-14 15:04:21 1794 2013-01-14 15:04:22 1795 2013-01-14 15:04:22 parent continue 1793 2013-01-14 15:04:22 child start, pid 1796 1796 2013-01-14 15:04:22 1793 2013-01-14 15:04:23 1796 2013-01-14 15:04:23 1795 2013-01-14 15:04:23 1795 2013-01-14 15:04:24 1796 2013-01-14 15:04:24 1796 2013-01-14 15:04:25 1796 2013-01-14 15:04:26
$pid = pcntl_fork();
if ($pid == -1){
...
} else if ($pid > 0){
echo "parent continue \n";
pcntl_wait($status);
for ($k=0; $k<2; ++$k){
beep();
}
} else if ($pid == 0){
...
}
#php -f test.php
parent start, pid 1807 1807 2013-01-14 15:20:05 parent continue child start, pid 1808 1808 2013-01-14 15:20:06 1808 2013-01-14 15:20:07 1808 2013-01-14 15:20:08 1808 2013-01-14 15:20:09 1808 2013-01-14 15:20:10 1807 2013-01-14 15:20:11 1807 2013-01-14 15:20:12 parent continue child start, pid 1809 1809 2013-01-14 15:20:13 1809 2013-01-14 15:20:14 1809 2013-01-14 15:20:15 1809 2013-01-14 15:20:16 1809 2013-01-14 15:20:17 1807 2013-01-14 15:20:18 1807 2013-01-14 15:20:19 child start, pid 1810 1810 2013-01-14 15:20:20 parent continue 1810 2013-01-14 15:20:21 1810 2013-01-14 15:20:22 1810 2013-01-14 15:20:23 1810 2013-01-14 15:20:24 1807 2013-01-14 15:20:25 1807 2013-01-14 15:20:26
<?php
// example of multiple processes
date_default_timezone_set( 'Asia/Chongqing');
declare (ticks = 1);
pcntl_signal(SIGCHLD, "garbage" );
echo "parent start, pid ", getmypid(), "\n" ;
beep();
for ($i=0; $i<3; ++$i){
$pid = pcntl_fork();
if ($pid == -1){
die ("cannot fork" );
} else if ($pid > 0){
echo "parent continue \n";
for ($k=0; $k<2; ++$k){
beep();
}
} else if ($pid == 0){
echo "child start, pid ", getmypid(), "\n" ;
for ($j=0; $j<5; ++$j){
beep();
}
exit (0);
}
}
// parent
while (1){
// do something else
sleep(5);
}
// ***
function garbage($signal){
echo "signel $signal received\n" ;
while (($pid = pcntl_waitpid(-1, $status, WNOHANG))> 0){
echo "\t child end pid $pid , status $status\n" ;
}
}
function beep(){
echo getmypid(), "\t" , date( 'Y-m-d H:i:s', time()), "\n" ;
sleep(1);
}
?>
#php -f test.php &
parent start, pid 2066
2066 2013-01-14 16:45:34
parent continue
2066 2013-01-14 16:45:35
child start, pid 2067
2067 2013-01-14 16:45:35
20662067 2013-01-14 16:45:362013-01-14 16:45:36
2067 2013-01-14 16:45:37
parent continue
2066 2013-01-14 16:45:37
child start, pid 2068
2068 2013-01-14 16:45:37
2067 2013-01-14 16:45:38
2068 2013-01-14 16:45:38
2066 2013-01-14 16:45:38
parent continue
2066 2013-01-14 16:45:40
child start, pid 2069
2069 2067 2013-01-14 16:45:40
2013-01-14 16:45:40
2068 2013-01-14 16:45:40
2066 2013-01-14 16:45:41
2069 2013-01-14 16:45:41
2068 2013-01-14 16:45:41
signel 17 received
child end pid 2067, status 0
2069 2013-01-14 16:45:42
2068 2013-01-14 16:45:42
2069 2013-01-14 16:45:43
signel 17 received
child end pid 2068, status 0
2069 2013-01-14 16:45:44
signel 17 received
child end pid 2069, status 0
pcntl_waitpid(-1, $status, WNOHANG) $status
<?php
date_default_timezone_set( 'Asia/Chongqing');
$tasks = array(
'http://localhost/feedbowl/t2.php?job=task1',
'http://localhost/feedbowl/t2.php?job=task2',
'http://localhost/feedbowl/t2.php?job=task3'
);
$mh = curl_multi_init();
foreach ($tasks as $i => $task){
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_URL, $task);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $ch[$i]);
}
do {$mrc = curl_multi_exec($mh,$active); } while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {$mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
// completed, checkout result
foreach ($tasks as $j => $task){
if (curl_error($ch[$j])){
echo "task ${j} [$task ] error " , curl_error($ch[$j]), "\r\n" ;
} else {
echo "task ${j} [$task ] get: \r\n" , curl_multi_getcontent($ch[$j]), "\r\n" ;
}
}
?>
<?php
date_default_timezone_set( 'Asia/Chongqing');
echo "child start, pid ", getmypid(), "\r\n" ;
for ($i=0; $i<5; ++$i){
beep();
}
exit (0);
// ***
function beep(){
echo getmypid(), "\t" , date('Y-m-d H:i:s' , time()), "\r\n";
sleep(1);
}
?>
#php -f test1.php &
task 0 [http://localhost/feedbowl/t2.php?job=task1] get: child start, pid 5804 5804 2013-01-15 20:22:35 5804 2013-01-15 20:22:36 5804 2013-01-15 20:22:37 5804 2013-01-15 20:22:38 5804 2013-01-15 20:22:39 task 1 [http://localhost/feedbowl/t2.php?job=task2] get: child start, pid 5804 5804 2013-01-15 20:22:35 5804 2013-01-15 20:22:36 5804 2013-01-15 20:22:37 5804 2013-01-15 20:22:38 5804 2013-01-15 20:22:39 task 2 [http://localhost/feedbowl/t2.php?job=task3] get: child start, pid 5804 5804 2013-01-15 20:22:35 5804 2013-01-15 20:22:36 5804 2013-01-15 20:22:37 5804 2013-01-15 20:22:38 5804 2013-01-15 20:22:39
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有