<?php
header('content-type:text/html;charset=utf-8' );
// 必须加载扩展
if (!function_exists("pcntl_fork")) {
die("pcntl extention is must !");
}
//总进程的数量
$totals = 3;
// 执行的脚本数量
$cmdArr = array();
// 执行的脚本数量的数组
for ($i = 0; $i < $totals; $i++) {
$cmdArr[] = array("path" => __DIR__ . "/run.php", 'pid' =>$i ,'total' =>$totals);
}
/*
展开:$cmdArr
Array
(
[0] => Array
(
[path] => /var/www/html/company/pcntl/run.php
[pid] => 0
[total] => 3
)
[1] => Array
(
[path] => /var/www/html/company/pcntl/run.php
[pid] => 1
[total] => 3
)
[2] => Array
(
[path] => /var/www/html/company/pcntl/run.php
[pid] => 2
[total] => 3
)
)
*/
pcntl_signal(SIGCHLD, SIG_IGN); //如果父进程不关心子进程什么时候结束,子进程结束后,内核会回收。
foreach ($cmdArr as $cmd) {
$pid = pcntl_fork(); //创建子进程
//父进程和子进程都会执行下面代码
if ($pid == -1) {
//错误处理:创建子进程失败时返回-1.
die('could not fork');
} else if ($pid) {
//父进程会得到子进程号,所以这里是父进程执行的逻辑
//如果不需要阻塞进程,而又想得到子进程的退出状态,则可以注释掉pcntl_wait($status)语句,或写成:
pcntl_wait($status,WNOHANG); //等待子进程中断,防止子进程成为僵尸进程。
} else {
//子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
$path = $cmd["path"];
$pid = $cmd['pid'] ;
$total = $cmd['total'] ;
echo exec("/usr/bin/php {$path} {$pid} {$total}")."\n";
exit(0) ;
}
}
?>
<?php
declare(ticks = 1);
function signal_handler($signal) {
print "Caught SIGALRM\n";
pcntl_alarm(5);
}
pcntl_signal(SIGALRM, "signal_handler", true);
pcntl_alarm(5);
for(;;) {
}
?>
<?php $dir = '/home/shankka/'; $cmd = 'ls'; $option = '-l'; $pathtobin = '/bin/ls'; $arg = array($cmd, $option, $dir); pcntl_exec($pathtobin, $arg); echo '123'; //不会执行到该行 ?>
<?php
$pid = pcntl_fork();
//这里最好不要有其他的语句
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
pcntl_wait($status); //Protect against Zombie children
} else {
// we are the child
}
?>
<?php
$pid = pcntl_fork();
if($pid) {
pcntl_wait($status);
$id = getmypid();
echo "parent process,pid {$id}, child pid {$pid}\n";
}else{
$id = getmypid();
echo "child process,pid {$id}\n";
sleep(2);
}
?>
<?php
/**
* Project: Signfork: php多线程库
* File: Signfork.class.php
*/
class Signfork{
/**
* 设置子进程通信文件所在目录
* @var string
*/
private $tmp_path='/tmp/';
/**
* Signfork引擎主启动方法
* 1、判断$arg类型,类型为数组时将值传递给每个子进程;类型为数值型时,代表要创建的进程数.
* @param object $obj 执行对象
* @param string|array $arg 用于对象中的__fork方法所执行的参数
* 如:$arg,自动分解为:$obj->__fork($arg[0])、$obj->__fork($arg[1])...
* @return array 返回 array(子进程序列=>子进程执行结果);
*/
public function run($obj,$arg=1){
if(!method_exists($obj,'__fork')){
exit("Method '__fork' not found!");
}
if(is_array($arg)){
$i=0;
foreach($arg as $key=>$val){
$spawns[$i]=$key;
$i++;
$this->spawn($obj,$key,$val);
}
$spawns['total']=$i;
}elseif($spawns=intval($arg)){
for($i = 0; $i < $spawns; $i++){
$this->spawn($obj,$i);
}
}else{
exit('Bad argument!');
}
if($i>1000) exit('Too many spawns!');
return $this->request($spawns);
}
/**
* Signfork主进程控制方法
* 1、$tmpfile 判断子进程文件是否存在,存在则子进程执行完毕,并读取内容
* 2、$data收集子进程运行结果及数据,并用于最终返回
* 3、删除子进程文件
* 4、轮询一次0.03秒,直到所有子进程执行完毕,清理子进程资源
* @param string|array $arg 用于对应每个子进程的ID
* @return array 返回 array([子进程序列]=>[子进程执行结果]);
*/
private function request($spawns){
$data=array();
$i=is_array($spawns)?$spawns['total']:$spawns;
for($ids = 0; $ids<$i; $ids++){
while(!($cid=pcntl_waitpid(-1, $status, WNOHANG)))usleep(30000);
$tmpfile=$this->tmp_path.'sfpid_'.$cid;
$data[$spawns['total']?$spawns[$ids]:$ids]=file_get_contents($tmpfile);
unlink($tmpfile);
}
return $data;
}
/**
* Signfork子进程执行方法
* 1、pcntl_fork 生成子进程
* 2、file_put_contents 将'$obj->__fork($val)'的执行结果存入特定序列命名的文本
* 3、posix_kill杀死当前进程
* @param object $obj 待执行的对象
* @param object $i 子进程的序列ID,以便于返回对应每个子进程数据
* @param object $param 用于输入对象$obj方法'__fork'执行参数
*/
private function spawn($obj,$i,$param=null){
if(pcntl_fork()===0){
$cid=getmypid();
file_put_contents($this->tmp_path.'sfpid_'.$cid,$obj->__fork($param));
posix_kill($cid, SIGTERM);
exit;
}
}
}
?>
<?php
//.....
//需要安装pcntl的php扩展,并加载它
if(function_exists("pcntl_fork")){
//生成子进程
$pid = pcntl_fork();
if($pid == -1){
die('could not fork');
}else{
if($pid){
$status = 0;
//阻塞父进程,直到子进程结束,不适合需要长时间运行的脚本,可使用pcntl_wait($status, 0)实现非阻塞式
pcntl_wait($status);
// parent proc code
exit;
}else{
// child proc code
//结束当前子进程,以防止生成僵尸进程
if(function_exists("posix_kill")){
posix_kill(getmypid(), SIGTERM);
}else{
system('kill -9'. getmypid());
}
exit;
}
}
}else{
// 不支持多进程处理时的代码在这里
}
//.....
?>
如果不需要阻塞进程,而又想得到子进程的退出状态,则可以注释掉pcntl_wait($status)语句,或写成:
<?php
pcntl_wait($status, 1);
//或
pcntl_wait($status, WNOHANG);
?>
<?php pcntl_signal(SIGCHLD, SIG_IGN); $pid = pcntl_fork(); //....code ?>
#include "apue.h"
#include <sys/wait.h>
int main(void){
pid_t pid;
if ((pid = fork()) < 0){
err_sys("fork error");
} else if (pid == 0){ /**//* first child */
if ((pid = fork()) < 0){
err_sys("fork error");
}elseif(pid > 0){
exit(0); /**//* parent from second fork == first child */
}
/**
* We're the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here's where we'd continue executing, knowing that when
* we're done, init will reap our status.
*/
sleep(2);
printf("second child, parent pid = %d ", getppid());
exit(0);
}
if (waitpid(pid, NULL, 0) != pid) /**//* wait for first child */
err_sys("waitpid error");
/**
* We're the parent (the original process); we continue executing,
* knowing that we're not the parent of the second child.
*/
exit(0);
}
<?php
//.....
//需要安装pcntl的php扩展,并加载它
if(function_exists("pcntl_fork")){
//生成第一个子进程
$pid = pcntl_fork(); //$pid即所产生的子进程id
if($pid == -1){
//子进程fork失败
die('could not fork');
}else{
if($pid){
//父进程code
sleep(5); //等待5秒
exit(0); //或$this->_redirect('/');
}else{
//第一个子进程code
//产生孙进程
if(($gpid = pcntl_fork()) < 0){ ////$gpid即所产生的孙进程id
//孙进程产生失败
die('could not fork');
}elseif($gpid > 0){
//第一个子进程code,即孙进程的父进程
$status = 0;
$status = pcntl_wait($status); //阻塞子进程,并返回孙进程的退出状态,用于检查是否正常退出
if($status ! = 0) file_put_content('filename', '孙进程异常退出');
//得到父进程id
//$ppid = posix_getppid(); //如果$ppid为1则表示其父进程已变为init进程,原父进程已退出
//得到子进程id:posix_getpid()或getmypid()或是fork返回的变量$pid
//kill掉子进程
//posix_kill(getmypid(), SIGTERM);
exit(0);
}else{ //即$gpid == 0
//孙进程code
//....
//结束孙进程(即当前进程),以防止生成僵尸进程
if(function_exists('posix_kill')){
posix_kill(getmypid(), SIGTERM);
}else{
system('kill -9'. getmypid());
}
exit(0);
}
}
}
}else{
// 不支持多进程处理时的代码在这里
}
//.....
?>
<?php
//Action代码
public function createAction(){
//....
//将args替换成要传给insertLargeData.php的参数,参数间用空格间隔
system('php -f insertLargeData.php ' . ' args ' . '&');
$this->redirect('/');
}
?>
<?php
//Action代码
public function createAction(){
//....
//将args替换成要传给insertLargeData.php的参数,参数间用空格间隔
system('nohup php -f insertLargeData.php ' . ' args ' . '&');
$this->redirect('/');
}
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有