为什么许多Unix程序使用USR1这样的信号?

时间:2022-08-26 12:37:28

Many Unix programs accept signals like USR1 and USR2. For example, to upgrade the executable for Nginx on the fly, you send kill -USR2.

许多Unix程序接受USR1和USR2这样的信号。例如,要动态升级Nginx的可执行文件,您需要发送kill -USR2。

I understand that USR1 is a "user defined" signal, meaning that whoever created the program can use it to mean "shut down" or "dump your logs" or "print foo a thousand times" or whatever. But I don't understand why they must use this arbitrary name. Why not kill -UPGRADE, or kill -GRACEFUL_SHUTDOWN? Does Unix only allow specific signals?

我理解USR1是一个“用户定义的”信号,这意味着任何创建程序的人都可以使用它来表示“关闭”或“转储您的日志”或“打印foo 1000次”或其他。但是我不明白他们为什么要用这个任意的名字。为什么不取消升级,或者取消优雅的关机?Unix是否只允许特定的信号?

While we're at it, Nginx also uses the following signals (see documentation):

在此期间,Nginx还使用了以下信号(参见文档):

  • TERM, INT: Quick shutdown
  • 项,INT:快速关闭
  • QUIT: Graceful shutdown
  • 戒烟:优雅的关闭
  • HUP:
    • Configuration reload
    • 重新加载配置
    • Start the new worker processes with a new configuration
    • 使用新的配置启动新的worker进程
    • Gracefully shutdown the old worker processes
    • 优雅地关闭老工作进程
  • HUP:配置重载,使用新的配置启动新的worker进程,优雅地关闭旧的worker进程
  • USR1: Reopen the log files
  • USR1:重新打开日志文件
  • USR2: Upgrade Executable on the fly
  • 动态升级可执行文件
  • WINCH: Gracefully shutdown the worker processes
  • WINCH:优雅地关闭工作进程。

HUP? WINCH? What's the reason for these names? Where can I learn more about this?

玫瑰吗?绞车吗?这些名字的原因是什么?我在哪里可以学到更多?

6 个解决方案

#1


70  

The signals available on an OS are defined by the OS (usually following POSIX) - they're not "strings" but rather integer constants with standard names. USR1 and USR2 are the two signals that have no attached specific meaning - intended for whatever arbitrary use the developer wants.

操作系统上可用的信号由操作系统定义(通常遵循POSIX)——它们不是“字符串”,而是具有标准名称的整数常量。USR1和USR2是两个没有附加特定含义的信号——用于开发人员想要的任意用途。

On your linux machine, read man 7 signal for an overview of signal handling and signals.

在您的linux机器上,请阅读man 7信号,以了解信号处理和信号的概述。

You can redefine the meaning of other signals if you're prepared to deal with the OS issuing those signals in response to events. You can e.g. make HUP mean "reload configuration" - as long as you're either certain that the process will never get a hangup (terminal loss), or you're prepared to handle cases where the OS and not a user sends the HUP signal.

如果您准备处理响应事件而发出这些信号的操作系统,您可以重新定义其他信号的含义。你可以做HUP的意思是“重新加载配置”——只要你确定这个过程永远不会出现挂起(终端丢失),或者你准备处理操作系统而不是用户发送HUP信号的情况。

#2


18  

HUP is short for "hang-up". This signal is sent to a process if its controlling terminal reaches end-of-file. In the old days, controlling terminals were typically attached to serial ports, possibly via a modem link over a telephone line. If the telephone connection was hung up, the local modem would lower the Carrier Detect line, which would result in the kernel reporting end-of-file and a SIGHUP signal being sent.

HUP是“挂起”的缩写。如果进程的控制终端到达文件的末尾,这个信号就被发送到进程。在过去,控制终端通常连接到串行端口,可能通过电话线上的调制解调器连接。如果电话连接被挂起,本地调制解调器将降低载波检测线路,这将导致内核报告结束文件并发送一个SIGHUP信号。

WINCH is short for "window change". It is sent to a process if its controlling terminal changes size. For obvious reasons, terminals that can change size are usually pseudo-terminals ultimately represented by a terminal emulator running in a windowing environment (like xterm).

