The pcntl_wait of PHP's pcntl process Control

Source: Internet
Author: User
This article mainly introduces the PCNTL process Control of PHP pcntl_wait, has a certain reference value, now share to everyone, the need for friends can refer to

pcntl_wait Introduction

# source Official pcntl_wait-Wait or return fork of the subprocess status int pcntl_wait (int & $status [, int $options = 0]) Wait function scraping the execution of the current process until a child process exits or receives a letter Requires interrupting the current process or invoking a signal processing function. If a child process has exited (commonly known as a zombie process) when calling this function, this function returns immediately. All system resources used by the child process will be freed. See the Wait (2) manual for your system for a detailed specification of what wait is working on your system. Note: This function is equivalent to a value of 1 as the parameter PID and there is no options parameter to invoke the Pcntl_waitpid () function. The parameter statuspcntl_wait () will store the state information on the status parameter, and the status information returned by the status parameter can be used with the following function pcntl_wifexited (), pcntl_wifstopped (), Pcntl_ Wifsignaled (), Pcntl_wexitstatus (), Pcntl_wtermsig (), and Pcntl_wstopsig () get their specific values. Options if your operating system (most BSD class systems) allows the use of WAIT3, you can provide optional options parameters. If this parameter is not provided, wait will be used as a system call. If the wait3 is not available, providing the parameter options will have no effect. The options value can be 0 or below two constants or two constants "or operations" (that is, two constants represent meaning). Options available Value Wnohang    returns immediately if no child process exits. Wuntraced is    returned when the child process has exited and its status is not reported. The return value pcntl_wait () returns the process number of the child process that exited, returns 1 if an error occurs, if Wnohang is provided as option (Wait3 available system) and returns 0 if no child processes are available.

Test Code

<?php/** * Created by Phpstorm. * User:object * DATE:2018/6/11 * time:10:28 */if (Strtolower (Php_sapi_name ())! = ' cli ') {die ("Please run in CLI mode");} $index = 0; $loop = 1;while ($index < $loop) {echo "Current process:". Getmypid ().    Php_eol; $pid = Pcntl_fork ();    Fork out child Process if ($pid = =-1) {//Create error, Return-1 die (' Process fork failed '); } else if ($pid) {//$pid > 0, if Fork succeeds, returns child process ID//Parent process Logic pcntl_wait ($status);//parent process must wait for a child process to exit before creating the next child        Process. $child _id = $pid; The ID of the child process $pid = Posix_getpid (); Gets the current process id $ppid = Posix_getppid ();        The parent ID of the process $time = Microtime (true); echo "I am the parent process, fork's child process ID: {$child _id}; The current process id:{$pid}; parent process id:{$ppid}; Current index:{$index}; Current time: {$time} ".    Php_eol;        } else {//$pid = 0//child process logic $cid = $pid;        $pid = Posix_getpid ();        $ppid = Posix_getppid ();        $myid = Getmypid ();        $time = Microtime (true); echo "I am the child process, the current process id:{$pid}; The parent process id:{$ppid}; Current index:{$index}; Current time: {$time} ".       Php_eol; Exit    Sleep (2); } $index + +;}

loop = 1 Execution result

Current process: 16604 I am the child process, the current process id:16605; the parent process id:16604; Current index:0; Current time: 1528696774.1978 I am the parent process, the child process of fork id:16605; current process id:16604; parent process id:15128; Current index:0; Current time: 1528696774.2032

loop = 2 Execution result

Current process: 16613 I am the child process, the current process id:16614; the parent process id:16613; Current index:0; Current time: 1528696781.4751 current process: 16614 I am a child process, current process id:16615; parent process id:16614; Current index:1; Current time: 1528696781.4756 I am the parent process, the child process of fork id:16615; current process id:16614; parent process id:16613; Current index:1; Current time: 1528696781.4802 I am the parent process, the child process of fork id:16614; current process id:16613; parent process id:15128; Current index:0; Current time: 1528696781.4858 current process: 16613 I am a child process, current process id:16616; parent process id:16613; Current index:1; Current time: 1528696781.4863 I am the parent process, the child process of fork id:16616; current process id:16613; parent process id:15128; Current index:1; Current time: 1528696781.4913

loop = 3 Execution result

Current process: 16625 I am the child process, the current process id:16626; the parent process id:16625; Current index:0; Current time: 1528696787.3334 current process: 16626 I am a child process, current process id:16627; parent process id:16626; Current index:1; Current time: 1528696787.3338 current process: 16627 I am a child process, current process id:16628; parent process id:16627; Current Index:2; Current time: 1528696787.3345 I am the parent process, the child process of fork id:16628; current process id:16627; parent process id:16626; Current Index:2; Current time: 1528696787.3391 I am the parent process, the child process of fork id:16627; current process id:16626; parent process id:16625; Current index:1; Current time: 1528696787.3434 current process: 16626 I am a child process, current process id:16629; parent process id:16626; Current Index:2; Current time: 1528696787.3441 I am the parent process, the child process of fork id:16629; current process id:16626; parent process id:16625; Current Index:2; Current time: 1528696787.3496 I am the parent process, the child process of fork id:16626; current process id:16625; parent process id:15128; Current index:0; Current time: 1528696787.3543 current process: 16625 I am a child process, current process id:16630; parent process id:16625; Current index:1; Current time: 1528696787.3548 current process: 16630 I am a child process, current process id:16631; parent process id:16630; Current Index:2; Current time: 1528696787.3555 I am the parent process, the child process of fork id:16631; current process id:16630; parent process id:16625; Current Index:2; Current time: 1528696787.3599 I am the parent process, the child process of fork id:16630; current process id:16625; parent process id:15128; Current index:1; Current time: 1528696787.3643 current process: 16625 I am a child process, current process id:16632; parent process id:16625; Current Index:2; Current time: 1528696787.3649 I'm the father.Process, fork sub-process id:16632; current process id:16625; parent process id:15128; Current Index:2; Current time: 1528696787.3697

Summarize

1. From the multiple results of execution, the program creates the fork from outside to inside. And then quit from the last fork in the
2. After a fork, the parent process of the program is blocked by pcntl_wait and then waits for the child process of the fork to exit, then the parent process of the corresponding child process executes the logic and exits
3. Then the parent process that executes the notebook process loops 2 logical exits in turn, ending the total process

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.