Pcntl pcntl_signal
Signal Registration function
PCNTL_ALARM specifies the number of seconds to interrupt the program execution task. Each execution will only have a timer to take effect, if the previous timer has not finished the definition of a new timer, will replace the previous timer and return before the end of the timer seconds, if the previous timer has completed the return 0 parameter set to 0, will clear all current timers, do not initiate the call timer will interrupt the system, even if it is sleep execution <php Declare (ticks = 1); function Signal_handler ($signal) {print "Caught sigalrm\n"; Echo pcntl_alarm (3). php_eol;//called again} pcntl_signal (SIGALRM, "Signal_handler", true); Echo Pcntl_alarm (5). php_eol;//will only be called once for Echo Pcntl_alarm (3). php_eol;//Tip 5, because the last timer finishes with 5s while (1) {//}?> Pcntl_fork
Create Child process
The UNIX creation process is more efficient than thread, but takes into account the number of processes and memory limitations <?php $pid = Pcntl_fork (); Switch ($pid) {case-1: print ' Could not fork!\n '; exit; case 0://child process print "in child!\n"; break; default://parent process, value represents child process PID Print "in parent!\n"; }?> Pcntl_waitpid
The child process state that waits or returns fork
Suspends the execution of the current process until the parameter PID specifies the process number of the process exit, or receives a signal that requires an interrupt to the current process or calls a signal handler function.
If the PID-specified child process has exited (commonly known as a zombie process) when this function is called, this function will return immediately.
PID selectable values
A process that is less than 1 waits for any process group ID equal to the absolute value of the parameter PID given values. -1 waits for any child process, consistent with the pcntl_wait function behavior. 0 wait for any child process that is the same as the calling process group ID. A child process that is greater than 0 waits for a process number equal to the parameter PID value. Options Wnohang returns immediately if no child process exits. Wuntraced is returned when the child process has exited and its status is not reported.
Other ways to calculate the return value of this function
Pcntl_wexitstatus
Check whether the status code represents a normal exit.
The process shares content <?php for ($i = 1; $i <= 5; + + $i) {$pid = Pcntl_fork (); if (! $pid) {sleep (1); print "in child $i \ n"; Exit ($ i); }} while (Pcntl_waitpid (0, $status)! =-1) {$status = Pcntl_wexitstatus ($status); echo "Child $status completed\n";}? & Gt SIGCHLD: Parent process does not block
The SIGCHLD signal notifies the parent process at the end of one or more child processes
At this point, add the Pcntl_waitpid loop to let the parent process reclaim all the finished child processes in time
<?php Declare (ticks = 1); Pcntl_signal (SIGCHLD, "Signal_handler"); function Signal_handler ($signal) {switch ($signal) {case Sigchld:while (pcntl_waitpid (0, $status)! =-1) {$status = PCN Tl_wexitstatus ($status); echo "Child $status completed\n"; } exit; }} for ($i = 1; $i <= 5; + + $i) {$pid = Pcntl_fork (); if (! $pid) {sleep (3); print "in child $i \ n"; exit ($i);} whil E (1) {//The following code executes the echo "Parent processing here" in the parent process. Php_eol; Sleep (1); }?> Pcntl_exec
Invokes the program execution and replaces itself
<?php print "before\n"; Pcntl_exec ("/usr/bin/uptime"); Print "after\n" is not output below;?>
PHP Signal Processing