Home>Article>Backend Development> Signal handling for PHP programmers

Signal handling for PHP programmers

藏色散人
藏色散人 forward
2021-02-04 07:04:08 3133browse

Signal Processing Tutorial for PHP Programmers

I had braised pork tonight, and the girl at the table asked me, can this thing be eaten? Me: You can eat it if you think you can. . . Topics unrelated to the content

What is a signal

A signal is a notification mechanism for the process (also called a software interrupt) when an event occurs. When a process receives a signal, the kernel will pause the code being executed by the process and jump to the corresponding signal processing function. If the processing function does not interrupt, after the processing function is executed, execution will continue where it was interrupted. implement.

When we write code in FPM mode, we will not encounter problems related to signal processing, but how can some memory-resident scripts in CLI mode be able to freely restart, shut down, and do some cleanup work before exiting ( break links, delete temporary files, etc.)?

C signal processing example

Signal handling for PHP programmers

In the above picture, I registered a processing function for the signalSIGINTsigint_handle, after capturing the signal, output the content and exit, it is simple and easy to understand. Executegcc -o run run.c && ./run, thenCTRL C(theSIGINTsignal will be triggered), successful output:Signal captured successfully 2!, the program ends directly.

PHP signal processing example

Signal handling for PHP programmers

pcntl_signalis PHP’s signal processing registration method, implemented above The function is basically the same as that implemented in C. The difference is that the current process will not exit, and an additionalsigninfois output (PHP is written in C, why is there no signal-related information in the C language just now? Because PHP uses another signal functionsigaction, those who are interested can learn more)

PHP’s signal processing does not directly call C

Signal handling for PHP programmers

This is when pcntl is initialized, registerpcntl_signal_dispatchas the tick processing function

Signal handling for PHP programmers

pcntl_signalwill register the processing function Put it into the signal collection (PHP's hash table), andphp_signale4will eventually callsigactionfor underlying signal management.

Signal handling for PHP programmers

I have omitted a lot of code here and marked the key points. In fact, PHP maintains its own signal collection. Wheneverpcntl_signal_dispatchis called It will query whether there is a signal. The aboveSIG_BLOCKwill block the signal, so that only after we execute the key code, we can trigger the signal processing function to ensure the integrity of the data and program logic.

How PHP handles signals gracefully

I often see programmers around me. Whenever they need to restart thePHP-FPMprocess, the trick they use is to kill it. All PHP processes are then newly started. In general, there is no problem, but sometimes the task of a certain process may not be completed, and it would be a bit rude to interrupt it directly. In fact, as long as you send aUSR2signal to the PHP Master process, it will restart the child process after processing all tasks. This is the so-called elegance~

Signal handling for PHP programmers

The picture above is an example I simply wrote. If we want the process to exit gracefully, we only need to send theSIGTERMsignal. It should be noted that theSIGKILLandSIGSTOPsignals will skip the signal blocking and stop the process directly, and the signal will interrupt sleep (SLEEP),sleepIf If the execution is not completed, the remaining seconds will be returned. If you are interested, you can try it.

There are actually many knowledge points related to signals, and we need to continue to study them in depth~ The PHP source code above is version 7.1.25, and each version may be different.

The above is the detailed content of Signal handling for PHP programmers. For more information, please follow other related articles on the PHP Chinese website!

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