Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem that php pcntl_signal has no effect

Shulou Source: shulou.com Published: 2022-06-03 05:22:57 09月28日 Update

This article introduces the relevant knowledge of "how to solve the problem of no effect of php pcntl_signal". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Php pcntl_signal has no effect because pcntl automatically restarts interrupted system calls by default, and the external performance is that the blocking of system calls cannot be interrupted. The solution is to set not to restart system calls.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

What if php pcntl_signal doesn't work?

Pcntl_signal usage of php and matters needing attention

Recently, we need to do a multi-process server based on php. In order to restart gracefully, you need to capture the SIGTERM signal from kill, so pcntl_signal is used.

At first, it was found that the captured signal was invalid and could not enter the signal processing function. After studying the documentation, I found that I want to run declare (ticks = 1); the explanation is that it is to generate a clock and so on, and my understanding is similar to crystal oscillator. After adding, the parent process can receive the signal.

However, the child process is still unable to receive the signal, and after troubleshooting for a long time, it does not see anything abnormal. The child process usually blocks the socket_accept, and it is suspected that the system call cannot be interrupted here, so it is changed to a while 1 loop, which can successfully enter the interrupt handling function.

Then search, it turns out that pcntl automatically restarts interrupted system calls by default, and the external performance is that the blocking of system calls cannot be interrupted.

Looking back at the manual at this time, it turns out that pcntl_signal has a third parameter, which is also an optional parameter, that is, let us decide whether to restart the system call automatically or not.

Brief introduction of pcntl_signal function

(PHP 4 > = 4.1.0, PHP 5, PHP 7, PHP 8)

Pcntl_signal-install a signal processor

Description

Pcntl_signal (int $signo, callback $handler, bool $restart_syscalls = true): bool

The function pcntl_signal () installs a new signal processor for the signal specified by signo.

Parameters.

Signo

Signal number.

Handler

The signal handler can be the name of a user-created function or method, or it can be the system constant SIG_IGN or SIG_DFL.

Note:

Note that when you use an object method, the reference count of the object increases so that it persists until you change to another processing or the end of the script.

Restart_syscalls

Specifies whether the system call restart is available when the signal arrives. Note: after looking up the data, this parameter means whether the system call restarts from the beginning when the system call is interrupted by the signal, but according to http://bugs.php.net/bug.php?id=52121, the existence of bug for this parameter is invalid. )

This is the end of the content of "how to solve the problem of no effect in php pcntl_signal". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Signals systems functions parameters processing effects processes signal processing signal processors blocking problems content objects that is methods more knowledge programs translation notes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS Huawei MySQL Shulou Technology