WINCH是“窗口更改”的缩写。如果它的控制终端改变了大小,它将被发送到进程。由于明显的原因,可以更改大小的终端通常是伪终端,最终由运行在窗口环境中的终端仿真器(如xterm)表示。

#3


8  

Because the names of the signals are standardised (by POSIX). You can write your own kill-type executable to take -UPGRADE if you want and have it deliver the USR1 signal, but the standard kill that comes with UNIX will not recognise it.

因为信号的名称是标准化的(通过POSIX)。您可以编写自己的killtype可执行文件来进行-UPGRADE(如果您愿意的话),并让它交付USR1信号,但是UNIX附带的标准kill不会识别它。

Alternatively, you could create an alias, function or shell script to do the translation for you, such as with the bash alias:

或者,您可以创建一个别名、函数或shell脚本来为您执行翻译,例如使用bash别名:

alias upgrade='kill -USR1'

The signal.h header file maps the signal names to their actual values, which are implementation-dependent.

这个信号。h标头文件将信号名映射到与实现相关的实际值。

In terms of WINCH, I consider this a bit of an abomination. This is the signal that is delivered to applications when their window size changes (specifically when the window of their controlling terminal changes).

就绞车而言,我认为这有点令人厌恶。这是当窗口大小发生变化时(特别是当控制终端的窗口发生变化时)发送给应用程序的信号。

Using it to gracefully shut down worker threads is not a good idea unless you can guarantee the process will never run in a terminal. I know I'd be pretty miffed if I was running an application and it decided to cancel all work in flight just because I maximised the window :-)

使用它来优雅地关闭工作线程不是一个好主意,除非你能保证这个过程永远不会在终端运行。我知道如果我在运行一个应用程序,它决定取消所有飞行中的工作,因为我最大化了窗口:-)

#4


6  

Try kill -l and find the answer yourself:

试试杀了-l,自己找到答案:

1)  SIGHUP       2)  SIGINT       3)  SIGQUIT      4)  SIGILL       5)  SIGTRAP
6)  SIGABRT      7)  SIGBUS       8)  SIGFPE       9)  SIGKILL      10) SIGUSR1
11) SIGSEGV      12) SIGUSR2      13) SIGPIPE      14) SIGALRM      15) SIGTERM
16) SIGSTKFLT    17) SIGCHLD      18) SIGCONT      19) SIGSTOP      20) SIGTSTP
21) SIGTTIN      22) SIGTTOU      23) SIGURG       24) SIGXCPU      25) SIGXFSZ
26) SIGVTALRM    27) SIGPROF      28) SIGWINCH     29) SIGIO        30) SIGPWR
31) SIGSYS       34) SIGRTMIN     35) SIGRTMIN+1   36) SIGRTMIN+2   37) SIGRTMIN+3
38) SIGRTMIN+4   39) SIGRTMIN+5   40) SIGRTMIN+6   41) SIGRTMIN+7   42) SIGRTMIN+8
43) SIGRTMIN+9   44) SIGRTMIN+10  45) SIGRTMIN+11  46) SIGRTMIN+12  47) SIGRTMIN+13
48) SIGRTMIN+14  49) SIGRTMIN+15  50) SIGRTMAX-14  51) SIGRTMAX-13  52) SIGRTMAX-12
53) SIGRTMAX-11  54) SIGRTMAX-10  55) SIGRTMAX-9   56) SIGRTMAX-8   57) SIGRTMAX-7
58) SIGRTMAX-6   59) SIGRTMAX-5   60) SIGRTMAX-4   61) SIGRTMAX-3   62) SIGRTMAX-2
63) SIGRTMAX-1   64) SIGRTMAX

#5


4  

On POSIX-compliant platforms, SIGUSR1 and SIGUSR2 are signals sent to a process to indicate user-defined conditions. The symbolic constants for them are defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms.

在与posix兼容的平台上,SIGUSR1和SIGUSR2是发送给进程的信号,以指示用户定义的条件。它们的符号常量定义在header文件signal.h中。之所以使用符号信号名,是因为不同平台的信号数可能不同。

SIG is a common prefix for signal names. USR is an abbreviation for user-defined.

SIG是信号名的常见前缀。USR是用户定义的缩写。

#6


4  

