A preliminary introduction to PHP multi-process programming
This article mainly introduces the initial introduction to PHP multi-process programming. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.
Envy Naruto in Naruto Shadow clone? Yes, PHP programs can activate shadow clones! If you want to complete a task but feel that one process is too slow, then try using multiple processes. This article will introduce the basic needs of PHP multi-process, how to create multi-process and basic signal control. It will not tell you how to communicate and share information between processes for the time being.
1. Preparation
Before you start, please make sure you are not using the M$ Windows platform (because I don’t have Windows). Linux/BSD/Unix should be no problem. After confirming the working environment, let's see if the PHP modules we need are available. Open the terminal and enter the following command:
1 |
|
This command checks and prints all the currently enabled extensions of PHP. Check whether pcntl
and posix
are in the output list.
1.1. pcntl
If pcntl
cannot be found, it is likely that this extension was not compiled during compilation. If you compiled and installed PHP like me, you need to recompile and install PHP. Remember to add the --enable-pcntl
parameter when configuring.
1 2 3 |
|
1.2. posix
This product will usually be installed by default, as long as you do not add --disable-posix
when compiling.
2. Preliminary knowledge
Before continuing, you also need to have a little understanding of Linux multi-process. What's going on with multi-processing? This is slightly different from the shadow clone in Naruto. First of all, Naruto grew up from a young age, like 16 years old, cough. One day he activated his shadow clone and split into five of himself. Obviously, these clones are also 16-year-old Naruto and not babies who cry when they are born and don't understand anything (that's called cloning). Then, here comes the difference: the clones have become independent people doing their own things, and they no longer know what other clones and the original body have done (of course they will not accumulate experience like in the cartoon) To the original person). Unless they communicate with each other, only things before the age of 16 are their common memories.
Some classmates said, boss, aren’t you cheating on me? I haven't watched Naruto! Then you should take a look...
Finally, the preparatory knowledge is over, just have a general understanding of what the child processes created by the main process are like. The code of the child process is exactly the same as the main process, and the other part of the same thing is everything executed until the shadow clone is launched. For details, please refer to the "Operating System" course.
3. The Art of Shadow Clone
So, how can you understand the contents of the scroll without some basic knowledge? When I opened the scroll, I first saw a word: fork.
3.1. fork
Fork? The forks are bifurcated, one becomes multiple! That’s pretty much what it means. Use this command to create a child process. You need to use the pcntl_fork()
function here. (You can first take a brief look at the introduction of this function in the PHP manual.) Create a PHP script:
1 2 3 4 |
|
pcntl_fork()
The function creates a child process. The only difference between the child process and the parent process The PID (process ID) and PPID (parent process ID) are different. To view the process in the terminal, use the ps
command (ask man how to use ps: man ps
). When the function returns -1, it means that the fork failed. Try adding a sentence in front of if
: echo $pid . PHP_EOL;
. Run your script, the output may be like the following (the result shows that the code of the child process and the parent process are the same):
1 2 |
|
pcntl_fork()
After the function call is successful, in the parent process The PID of the child process will be returned, and 0 is returned in the child process. Therefore, the if
branch is used directly below to control the parent process and the child process to do different things.
3.2. Assign tasks
Then let’s talk about Naruto’s shadow clone when he was 16 years old, and assign two simple output tasks to the original body and clone:
1 2 3 4 5 6 7 8 9 |
|
The output result may be like this:
1 2 |
|
Let me emphasize again, pcntl_fork()
After the call is successful, one program becomes two programs: one program gets the The value of the $pid
variable is 0, which is the child process; the value of $pid
obtained by another program is greater than 0. This value is the PID of the child process, which is the parent process. In the branch statement below, different codes are run due to different $pid
values. Let me emphasize again: the code of the child process is the same as that of the parent process. Therefore, different tasks must be assigned to them through branch statements.
3.3. 子进程回收
刚刚有man ps
么?一般我习惯用ps aux
加上grep
命令来查找运行着的后台进程。其中有一列STAT
,标识了每个进程的运行状态。这里,我们关注状态Z
:僵尸(Zombie)。当子进程比父进程先退出,而父进程没对其做任何处理的时候,子进程将会变成僵尸进程。Oops,又跟火影里的影分身不一样了。鸣人的影分身被干死了以后就自动消失了,但是这里的子进程分身死了话还留着一个空壳在,直到父进程回收它。僵尸进程虽然不占什么内存,但是很碍眼,院子里一堆躺着的僵尸怎么都觉得怪怪的。(别忘了它们还占用着PID)
一般来说,在父进程结束之前回收挂掉的子进程就可以了。在pcntl
扩展里面有一个pcntl_wait()
函数,它会将父进程挂起,直到有一个子进程退出为止。如果有一个子进程变成了僵尸的话,它会立即返回。所有的子进程都要回收,所以多等等也没关系啦!
3.4. 父进程先挂了
如果父进程先挂了怎么办?会发生什么?什么也不会发生,子进程依旧还在运行。但是这个时候,子进程会被交给1号进程,1号进程成为了这些子进程的继父。1号进程会很好地处理这些进程的资源,当它们结束时1号进程会自动回收资源。所以,另一种处理僵尸进程的临时办法是关闭它们的父进程。
4. 信号
一般多进程的事儿讲到上面就完了,可是信号在系统中确实是一个非常重要的东西。信号就是信号灯,点亮一个信号灯,程序就会做出反应。这个你一定用过,比如说在终端下运行某个程序,等了半天也没什么反应,可能你会按 Ctrl+C 来关闭这个程序。实际上,这里就是通过键盘向程序发送了一个中断的信号:SIGINT。有时候进程失去响应了还会执行kill [PID]
命令,未加任何其他参数的话,程序会接收到一个SIGTERM信号。程序收到上面两个信号的时候,默认都会结束执行,那么是否有可能改变这种默认行为呢?必须能啊!
4.1. 注册信号
人是活的程序也是活的,只不过程序需要遵循人制定的规则来运行。现在开始给信号重新设定规则,这里用到的函数是pcntl_signal()
(继续之前为啥不先查查PHP手册呢?)。下面这段程序将给SIGINT重新定义行为,注意看好:
1 2 3 4 5 |
|
执行一下,随时按下 Ctrl+C 看看会发生什么事。
4.2. 信号分发
说明一下:pcntl_signal()
函数仅仅是注册信号和它的处理方法,真正接收到信号并调用其处理方法的是pcntl_signal_dispatch()
函数。试试把// do something
替换成下面这段代码:
1 2 3 |
|
在终端下执行这个脚本,当它不停输出数字的时候尝试按下 Ctrl+C 。看看程序有什么响应?嗯……什么都没有,除了屏幕可能多了个^C
以外,程序一直在不停地输出数字。因为程序一直没有执行到pcntl_signal_dispatch()
,所以就并没有调用signalHandler()
,所以就没有输出signal received
。
4.3. 版本问题
如果认真看了PHP文档,会发现pcntl_signal_dispatch()
这个函数是PHP 5.3以上才支持的,如果你的PHP版本大于5.3,建议使用这个方法调用信号处理器。5.3以下的版本需要在注册信号之前加一句:declare(ticks = 1);
表示每执行一条低级指令,就检查一次信号,如果检测到注册的信号,就调用其信号处理器。想想就挺不爽的,干嘛一直都检查?还是在我们指定的地方检查一下就好。
4.4. 感受僵尸进程
现在我们回到子进程回收的问题上(差点忘了= =")。当你的一个子进程挂了(或者说是结束了),但是父进程还在运行中并且可能很长一段时间不会退出。一个僵尸进程从此站起来了!这时,保护伞公司(内核)发现它的地盘里出现了一个僵尸,这个僵尸是谁儿子呢?看一下PPID就知道了。然后,内核给PPID这个进程(也就是僵尸进程的父进程)发送一个信号:SIGCHLD。然后,你知道怎么在父进程中回收这个子进程了么?提示一下,用pcntl_wait()
函数。
4.5. Send signal
I hope you have carefully manned the kill
command just now. It actually sends a signal to the process. In PHP, you can also call the posix_kill()
function to achieve the same effect. With it, you can control the running of other child processes in the parent process. For example, to close all child processes before the parent process ends, then record the PIDs of all child processes in the parent process when fork
, and send end signals to the child processes in sequence before the parent process ends.
5. Practice
PHP's multi-process is quite similar to C. Once you understand it, it will be similar if you write it in other languages. If you have time, try writing a small program and experience it personally:
16-year-old Naruto sent shadow clones and split into 5 clones
Each clone survives randomly for 10 to 30 seconds, and outputs something every second.
Ensure that the original clone can feel the end of the clone, and then activate another clone. Ensure that there are at most 5 clones
Do not use
nohup
, so that the original body can still run after the terminal is closedWrite the number of clones (5) into a configuration file. When sending a signal to the original body (consider using SIGUSR1 or SIGUSR2), the original body reads the configuration file and updates the maximum number of clones allowed
If there are too many clones, close a few; if there are fewer, split out a few more
Tips:
Use
while
Loop to ensure that the process is running, pay attention tosleep
to avoid 100% CPU usageWhen the terminal running the process is closed, the program will receive a SIGHUP signal
You can use the
parse_ini_file()
function to parse the INI configuration file
The above is the entire content of this article, I hope It will be helpful for everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Understanding of PHP polymorphism
Introduction to PHP file programming
The above is the detailed content of A preliminary introduction to PHP multi-process programming. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.
