man 7 signal
这是您经常希望查看Linux信号信息的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具有动作Core
和SIGINT ,因此总结了区分SIGQUIT和SIGQUIT的信号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
并且都可以被捕获。看来这只是“常用用法约定的区别”:
- 从终端执行CTRL-C时会发生SIGINT
- SIGTERM是发送的默认信号
kill
有些信号是ANSI C,有些则不是
一个显着的区别是:
- SIGINT和SIGTERM是ANSI C,因此更具可移植性
- SIGQUIT和SIGKILL不是
它们在C99 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初始化
BusyBox的1.29.2默认reboot
命令将SIGTERM发送到进程,休眠一秒钟,然后发送SIGKILL。这似乎是不同发行版之间的通用约定。
当您通过以下方式关闭BusyBox系统时:
reboot
它向初始化过程发送信号。
然后,init信号处理程序最终调用:
static void run_shutdown_and_kill_processes(void)
{
/* Run everything to be run at "shutdown". This is done _prior_
* to killing everything, in case people wish to use scripts to
* shut things down gracefully... */
run_actions(SHUTDOWN);
message(L_CONSOLE | L_LOG, "The system is going down NOW!");
/* Send signals to every process _except_ pid 1 */
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();
/*sleep(1); - callers take care about making a pause */
}
打印到终端:
The system is going down NOW!
Sent SIGTERM to all processes
Sent SIGKILL to all processes
这是一个最小的具体示例。
内核发送的信号