Signal names originate from earlier times than Posix.

信号名起源于比Posix更早的时代。

I want to talk about SIG**IOT**. In the times when DEC PDP mainframes were used, the processor(s) used had a special IOT instruction (I/O Trap) which was often used to gently crash the system--usually forcing it to reboot (in realtime servers). The whole kernel along with the device drivers, and privileged processes, (written in assembler), used this method. Even today, there are processors that still have this IOT instruction.

我想谈谈生活。在使用DEC PDP大型机的时候,所使用的处理器有一个特殊的物联网指令(I/O Trap),它通常用于温柔地破坏系统——通常迫使系统重新启动(在实时服务器中)。整个内核以及设备驱动程序和特权进程(用汇编程序编写)都使用了这种方法。即使在今天,仍然有处理器具有这种物联网指令。

So, when kernel experiences an execution of an IOT instruction in unprivileged domain, it raises a SIGIOT to the affected process.

因此,当内核在非特权领域执行物联网指令时,它会对受影响的进程提出一个问题。

#1


70  

The signals available on an OS are defined by the OS (usually following POSIX) - they're not "strings" but rather integer constants with standard names. USR1 and USR2 are the two signals that have no attached specific meaning - intended for whatever arbitrary use the developer wants.

操作系统上可用的信号由操作系统定义(通常遵循POSIX)——它们不是“字符串”,而是具有标准名称的整数常量。USR1和USR2是两个没有附加特定含义的信号——用于开发人员想要的任意用途。

On your linux machine, read man 7 signal for an overview of signal handling and signals.

在您的linux机器上,请阅读man 7信号,以了解信号处理和信号的概述。

You can redefine the meaning of other signals if you're prepared to deal with the OS issuing those signals in response to events. You can e.g. make HUP mean "reload configuration" - as long as you're either certain that the process will never get a hangup (terminal loss), or you're prepared to handle cases where the OS and not a user sends the HUP signal.

如果您准备处理响应事件而发出这些信号的操作系统,您可以重新定义其他信号的含义。你可以做HUP的意思是“重新加载配置”——只要你确定这个过程永远不会出现挂起(终端丢失),或者你准备处理操作系统而不是用户发送HUP信号的情况。

#2


18  

HUP is short for "hang-up". This signal is sent to a process if its controlling terminal reaches end-of-file. In the old days, controlling terminals were typically attached to serial ports, possibly via a modem link over a telephone line. If the telephone connection was hung up, the local modem would lower the Carrier Detect line, which would result in the kernel reporting end-of-file and a SIGHUP signal being sent.

HUP是“挂起”的缩写。如果进程的控制终端到达文件的末尾,这个信号就被发送到进程。在过去,控制终端通常连接到串行端口,可能通过电话线上的调制解调器连接。如果电话连接被挂起,本地调制解调器将降低载波检测线路,这将导致内核报告结束文件并发送一个SIGHUP信号。

WINCH is short for "window change". It is sent to a process if its controlling terminal changes size. For obvious reasons, terminals that can change size are usually pseudo-terminals ultimately represented by a terminal emulator running in a windowing environment (like xterm).

WINCH是“窗口更改”的缩写。如果它的控制终端改变了大小,它将被发送到进程。由于明显的原因,可以更改大小的终端通常是伪终端,最终由运行在窗口环境中的终端仿真器(如xterm)表示。

#3


8  

Because the names of the signals are standardised (by POSIX). You can write your own kill-type executable to take -UPGRADE if you want and have it deliver the USR1 signal, but the standard kill that comes with UNIX will not recognise it.

因为信号的名称是标准化的(通过POSIX)。您可以编写自己的killtype可执行文件来进行-UPGRADE(如果您愿意的话),并让它交付USR1信号,但是UNIX附带的标准kill不会识别它。

Alternatively, you could create an alias, function or shell script to do the translation for you, such as with the bash alias:

或者,您可以创建一个别名、函数或shell脚本来为您执行翻译,例如使用bash别名:

alias upgrade='kill -USR1'

The signal.h header file maps the signal names to their actual values, which are implementation-dependent.

这个信号。h标头文件将信号名映射到与实现相关的实际值。

