from : http://www.php.net/manual/zh/function.popen.php
<?php
function execInBackground ( $cmd ) {if ( substr ( php_uname (), 0 , 7 ) == "Windows" ){
pclose ( popen ( "start /B " . $cmd , "r" ));
}
else {
exec ( $cmd . " > /dev/null &" );
}
}
?>
linux下多进程实现:
from: http://www.kuqin.com/web/20110724/92668.html
<?php
//配合pcntl_signal使用
declare(ticks=1);
//最大的子进程数量
$max = 5;
//当前的子进程数量
$child = 0;
//当子进程退出时,会触发该函数
function sig_handler($sig) {
global $child;
switch($sig) {
case SIGCHLD:
echo 'SIGCHLD received'."\n";
$child--;
}
}
//注册子进程退出时调用的函数
pcntl_signal(SIGCHLD, "sig_handler");
while(true) {
$child++;
/**
* 这个函数会返回两个值,一个为0,表示子进程;一个为正整数表示子进程的id
* 所以if和else里的两段代码都会执行
* if里的代码是父进程执行的
* else里的代码是子进程执行的
*/
$pid = pcntl_fork();
if ($pid) {
//这里是父进程执行的代码
//如果子进程数超过了最大值,则挂起父进程
//也就是说while语句不会继续执行
if ($child >= $max) {
pcntl_wait($status);
}
}
else {
//这里是子进程执行的代码
//如果要执行其他命令的话,使用pcntl_exec
echo "starting new child | now we have $child child process\n";
sleep(rand(3, 5));
exit;
}
}