man 7 signal
这是Linux man-pages project的便捷非规范手册,您经常需要查看其中的Linux信号信息。
版本3.22提到了一些有趣的事情,例如:
信号SIGKILL和SIGSTOP无法被捕获、阻止或忽略。
并包含以下表格:
Signal Value Action Comment
----------------------------------------------------------------------
SIGHUP 1 Term Hangup detected on controlling terminal
or death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: write to pipe with no
readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at tty
SIGTTIN 21,21,26 Stop tty input for background process
SIGTTOU 22,22,27 Stop tty output for background process
这段文字概括了信号中的Action
,用于区分例如SIGQUIT和SIGINT等不同信号,因为SIGQUIT的操作是Core
,而SIGINT的操作是Term
。
这些操作在同一文档中有详细记录:
The entries in the "Action" column of the tables below specify the default disposition for each signal, as follows:
Term Default action is to terminate the process.
Ign Default action is to ignore the signal.
Core Default action is to terminate the process and dump core (see core(5)).
Stop Default action is to stop the process.
Cont Default action is to continue the process if it is currently stopped.
就内核而言,我无法看出SIGTERM和SIGINT之间的任何区别,因为两者都有Term
操作,并且都可以被捕获。似乎这只是一种“常见用法约定区别”:
- SIGINT是从终端执行CTRL-C时发生的情况
- SIGTERM是由
kill
发送的默认信号
某些信号是ANSI C而其他信号不是
一个相当大的区别是:
- SIGINT和SIGTERM是ANSI C,因此更具可移植性
- SIGQUIT和SIGKILL则不是
它们在C99 draft N1256的“7.14信号处理”部分中进行了描述:
- SIGINT交互式注意信号的接收
- SIGTERM向程序发送终止请求
这使得SIGINT成为交互式Ctrl + C的理想候选。
POSIX 7
POSIX 7文件使用signal.h
头文件记录了信号: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
此页面还有一个相关的表格,提到了我们已经在man 7 signal
中看到的一些内容:
Signal Default Action Description
SIGABRT A Process abort signal.
SIGALRM T Alarm clock.
SIGBUS A Access to an undefined portion of a memory object.
SIGCHLD I Child process terminated, stopped,
SIGCONT C Continue executing, if stopped.
SIGFPE A Erroneous arithmetic operation.
SIGHUP T Hangup.
SIGILL A Illegal instruction.
SIGINT T Terminal interrupt signal.
SIGKILL T Kill (cannot be caught or ignored).
SIGPIPE T Write on a pipe with no one to read it.
SIGQUIT A Terminal quit signal.
SIGSEGV A Invalid memory reference.
SIGSTOP S Stop executing (cannot be caught or ignored).
SIGTERM T Termination signal.
SIGTSTP S Terminal stop signal.
SIGTTIN S Background process attempting read.
SIGTTOU S Background process attempting write.
SIGUSR1 T User-defined signal 1.
SIGUSR2 T User-defined signal 2.
SIGTRAP A Trace/breakpoint trap.
SIGURG I High bandwidth data is available at a socket.
SIGXCPU A CPU time limit exceeded.
SIGXFSZ A File size limit exceeded.
忙碌箱初始化
BusyBox 1.29.2 默认的 reboot
命令会向进程发送 SIGTERM 信号,等待一秒钟,然后发送 SIGKILL 信号。这似乎是不同发行版之间的常见惯例。
当您关闭 BusyBox 系统时:
reboot
我会尽力帮助您进行翻译。以下是需要翻译的内容:
它向 init 进程发送一个信号。
然后,init 信号处理程序最终会调用:
static void run_shutdown_and_kill_processes(void)
{
run_actions(SHUTDOWN);
message(L_CONSOLE | L_LOG, "The system is going down NOW!");
kill(-1, SIGTERM);
message(L_CONSOLE, "Sent SIG%s to all processes", "TERM");
sync();
sleep(1);
kill(-1, SIGKILL);
message(L_CONSOLE, "Sent SIG%s to all processes", "KILL");
sync();
}
这将打印到终端:
The system is going down NOW!
Sent SIGTERM to all processes
Sent SIGKILL to all processes
这里是一个最简单的实例。
内核发送的信号
sudo fuser -l
来找到它们。对我来说,这会带出:HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS UNUSED
。 - Nick Bullkill -l
命令获取信号列表。 - AAAfarmclub