什么是 signal

Signals are various notifications sent to a process in order to notify it of various “important” events. 信号是发送给进程的各种通知,以便通知它发生了各种“重要”的事件。


Signal NameDefault ActionCommentPOSIX
1 SIGHUPTerminateHang up controlling terminal or processYes
2 SIGINTTerminateInterrupt from keyboard, Control-CYes
3 SIGQUITDumpQuit from keyboard, Control-\Yes
4 SIGILLDumpIllegal instructionYes
5 SIGTRAPDumpBreakpoint for debuggingNo
6 SIGABRTDumpAbnormal terminationYes
6 SIGIOTDumpEquivalent to SIGABRTNo
7 SIGBUSDumpBus errorNo
8 SIGFPEDumpFloating-point exceptionYes
9 SIGKILLTerminateForced-process terminationYes
10 SIGUSR1TerminateAvailable to processesYes
11 SIGSEGVDumpInvalid memory referenceYes
12 SIGUSR2TerminateAvailable to processesYes
13 SIGPIPETerminateWrite to pipe with no readersYes
14 SIGALRMTerminateReal-timer clockYes
15 SIGTERMTerminateProcess terminationYes
16 SIGSTKFLTTerminateCoprocessor stack errorNo
17 SIGCHLDIgnoreChild process stopped or terminated or got a signal if tracedYes
18 SIGCONTContinueResume execution, if stoppedYes
19 SIGSTOPStopStop process execution, Ctrl-ZYes
20 SIGTSTPStopStop process issued from ttyYes
21 SIGTTINStopBackground process requires inputYes
22 SIGTTOUStopBackground process requires outputYes
23 SIGURGIgnoreUrgent condition on socketNo
24 SIGXCPUDumpCPU time limit exceededNo
25 SIGXFSZDumpFile size limit exceededNo
26 SIGVTALRMTerminateVirtual timer clockNo
27 SIGPROFTerminateProfile timer clockNo
28 SIGWINCHIgnoreWindow resizingNo
29 SIGIOTerminateI/O now possibleNo
29 SIGPOLLTerminateEquivalent to SIGIONo
30 SIGPWRTerminatePower supply failureNo
31 SIGSYSDumpBad system callNo
31SIGUNUSEDDumpEquivalent to SIGSYSNo


1. keyboard

  1. SIGINT(Ctrl + C)
  2. SIGTSTP(Ctrl + Z) -> resume by fg
  3. SIGQUIT(Ctrl + \)

2. command line

  1. kill -<signal> <PID>
  2. fg

3. system call

kill syscall


分为 3 种情况:

  1. Ignore it. Many signals can be and are ignored, but not all. Hardware exceptions such as “divide by 0” (with integers) cannot be ignored successfully and some signals such as SIGKILL cannot be ignored at all.
  2. Catch and handle the exception. The process has a function to be executed if and when the exception occurs. The function may terminate the program gracefully or it may handle it without terminating the program.
  3. Let the default action apply. Every signal has a default action. The default may be:
    • ignore
    • terminate
    • terminate and dump core
    • stop or pause the program
    • resume a program paused earlier

如何自定义 signal handler?

注意:这里只有可以被 caught 的才可以自定义 handler。SIGKILL 和 SIGSTOP 这两个是不能自己定义 handler 的。

java example

import sun.misc.Signal;
import sun.misc.SignalHandler;

public class ExampleSignalHandler {
    public static void main(String... args) throws InterruptedException {
        final long start = System.nanoTime();
        Signal.handle(new Signal("INT"), new SignalHandler() {
            public void handle(Signal sig) {
                System.out.format("\nProgram execution took %f seconds\n", (System.nanoTime() - start) / 1e9f);
                System.out.println("cat the INT signal, but still run.....😄");
        int counter = 0;
        while(true) {
  1. 可以将 “INT” 替换为 “STOP” 运行结果为:
    Exception in thread "main" java.lang.IllegalArgumentException: Signal already used by VM or OS: SIGSTOP
     at sun.misc.Signal.handle(
     at ExampleSignalHandler.main(
  2. 可以将 “INT” 替换为 “KILL” 运行结果为:
    Exception in thread "main" java.lang.IllegalArgumentException: Signal already used by VM or OS: SIGKILL
     at sun.misc.Signal.handle(


  1. Linux Signal Table
  2. Signal_(IPC)
  3. Introduction To Unix Signals Programming
  4. Standard-Signals
  5. handle-a-signal
  6. signal-block
  7. handling-unix-signals-in-python
  8. signals
  9. signal-handling-in-linux
  10. nohup
  13. csci480/signals.htm
  14. kill(2)