Signal handling for PHP programmers

藏色散人
Release: 2023-04-09 18:06:02
forward
3232 people have browsed it

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 signal SIGINT sigint_handle, after capturing the signal, output the content and exit, it is simple and easy to understand. Execute gcc -o run run.c && ./run, then CTRL C (the SIGINT signal will be triggered), successful output: Signal captured successfully 2!, the program ends directly.

PHP signal processing example

Signal handling for PHP programmers

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 additional signinfo 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 function sigaction, 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, register pcntl_signal_dispatch as the tick processing function

Signal handling for PHP programmers

pcntl_signal will register the processing function Put it into the signal collection (PHP's hash table), and php_signale4 will eventually call sigaction for 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. Whenever pcntl_signal_dispatch is called It will query whether there is a signal. The above SIG_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 the PHP-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 a USR2 signal 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 the SIGTERM signal. It should be noted that the SIGKILL and SIGSTOP signals 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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template