As shown below:
Copy the code The code is as follows:
/**
* Entry function
* Save this file as ProcessOpera.php
* Run /usr/local/php/bin/php ProcessOpera.php &
in terminal php
*/
ProcessOpera("runCode", array(), 8);
/**
* run Code
*/
function runCode($opt = array()) {
//Code that needs to be run in the daemon process
}
/**
* $func is the function name of the child process to perform specific things
* $opt is the parameter of $func in array form
* $pNum is the number of child processes for fork
*/
function ProcessOpera($func, $opts = array(), $pNum = 1) {
while(true) {
$pid = pcntl_fork();
if($pid == -1) {
exit("pid fork error");
}
if($pid) {
static $execute = 0;
$execute++;
if($execute >= $pNum) {
pcntl_wait($status);
$execute--;
}
} else {
while(true) {
//somecode
$func($opts);
sleep(1);
}
exit(0);
}
}
}
http://www.bkjia.com/PHPjc/327645.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327645.htmlTechArticle is as follows: Copy the code as follows: /** * Entry function* Save this file as ProcessOpera.php * Run /usr/local/php/bin/php ProcessOpera.php nbsp;* View the process...