php multiprocess programming

php multiprocess programming
PHP's process control supports Unix mode process creation, program execution, signal processing and process interruption. Process control cannot be applied to the Web server environment, which may bring unexpected results when used in the Web service environment.
 
pcntl function
  • pcntl_fork(): generates a branch (child process) at the current position of the current process. A fork creates a child process. Both the parent process and the child process continue to execute from the fork position. The difference is that during the execution of the parent process, the fork return value is the child process number, while the child process gets 0
<?php
$pid = pcntl_fork();
//Both the parent and child processes execute the following code
if ($pid == -1) {
    //Error handling: return when failed to create child process-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 execution
     pcntl_wait($status); //Wait for the subprocess to be interrupted to prevent the subprocess from becoming a zombie process.
} else {
     //The result of subprocess $pid For 0, So here is the logic of the subprocess execution.
    exit();//The subprocess should exit after execution, otherwise it will continue to execute the following logic
}
  • Pcntl_wait (int & $status [, int $options = 0]): wait or return the subprocess status of fork, equivalent to pcntl_waitpid (- 1, int & $status [, int $options = 0])
  • Pcntl [waitpid (int $PID, int & $status [, int $options = 0]) $status is the parameter of the following function
  • Pcntl [wifexited (int $status) checks whether the subprocess status code represents a normal exit,
  • Pcntl 〝 wexistatus (int $status) returns the return code of an interrupted subprocess, which is valid only in normal interrupts
  • Pcntl ﹣ wifsignalized (int $status) checks whether a subprocess exits from an uncaughted signal. Yes returns true, no returns false
  • pcntl_wtermsig(int $status) returns the signal that causes the subprocess to be interrupted. It is valid when pcntl_wifsigned returns true
<?php
 echo "Main process\n";
 $pid = pcntl_fork();
 //Both the parent and child processes execute this code
 if($pid == -1 ){
     //Failed to create child process will return-1
     throw new Exception ('fork error on Task object');
 }else if($pid){
     //If the creation is successful, the parent process will get the child process's pid
     echo "Wait for subprocess to execute";
     pcntl_wait($status);//Wait for subprocess to break
     echo "Subprocess execution status:";
     echo "Exit normally:",pcntl_wifexited($status),"\n";
     echo "Code returned by subprocess:",pcntl_wexitstatus($status),"\n";//Only in pcntl_wifexited Return true Come into effect,Can only be int,Output 123
     echo "Whether the subprocess exits due to an uncaughted signal:",pcntl_wifsignaled($status),"\n";//If it is kill -9|-15 Kill process return true
     echo "Signals that cause subprocesses to break:",pcntl_wtermsig($status),"\n"; Output 9 | 15
     var_dump($status);
 }else{
     //Creating a successful subprocess will get pid=0
     sleep(2);
     echo "Subprocess completed\n";
         exit(123)
 }
  • pcntl_alarm(int $seconds): set an alarm signal for the process
  • pcntl_signal(int $signo, callback $handler [, bool $restart_syscalls = true]) installs a new signal processor for the specified signal
  • Pcntl? Signal? Get? Handler (int $Signo) gets the processing function of the specified signal
<?php
 
echo "Set alarm signal after 3 seconds\n";
pcntl_alarm(3);
 
function dealSigalarm(){
    echo "Received signal SIGALRM \n Exit program...\n";
    exit();
}
 
echo "Installing the signal processor\n";
pcntl_signal(SIGALRM,"dealSigalarm");//For signals that cannot be blocked, processed, or ignored, php Registering a signal handler for these times can result in a fatal error SIGSTOP,SIGKILL
var_dump(pcntl_signal_get_handler(SIGUSR1));//output dealSigalarm
pcntl_signal(SIGUSR1,function(){
    echo "Receive user defined signal\n";
});
$i = 1;
while(1){
    sleep(1);
    echo $i++,"\n";
    echo "distribute... \n";
    pcntl_signal_dispatch();
};
  • Pcntl_getpriority (int $PID = getmypid() [, int $process_identifier = PRIO_PROCESS]) gets the priority of the process
  • Pcntl_setpriority (int $priority [, int $PID = getmypid() [, int $process_identifier = PRIO_PROCESS]]) sets the priority of the process
  • getmypid() gets the pid of the current php process
  • posix_getpid() gets the pid of the current process
<?php
 /**
 * php Priority of the process
 */
 
 for($i = 1;$i<=5;$i++){
     $pid = pcntl_fork();
     if($pid == -1){
         throw new Exception("fork error on task object");
     }else if ($pid){
         pcntl_wait($status);
     }else{
         $end_time = time()+3;
         $k = 0;
         while(time()<=$end_time){
             $k++;
         }
         $pid = getmypid();
         echo "Current process id:".$pid,"priority:",pcntl_getpriority($pid);
         pcntl_setpriority($i);
         echo "The priority after modification is:",pcntl_getpriority(),"\n";
         echo "Process executed{$i} {$k}second\r\n";
         exit();
     }
 }

 

 

Tags: PHP Programming Unix Web Server

Posted on Thu, 16 Jan 2020 03:25:27 -0500 by Travist6983