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
In the above picture, I registered a processing function for the signalSIGINT
sigint_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
(theSIGINT
signal will be triggered), successful output:Signal captured successfully 2!
, the program ends directly.
PHP signal processing example
pcntl_signal
is 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 additionalsigninfo
is 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
This is when pcntl is initialized, registerpcntl_signal_dispatch
as the tick processing function
pcntl_signal
will register the processing function Put it into the signal collection (PHP's hash table), andphp_signale4
will eventually callsigaction
for underlying signal management.
I have omitted a lot of code here and marked the key points. In fact, PHP maintains its own signal collection. Wheneverpcntl_signal_dispatch
is called It will query whether there is a signal. The aboveSIG_BLOCK
will 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-FPM
process, 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 aUSR2
signal to the PHP Master process, it will restart the child process after processing all tasks. This is the so-called elegance~
The picture above is an example I simply wrote. If we want the process to exit gracefully, we only need to send theSIGTERM
signal. It should be noted that theSIGKILL
andSIGSTOP
signals will skip the signal blocking and stop the process directly, and the signal will interrupt sleep (SLEEP),sleep
If 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.