Get the App
SLTechnology News&Howtos  ›  Development  › 

Detailed introduction of PHP signal processing mechanism

Shulou Source: shulou.com Published: 2022-06-03 19:51:18 10月03日 Update

This article mainly explains the "detailed introduction of PHP signal processing mechanism". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed introduction of PHP signal processing mechanism".

Let's first take a look at the example code:

Function sig_handler ($sig) {print ("handled sig: $sig\ n");} pcntl_signal (SIGIO, "sig_handler"); posix_kill (posix_getpid (), SIGIO); while (true) {posix_kill (posix_getpid (), SIGIO); pcntl_signal_dispatch (); sleep (1);}

It is best to cycle through the signal queue manually instead of using the signal processing mechanism of declare (ticks=1) and tick_handler () provided by php. Because of the performance problems of the tick mechanism, every statement executed is called back to tick_handler to see if there is a signal, and most of the time there is no signal.

Callback processing of posix_signal setting signal

Posix_kill simply puts the signal into the signal waiting queue of the process, which does not trigger the signal callback, and the pcntl_signal_dispatch processes the signal in the signal queue.

Posix_getpwnam ("nginx"): get the uid,gid and other information of the user name

Pcntl_signal (SIGPIPE, SIG_IGN, false): ignore the SIGPIPE signal sent by the kernel. When the connection has been closed and the process continues to send data to invalid socket, the system will receive a TCP packet with RST control bit, and the system will send a SIGPIPE signal to the process, telling the process that the connection has been disconnected and do not write any more. The default processing of this signal is to terminate the process, and the process can capture it and ignore the signal to avoid being terminated reluctantly.

Socket context options:

Backlog: used to limit the number of outstanding connections in the flow monitoring queue

So_reuseport: reuse port (the kernel schedules multiple processes listening to the same port, and the number of listening processes cannot be changed because the connected processes are marked by hash.)

Timer signal processing

Pcntl_signal (SIGALRM, "sig_handler"); pcntl_alarm (2); function sig_handler ($sig) {echo "one second after";} while (1) {pcntl_signal_dispatch (); sleep (1) Thank you for your reading. The above is the content of "detailed introduction of PHP signal processing mechanism". After the study of this article, I believe you have a deeper understanding of the detailed introduction of PHP signal processing mechanism, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Signal processing process signal processing mechanism queue learning monitoring content kernel port system problem large up and down context code information multiple timer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Microsoft Xiaomi Docker