As follows:
Copy codeThe Code is as follows:
/**
* Entry functions
* Save this file as ProcessOpera. php
* Run/usr/local/php/bin/php ProcessOpera. php &
* View the process ps aux | grep php
*/
ProcessOpera ("runCode", array (), 8 );
/**
* Run Code
*/
Function runCode ($ opt = array ()){
// Code to be run in the daemon
}
/**
* $ Func is the name of the function for the sub-process to execute specific things.
* $ Opt is in the format of $ func parameter Array
* $ Number of child processes whose pNum is 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 );
}
}
}