Blocking and non-blocking of the PHP multi-process parent process

Source: Internet
Author: User

The blocking process in PHP is primarily the parent process waiting for the child process to exit.

The 1.php code is as follows:

<?php//defines the number of processes define (' Fork_nums ', 5);//used to save the process Pid$pids = array ();//We create 5 sub-processes for ($i = 0; $i < fork_nums; + + $i) {    $pids [$i] = Pcntl_fork ();    if ($pids [$i] = = 1) {die        (' fork error '),    } else if ($pids [$i]) {        //This is the parent process space, i.e. the main process        //Our For loop first entered here , pcntl_wait suspends the current master process, waits for the first child process to complete exit        //Note For Loop code is in the main process, hangs the main process, the equivalent of the current for loop is also blocked here        //After the first child process exits, and then creates a second child process , hang up here, wait for the second child process to exit, continue to create a third, and so on.        pcntl_wait ($status);    } else {        //Here is the child process space        echo "Parent process ID:", posix_getppid (), "Process ID:", posix_getpid (), "{$i} \ r \ n";        We let the child process wait for 3 seconds before exiting        sleep (3);        Exit;    }}

The above code results are as follows:

What happens if we take pcntl_wait out of the for loop?

The 2.php code is as follows:

<?php//defines the number of processes define (' Fork_nums ', 5);//used to save the process Pid$pids = array ();//We create 5 sub-processes for ($i = 0; $i < fork_nums; + + $i) {    $pids [$i] = Pcntl_fork ();    if ($pids [$i] = = 1) {die        (' fork error '),    } else if ($pids [$i]) {    } else {        //Here is the child process space        echo "Parent process ID:", Posix_getppid (), "Process ID:", posix_getpid (), "{$i} \ r \ n";        We let the child process wait for 3 seconds before exiting        sleep (3);        Exit;    }} We put the pcntl_waitpid out of the for loop so that creating a child process in the for loop would not block//But it would still block, and the main process would wait until 5 of the child processes exited before exiting. foreach ($pids as $pid) {    pcntl_waitpid ($pid, $status);}

The above code results are as follows:

The second parameter of the pcntl_wait can be used to set the main process not to wait for the child process to exit and continue execution of subsequent code.

The 3.php code is as follows:

<?php//defines the number of processes define (' Fork_nums ', 5);//used to save the process Pid$pids = array ();//We create 5 sub-processes for ($i = 0; $i < fork_nums; + + $i) {    $pids [$i] = Pcntl_fork ();    if ($pids [$i] = = 1) {die        (' fork error ');    } else if ($pids [$i]) {        //This is the parent process space, which is the main process        // This is only a bit different from the 1.php code, that is, add the second parameter Wnohang        //for loop here, will not suspend the main process, but continue to execute subsequent code        pcntl_wait ($status, Wnohang);    } else {        //Here is the child process space        echo "Parent process ID:", posix_getppid (), "Process ID:", posix_getpid (), "{$i} \ r \ n";        We let the child process wait for 3 seconds before exiting        sleep (3);        Exit;    }} Here we wait 10 seconds, otherwise the child process has not finished, the main process is exited, see no effect sleep (10);

The above code results are as follows:

Code 3.php As with the 2.php effect, the pcntl_wait () function is used only to let the parent process wait for the child process to exit, which by default blocks the main process.

Blocking and non-blocking of the PHP multi-process parent process

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.