[PHP]
Guaranteed child Process Upper limit
if ($this->_maxfork >0 && $this->_children > $this->_maxfork)
{
Yii::log ("_children >". $this->_maxfork,clogger::level_warning,__method__);
$this->handler (SIGCHLD);
Usleep (200);
Continue
}
/**
* Monitoring Signal
* @param object Socket $clientt
* @return Boolean
*/
Public Function Handler ($signo) {
Yii::log ("Handler {$signo}", Clogger::level_info, __method__);
Switch (intval ($signo)) {
Case SIGCLD:
Case SIGCHLD:
Yii::log ("SIGCHLD Sub proccess", Clogger::level_trace, __method__);
Normal exit
DECLARE = 1, that means one signal is correspond multi-process die
while ($pid = pcntl_wait ($status, wnohang| wuntraced)) > 0) {
if (FALSE = = = pcntl_wifexited ($status)) {
Yii::log ("Sub proccess {$pid} exited unormally with code {$status}", clogger::level_warning, __method__);
} else {
Yii::log ("Sub proccess {$pid} exited normally", Clogger::level_info, __method__);
}
$this->_children--;
}
Break
Case SIGINT:
Case Sigquit:
Case SIGHUP:
Exception exit
$this->_cleanup ();
Exit (0);
Break
Default
Break
}
}
Guaranteed child Process Upper limit
if ($this->_maxfork >0 && $this->_children > $this->_maxfork)
{
Yii::log ("_children >". $this->_maxfork,clogger::level_warning,__method__);
$this->handler (SIGCHLD);
Usleep (200);
Continue
}
/**
* Monitoring Signal
* @param object Socket $clientt
* @return Boolean
*/
Public Function Handler ($signo) {
Yii::log ("Handler {$signo}", Clogger::level_info, __method__);
Switch (intval ($signo)) {
Case SIGCLD:
Case SIGCHLD:
Yii::log ("SIGCHLD Sub proccess", Clogger::level_trace, __method__);
Normal exit
DECLARE = 1, that means one signal is correspond multi-process die
while ($pid = pcntl_wait ($status, wnohang| wuntraced)) > 0) {
if (FALSE = = = pcntl_wifexited ($status)) {
Yii::log ("Sub proccess {$pid} exited unormally with code {$status}", clogger::level_warning, __method__);
} else {
Yii::log ("Sub proccess {$pid} exited normally", Clogger::level_info, __method__);
}
$this->_children--;
}
Break
Case SIGINT:
Case Sigquit:
Case SIGHUP:
Exception exit
$this->_cleanup ();
Exit (0);
Break
Default
Break
}
}
"Multi-process approach Note point"
Variable sharing issues
Because it is a process, can be understood as a copy of the main process, the execution is from the call Pcntl_fork () after the main, sub-process from the back to run, to avoid the sub-process layer nesting, the general sub-process after the execution of exit (0) to exit. A child process that crashes during execution does not affect the main process, nor can it share variables with the main process.
Signal and select conflict issues
Pcntl_signal will be in conflict with Stream_select after registering the signal.
PHP error[2]: Stream_select (): Unable to select [4]: Interrupted system call (max_fd=10)
No solution has been found, and pcntl_signal is not being used for monitoring.
Process Recycling
When a child process exits through exit, it becomes a zombie process and needs to be reclaimed by pcntl_wait.
The maximum process limit found in the CentOS test is 32,000, which is recycled by setting the total number of child processes, greater than the set value, and then calling Pcntl_wait
Socket read/write note
In the case of multiple processes, there is a problem with the same socket handle, with multiple processes simultaneously reading.
The workaround is that the read operation is in the main process, and after all the data is read out, it is thrown to the child process for execution.
File Operation Note
The FileSize function does not change the size of the file that is not being taken or taken in the multi-process situation.
This is solved by calling the system functions directly under Linux.
$file _size = @filesize ($logFile);
Resolving file size Issues when concurrency is not in progress
if (0 = = $file _size | | $this->_prevfilesize = = $file _size)
{
$file _size = @exec ('/usr/bin/stat-c%s '. Escapeshellarg ($logFile));
Clearstatcache ();
}
1000 of the concurrency after the stress test can reach 350 requests per second. should also be optimized.
http://www.bkjia.com/PHPjc/477526.html www.bkjia.com true http://www.bkjia.com/PHPjc/477526.html techarticle [PHP]//guaranteed child process upper bound if ($this-_maxfork 0 $this-_children $this-_maxfork) {yii::log (_children. $this-_maxfork,clogger:: LEVEL_WARNING,__METHOD__); $this-handler (SIGCHLD); U ...