This article mainly introduces how to use multi-process programming functions in PHP using examples, including processing zombie processes! For more information, see
This article mainly introduces how to use multi-process programming functions in PHP using examples, including processing zombie processes! For more information, see
PHP has a set of process control functions (enable-pcntl and posix extensions are required during compilation ), php allows you to create sub-processes, execute programs using exec functions, and process signals like c.
<? Phpheader ('content-type: text/html; charset = UTF-8 '); // The extension if (! Function_exists ("pcntl_fork") {die ("pcntl extention is must! ");} // Total number of processes $ totals = 3; // Number of executed scripts $ cmdArr = array (); // array of the number of executed scripts ($ I = 0; $ I <$ totals; $ I ++) {$ cmdArr [] = array ("path" => _ DIR __. "/run. php ", 'pid '=> $ I, 'Total' => $ totals);}/* expand: $ cmdArrArray ([0] => Array ([path] =>/var/www/html/company/pcntl/run. php [pid] => 0 [total] => 3) [1] => Array ([path] =>/var/www/html/company/pcntl/run. php [pid] => 1 [total] => 3) [2] => Array ([path] =>/var/www/html/com Pany/pcntl/run. php [pid] => 2 [total] => 3) */pcntl_signal (SIGCHLD, SIG_IGN); // if the parent process does not care when the child process ends, after the child process ends, the kernel will recycle it. Foreach ($ cmdArr as $ cmd) {$ pid = pcntl_fork (); // create a sub-process // both the parent process and the sub-process will execute the following code if ($ pid =-1) {// handle the error: -1.die( 'could not fork');} else if ($ pid) {// The parent process will get the child process number, so here is the logic of the parent process. // if you do not need to block the process and want to exit the sub-process, you can comment out the pcntl_wait ($ status) statement or write it: pcntl_wait ($ status, WNOHANG); // wait for the sub-process to interrupt and prevent the sub-process from becoming a zombie .} Else {// The $ pid obtained by the sub-process is 0, so here is the logic of sub-process execution. $ Path = $ cmd ["path"]; $ pid = $ cmd ['pid ']; $ total = $ cmd ['Total']; echo exec ("/usr/bin/php {$ path} {$ pid} {$ total }"). "\ n"; exit (0) ;}}?>
The real multi-process running mode of PHP is suitable for data collection, mass mailing, data source update, and tcp server.
PHP has a set of process control functions (enable-pcntl and posix extensions are required during compilation ), this allows php to create sub-processes, execute programs using exec functions, and process signals in * nix systems like c. PCNTL uses ticks as the signal processing mechanism (signal handle callback mechanism) to minimize the load for processing asynchronous events. What is ticks? Tick is an event that occurs when the interpreter executes N low-level statements in a code segment. This code segment needs to be specified through declare.
Common PCNTL Functions
1. pcntl_alarm (int $ seconds)
Sets a counter for sending SIGALRM signals after $ seconds.
2. pcntl_signal (int $ signo, callback $ handler [, bool $ restart_syscils])
Set a callback function for $ signo to process the signal. The following is an example of how to send a SIGALRM signal every five seconds, which is obtained by the signal_handler function, and then print "Caught SIGALRM:
<? Phpdeclare (ticks = 1); function signal_handler ($ signal) {print "Caught SIGALRM \ n"; pcntl_alarm (5);} pcntl_signal (SIGALRM, "signal_handler", true ); pcntl_alarm (5); for (;) {}?>
3. pcntl_exec (string $ path [, array $ args [, array $ envs])
Execute a specified program in the current process space, similar to the exec family function in c. The so-called current space, that is, the Code loaded into the specified program overwrites the space of the current process, and the process ends after the program is executed.
<? Php $ dir = '/home/shankka/'; $ cmd = 'LS'; $ option = '-l'; $ pathtobin ='/bin/ls '; $ arg = array ($ cmd, $ option, $ dir); pcntl_exec ($ pathtobin, $ arg); echo '2016'; // will not execute to this row?>
4. pcntl_fork (void)
Create a child process for the current process and run the parent process first. The PID of the child process is returned, which must be greater than zero. In the code of the parent process, you can use pcntl_wait (& $ status) to pause the parent process to know that its child process has returned values. Note: Blocking of the parent process also blocks the child process. However, the completion of the parent process does not affect the running of the child process.
After the parent process is run, the child process is run. The child process starts to execute (including this function) from the statement that executes pcntl_fork ), however, it returns zero (representing a sub-process ). It is better to have an exit statement in the code block of the child process, that is, the execution of the child process ends immediately after the execution. Otherwise, it starts executing some parts of the script again.
Note:
It is best for a sub-process to have an exit statement to prevent unnecessary errors;
It is best not to have other statements between pcntl_fork, for example:
<? Php $ pid = pcntl_fork (); // it is best not to have other statements here if ($ pid =-1) {die ('could not fork ');} else if ($ pid) {// we are the parentpcntl_wait ($ status); // Protect against Zombie children} else {// we are the child}?>
5. pcntl_wait (int & $ status [, int $ options])
Blocks the current process. Only one sub-process of the current process exits or receives a signal to end the current process. Use $ status to return the status code of the sub-process, and specify the second parameter to indicate whether the call is blocked:
If the call is blocked, the return value of the function is the pid of the sub-process. If no sub-process is returned, the return value is-1;
A function can be called in non-blocking mode. A function can return 0 when a child process is running but has not ended.
6. pcntl_waitpid (int $ pid, int & $ status [, int $ options])
The difference between the function and pcntl_wait is that waitpid is a child process waiting for the specified pid. When the pid is-1, pcntl_waitpid is the same as pcntl_wait. The $ status of the sub-process is saved in the pcntl_wait and pcntl_waitpid functions. This parameter can be used for functions such as pending, running, running, pcntl_wexitstatus, closing, pcntl_wstopsig, and pcntl_waitpid.
For example:
<? Php $ pid = pcntl_fork (); if ($ pid) {pcntl_wait ($ status); $ id = getmypid (); echo "parent process, pid {$ id }, child pid {$ pid} \ n ";}else {$ id = getmypid (); echo" child process, pid {$ id} \ n "; sleep (2) ;}?>
The child process stops running after two seconds after the child process is output, and the parent process is blocked until the child process exits.