php pcntl_signal has no effect because pcntl will automatically restart the interrupted system call by default. The external performance is that the system call cannot be interrupted. The solution is to set the system call not to be restarted.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if php pcntl_signal does not work?
php pcntl_signal usage and precautions
Recently I need to build a multi-process server based on php. In order to restart gracefully, you need to capture the SIGTERM signal sent by kill, so pcntl_signal is used.
At first I found that the captured signal was invalid and could not enter the signal processing function. I studied the document and found that I need to run declare(ticks = 1); the explanation says that it is to generate a clock and so on. My understanding is similar to a crystal oscillator. After adding, the parent process can receive the signal.
But the child process still cannot receive the signal. After checking for a long time, I did not see any exception. The child process usually blocks socket_accept. I suspected that the system call cannot be interrupted here, so I changed it to a while 1 loop. , you can successfully enter the interrupt processing function.
Then I searched and found that pcntl will automatically restart the interrupted system call by default, and the external performance is that it cannot interrupt the system call.
Looking back at the manual at this time, it turns out that pcntl_signal has a third parameter, which is also an optional parameter. It allows us to decide whether to automatically restart the system call. Just choose not to restart.
Introduction to 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 function or method created by the user, or it can be the system constant SIG_IGN (Annotation: Ignore signal handler) or SIG_DFL (default signal handler).
Note:
Note that when you use an object method, the object's reference count is incremented so that it persists until you change to other processing or the script ends.
restart_syscalls
Specifies whether system call restart is available when the signal arrives. (Translation note: After checking the information, this parameter means whether the system call is restarted from the beginning when the system call is interrupted by a signal. However, according to http://bugs.php.net/bug.php?id=52121, this parameter There is a bug and it is invalid.)
The above is the detailed content of What should I do if php pcntl_signal has no effect?. For more information, please follow other related articles on the PHP Chinese website!