PHP multi-process to prevent zombie processes from appearing

Source: Internet
Author: User
Tags signal handler

For multi-process concurrent programming with PHP, it is unavoidable to encounter the problem of zombie process.

A zombie process is a zombie process (zombie) process in which a parent process has exited, and the process dead after it has not been accepted by the process. Any process that exits before exiting (using exit exit) becomes a zombie process (used to hold information such as the state of the process) and is then taken over by the INIT process. If the zombie process is not recycled in time, it will occupy a process table entry in the system, and if the zombie process is too large, the system will not be able to use the process table entries, so it can no longer run other programs. Method One: The parent process waits for the child process to end through functions such as pcntl_wait and Pcntl_waitpid
1 $pid=pcntl_fork ();2  3 if($pid= =-1) {4      die(' Fork error ');5}Else if($pid) {6     //the parent process blocks waiting for the child process to exit7 //pcntl_wait ($status);8  9 //pcntl_waitpid ($pid, $status);Ten       One //non-blocking mode A //pcntl_wait ($status, Wnohang); -   - //pcntl_waitpid ($pid, $status, Wnohang); the}Else { -     Sleep(3); -     Echo"Child \ r \ n"; -     Exit; +}
Method Two: You can use the signal function to install handler for SIGCHLD, because when the child process ends, the parent process receives the signal and can call pcntl_wait or pcntl_waitpid in handler to reclaim it.
1<?PHP2 Declare(Ticks = 1);3  4 //Signal Processing Functions5 functionSig_func () {6     Echo"SIGCHLD \ r \ n";7Pcntl_wait ($status);8  9     //pcntl_waitpid ( -1, $status);Ten   One //non-blocking A //pcntl_wait ($status, Wnohang); - //pcntl_waitpid ( -1, $status, Wnohang); - } the   -Pcntl_signal (SIGCHLD, ' Sig_func ')); -   - $pid=pcntl_fork (); +   - if($pid= =-1) { +      die(' Fork error '); A}Else if($pid) { at     Sleep(10); -}Else { -     Sleep(3); -     Echo"Child \ r \ n"; -     Exit; -}
If the child process is not finished and the parent process is finished, the Init process automatically takes over the child process and recycles it. If the parent process is a loop, and no SIGCHLD signal handler is installed, call wait or waitpid () to wait for the child process to end. Then, when the child process is finished, there is no recycling and the zombie process is generated. For example:
 <? php   $pid  = Pcntl_fork ();  if  ( $pid  = = -1 die  (' fork error '  else  if  (  $pid   for   (;;)    { sleep  (3 else   {     echo  "child \ r \ n" ;  exit  ;}  

The parent process is a dead loop, and there is no SIGCHLD signal processing function installed, after the child process has ended. We use the following command to view> ps -A -o stat,ppid,pid,cmd | grep -e ‘^[Zz]‘

A zombie process will be found. Code improvements:
1<?PHP2 Declare(Ticks = 1);3  4 //Signal Processing Functions5 functionSig_func () {6     Echo"SIGCHLD \ r \ n";7  8Pcntl_waitpid (-1,$status,Wnohang);9 }Ten   OnePcntl_signal (SIGCHLD, ' Sig_func ')); A   - $pid=pcntl_fork (); -   the if($pid= =-1) { -      die(' Fork error '); -}Else if($pid) { -      for(;;) { +         Sleep(3); -     } +}Else { A     Echo"Child \ r \ n"; at     Exit; -}
When the child process is finished, and then viewed through the command, we find that there is no zombie process, which means that the parent process recycles it. Method Three: If the parent process does not care about when the child process ends, then you can use Pcntl_signal (SIGCHLD, Sig_ign) to notify the kernel, the end of the child process is not interested, then the end of the process, the kernel will be recycled, and no longer send a signal to the parent process.
1<?PHP2 Declare(Ticks = 1);3  4Pcntl_signal (SIGCHLD,sig_ign);5  6 $pid=pcntl_fork ();7  8 if($pid= =-1) {9      die(' Fork error ');Ten}Else if($pid) { One      for(;;) { A         Sleep(3); -     } -}Else { the     Echo"Child \ r \ n"; -     Exit; -}
When the child process finishes, the SIGCHLD signal is not sent to the parent process, but instead notifies the kernel that the child process has been reclaimed. Method Four: Through Pcntl_fork two times, that is, the parent process fork out the child process, then the child process and then fork out the grandchild process, then the child process exits. Then the init process takes over the grandchild process, and after the grandchild process exits, init recycles. However, the child process still needs to be reclaimed by the parent process. We put the business logic into the grandchild process, and the parent process does not need pcntl_wait or pcntl_waitpid to wait for the grandchild process (that is, the business process).
1<?PHP2 $pid=pcntl_fork ();3  4 if($pid= =-1) {5      die(' Fork error ');6}Else if($pid) {7     //parent process waits for child process to exit8Pcntl_wait ($status);9     Echo"Parent \ r \ n";Ten}Else { One     //Sub-process fork again, resulting in grandchild process A     $cpid=pcntl_fork ();  -     if($cpid= =-1) { -          die(' Fork error '); the}Else if($cpid) { -         //This is a sub-process, exit directly -         Echo"Child \ r \ n"; -         Exit; +}Else { -         //here is the grandchild process, which handles the business logic +          for($i= 0;$i< 10; ++$i) { A             Echo"Work ... \ r \ n"; at             Sleep(3); -         } -     } -}

After the child process exits, the parent process reclaims the child process, and the grandchild process resumes the processing of the business logic. When the grandchild process is executed and exited, Init reclaims the grandchild process.

PHP multi-process to prevent zombie processes from appearing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.