Home >Backend Development >PHP Problem >How to enable pcntl module and implement multi-process programming in PHP?

How to enable pcntl module and implement multi-process programming in PHP?

王林
王林forward
2019-08-29 14:38:384584browse

First check the PHP version I installed:

PHP 7.3.7 (cli) (built: Jul 12 2019 22:25:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies

php needs to install the pcntl module to implement multi-process. This module is officially provided by php, so we can use it in PHP Found in the source code, download the php7.3.7 source code and extract it to the /home directory. At this time, the extension pcntl we need is at /home/php-7.3.7/ext/pcntl

Execute the following commands in sequence:

phpize
./configure --with-php-config=/usr/local/bin/php-config
make & make install

To determine the path of the php-config file, you can use find / -name php-config and finally generatepcntl.so file.

Then find the path where the ini file of php is located and use the php --ini command to view

As for the path of the php extension module, you can use php -i | grep extension_dir Check, then copy the generated so file to the module path and add extension=pcntl to the php.ini file

Use php -m to check whether the module is loaded ! At this point, the pcntl module is installed. Let’s start coding.

for ($i = 0; $i < 3; $i++){   
    $pid = pcntl_fork();
     if ($pid == -1) {
        die("开启进程失败");
    } elseif ($pid) { 
         echo "启动子进程 $pid \n";
    } else {
      echo "子进程 ".getmypid()." 正在处理任务\n";
      sleep(rand(5,10));
      exit;
    }
}

while (pcntl_waitpid(0, $status) != -1) {
    $status = pcntl_wexitstatus($status);    
    echo "子进程推出,状态码 $status \n";
}

pcntl_fork()The function creates a child process. When successful, the PID of the generated child process is returned in the execution thread of the parent process. , returns 0 in the child process execution thread. On failure, -1 is returned in the parent process context, the child process is not created, and a PHP error is raised.

pcntl_waitpid() — Wait for or return the child process status of fork, suspend the execution of the current process until the process with the process number specified by the parameter pid exits, or a signal is received to interrupt the current process. Process or call a signal handling function. The returned value can be -1, 0 or >0. If it is -1, it means that the child process has an error. If >0, it means that the child process has exited and the value is the pid of the exiting child process. As for how to exit, you can pass $status status code response

root@4226aaf8d937:/home/demo# php index.php 
启动子进程 150 
启动子进程 151 
启动子进程 152 
子进程 152 正在处理任务
子进程 151 正在处理任务
子进程 150 正在处理任务
子进程推出,状态码 0 
子进程推出,状态码 0 
子进程推出,状态码 0
root@4226aaf8d937:/# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3   3976  3180 pts/0    Ss   04:42   0:00 bash
root        17  0.0  0.3   3868  3184 pts/1    Ss   04:48   0:00 bash
root       149  0.3  2.1  79740 21888 pts/0    S+   06:18   0:00 php index.php
root       150  0.0  0.6  79740  6664 pts/0    S+   06:18   0:00 php index.php
root       151  0.0  0.6  79740  6604 pts/0    S+   06:18   0:00 php index.php
root       152  0.0  0.6  79740  6604 pts/0    S+   06:18   0:00 php index.php
root       153  0.0  0.2   7640  2660 pts/1    R+   06:18   0:00 ps -aux

How to deal with when the child process is forcibly killed using kill -9 process id?

<?php
$pid_arr = [];
for ($i = 0; $i < 3; $i++){   
    $pid = pcntl_fork();
     if ($pid == -1) {
        die("开启进程失败");
    } elseif ($pid) { 
         echo "启动子进程 $pid \n";
         array_push($pid_arr, $pid);
    } else {
      echo "子进程 ".getmypid()." 正在处理任务\n";
      sleep(rand(5,10));
      exit;
    }
}

for ($i=0; $i < count($pid_arr); $i++) {
    while (pcntl_waitpid($pid_arr[$i], $status) != -1) {
        if(!pcntl_wifexited($status)){
            //进程非正常退出
            if(pcntl_wifsignaled($status)){
                $signal = pcntl_wtermsig($status);
                //不是通过接受信号中断
                echo "子进程 $pid_arr[$i] 属于非正常停止,接收到信号 $signal \n";
            }else{
                print_r("子进程 $pid_arr[$i] 完成任务并退出 \n");
            }

        }else{
            //获取进程终端的退出状态码;
            $code = pcntl_wexitstatus($status);
            print_r("子进程 $pid_arr[$i] 正常结束任务并退出,状态码 $status \n ");
        }
    }
}

pcntl_wifexited— Check whether the status code represents a normal exit

pcntl_wifsignaled — Check whether the child process status code represents a normal exit Interrupted by a signal

pcntl_wtermsig —Returns the signal that caused the child process to be interrupted

We open one of the two windows:

How to enable pcntl module and implement multi-process programming in PHP?

How to enable pcntl module and implement multi-process programming in PHP?

For more information, please visit the PHP Chinese website: PHP Video Tutorial

The above is the detailed content of How to enable pcntl module and implement multi-process programming in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete