signal

什么是 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) {
            System.out.println(counter++);
            Thread.sleep(500);
        }
    }
}
  1. 可以将 “INT” 替换为 “STOP” 运行结果为:
    Exception in thread "main" java.lang.IllegalArgumentException: Signal already used by VM or OS: SIGSTOP
     at sun.misc.Signal.handle(Signal.java:166)
     at ExampleSignalHandler.main(ExampleSignalHandler.java:9)
    
  2. 可以将 “INT” 替换为 “KILL” 运行结果为:
    Exception in thread "main" java.lang.IllegalArgumentException: Signal already used by VM or OS: SIGKILL
     at sun.misc.Signal.handle(Signal.java:166)
     at stardustman.github.io.signal.ExampleSignalHandler.main(ExampleSignalHandler.java:9)
    

References

  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
  11. https://www.cs.princeton.edu/courses/archive/fall05/cos217/lectures/23signals.pdf
  12. cs.kent.edu/signals.html
  13. csci480/signals.htm
  14. kill(2)
  15. https://goodyduru.github.io/os/2023/10/05/ipc-unix-signals.html
  16. java Signal 处理实现