In terms of WINCH, I consider this a bit of an abomination. This is the signal that is delivered to applications when their window size changes (specifically when the window of their controlling terminal changes).

就绞车而言,我认为这有点令人厌恶。这是当窗口大小发生变化时(特别是当控制终端的窗口发生变化时)发送给应用程序的信号。

Using it to gracefully shut down worker threads is not a good idea unless you can guarantee the process will never run in a terminal. I know I'd be pretty miffed if I was running an application and it decided to cancel all work in flight just because I maximised the window :-)

使用它来优雅地关闭工作线程不是一个好主意,除非你能保证这个过程永远不会在终端运行。我知道如果我在运行一个应用程序,它决定取消所有飞行中的工作,因为我最大化了窗口:-)

#4


6  

Try kill -l and find the answer yourself:

试试杀了-l,自己找到答案:

1)  SIGHUP       2)  SIGINT       3)  SIGQUIT      4)  SIGILL       5)  SIGTRAP
6)  SIGABRT      7)  SIGBUS       8)  SIGFPE       9)  SIGKILL      10) SIGUSR1
11) SIGSEGV      12) SIGUSR2      13) SIGPIPE      14) SIGALRM      15) SIGTERM
16) SIGSTKFLT    17) SIGCHLD      18) SIGCONT      19) SIGSTOP      20) SIGTSTP
21) SIGTTIN      22) SIGTTOU      23) SIGURG       24) SIGXCPU      25) SIGXFSZ
26) SIGVTALRM    27) SIGPROF      28) SIGWINCH     29) SIGIO        30) SIGPWR
31) SIGSYS       34) SIGRTMIN     35) SIGRTMIN+1   36) SIGRTMIN+2   37) SIGRTMIN+3
38) SIGRTMIN+4   39) SIGRTMIN+5   40) SIGRTMIN+6   41) SIGRTMIN+7   42) SIGRTMIN+8
43) SIGRTMIN+9   44) SIGRTMIN+10  45) SIGRTMIN+11  46) SIGRTMIN+12  47) SIGRTMIN+13
48) SIGRTMIN+14  49) SIGRTMIN+15  50) SIGRTMAX-14  51) SIGRTMAX-13  52) SIGRTMAX-12
53) SIGRTMAX-11  54) SIGRTMAX-10  55) SIGRTMAX-9   56) SIGRTMAX-8   57) SIGRTMAX-7
58) SIGRTMAX-6   59) SIGRTMAX-5   60) SIGRTMAX-4   61) SIGRTMAX-3   62) SIGRTMAX-2
63) SIGRTMAX-1   64) SIGRTMAX

#5


4  

On POSIX-compliant platforms, SIGUSR1 and SIGUSR2 are signals sent to a process to indicate user-defined conditions. The symbolic constants for them are defined in the header file signal.h. Symbolic signal names are used because signal numbers can vary across platforms.

在与posix兼容的平台上,SIGUSR1和SIGUSR2是发送给进程的信号,以指示用户定义的条件。它们的符号常量定义在header文件signal.h中。之所以使用符号信号名,是因为不同平台的信号数可能不同。

SIG is a common prefix for signal names. USR is an abbreviation for user-defined.

SIG是信号名的常见前缀。USR是用户定义的缩写。

#6


4  

Signal names originate from earlier times than Posix.

信号名起源于比Posix更早的时代。

I want to talk about SIG**IOT**. In the times when DEC PDP mainframes were used, the processor(s) used had a special IOT instruction (I/O Trap) which was often used to gently crash the system--usually forcing it to reboot (in realtime servers). The whole kernel along with the device drivers, and privileged processes, (written in assembler), used this method. Even today, there are processors that still have this IOT instruction.

我想谈谈生活。在使用DEC PDP大型机的时候,所使用的处理器有一个特殊的物联网指令(I/O Trap),它通常用于温柔地破坏系统——通常迫使系统重新启动(在实时服务器中)。整个内核以及设备驱动程序和特权进程(用汇编程序编写)都使用了这种方法。即使在今天,仍然有处理器具有这种物联网指令。

So, when kernel experiences an execution of an IOT instruction in unprivileged domain, it raises a SIGIOT to the affected process.

因此,当内核在非特权领域执行物联网指令时,它会对受影响的进程提出一个问题。