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.

The zombie process refers to the parent process that has exited,And the process dead after the process is not accepted,It becomes the zombie process (zombie) 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
$pid = Pcntl_fork (), if ($pid = =-1) {die    (' fork error ');} else if ($pid) {    //parent process blocks waiting for the child process to exit    //pcntl_wait ($ status);    Pcntl_waitpid ($pid, $status);        Non-blocking mode    //pcntl_wait ($status, Wnohang);    Pcntl_waitpid ($pid, $status, Wnohang);} 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.
<?phpdeclare (ticks = 1);//Signal handler function Sig_func () {    echo "SIGCHLD \ r \ n";    Pcntl_wait ($status);    Pcntl_waitpid ( -1, $status);    Non-blocking    //pcntl_wait ($status, Wnohang);    Pcntl_waitpid ( -1, $status, Wnohang);} Pcntl_signal (SIGCHLD, ' sig_func '); $pid = Pcntl_fork (); if ($pid = =-1) {die    (' fork error ');} else if ($pid) {    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, no recycling occurs,the zombie process.  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 noInstall the SIGCHLD signal handler function after the child process is finished. 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:
<?phpdeclare (ticks = 1);//Signal handler function Sig_func () {    echo "SIGCHLD \ r \ n";    Pcntl_waitpid ( -1, $status, Wnohang);} Pcntl_signal (SIGCHLD, ' sig_func '); $pid = Pcntl_fork (); if ($pid = =-1) {die    (' fork error ');} else if ($pid) {for    ( ;;) {        sleep (3);    }} else {    echo "child \ r \ n";    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.
<?phpdeclare (ticks = 1);p cntl_signal (SIGCHLD, sig_ign); $pid = Pcntl_fork (); if ($pid = =-1) {die    (' fork error ');} else if ($pid) {for    (;;) {        sleep (3);    }} else {    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).
<?php$pid = Pcntl_fork (); if ($pid = =-1) {die    (' fork error ');} else if ($pid) {    //parent process waits for the child process to exit    pcntl_wait ($ status);    echo "parent \ r \ n";} else {    //Sub-process is forked again, resulting in grandchild process    $cpid = Pcntl_fork ();        if ($cpid = =-1) {die        (' fork error ')    ,} else if ($cpid) {        //Here is a subprocess, exit        echo "child \ r \ n" directly;        Exit;    } else {        //Here is the grandchild process, which handles business logic for        ($i = 0; $i < + + + $i) {            echo "work ... \ r \ n";            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.