Home > Backend Development > PHP Tutorial > PHP learning the operation of signal processing mechanism

PHP learning the operation of signal processing mechanism

little bottle
Release: 2023-04-06 06:02:01
forward
2321 people have browsed it

This article mainly describes examples of signal processing mechanisms written in PHP language. It has certain reference value. Interested friends can come and learn about it.

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);
}
Copy after login

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

posix_signal sets the callback processing of the signal.

posix_kill only puts the signal into the signal pending queue of the process. It does not trigger the signal callback. The signal in the signal queue is processed by pcntl_signal_dispatch.

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 connecting has been closed, the process continues to send data to the invalid socket, the system will receive a TCP packet containing the RST control bit, and the system will send a SIGPIPE signal to the process, telling the process that the connection has been disconnected and no more writing is required. The default handling of this signal is to terminate the process, but the process can catch it and ignore the signal to avoid being terminated unwillingly.

socket context options:

backlog: used to limit the number of outstanding connections in the stream listening queue

so_reuseport: reuse port (connected to by kernel scheduling There are multiple processes listening on the same port. Since the corresponding process is marked by hashing, the number of listening processes cannot be changed.)

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);
}
Copy after login

Related tutorials: php programming from entry to proficiency

The above is the detailed content of PHP learning the operation of signal processing mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template