[Linux operating system] You must know these before learning the signal part of Linux system programming

This article focuses on the basic knowledge of signal processing in Linux system programming, including the concept of signals, signal mechanisms, signal-related events and states, signal sets, signal numbers, 4 elements of signals, and conventional signals. The content of the signal part must be mastered.
insert image description here

1. The concept of signal

Signals can be seen everywhere in our lives, such as: throwing cups in ancient wars; signal flares in modern warfare; signal guns used in sports competitions...

They all have in common:

1. Simple
2. Cannot carry a large amount of information
3. Only send when a certain condition is met.

Signal is the carrier of information. Under the Linux/UNIX environment, the ancient and classic communication method is still the main means of communication. Early versions of Unix provided a signaling mechanism, but it was unreliable and signals could be lost. Both Berkeley and AT&T made changes to the signaling model, adding reliable signaling mechanisms. but not compatible with each other. POSIX.1 standardized reliable signal routines.

2. Signal Mechanism

A sends a signal to B, and B executes its own code before receiving the signal. After receiving the signal, no matter where the program is executed, it must stop running to process the signal, and continue to execute after processing . Similar to hardware interrupts - asynchronous mode. But the signal is an interrupt implemented at the software level, and it was often called " soft interrupt " in the early days.

The characteristics of the signal: Since the signal is realized by software, the means of its realization causes the signal to have a strong delay. But for users, this delay time is very short and hardly noticeable. All signals received by each process are sent by the kernel and processed by the kernel.

3. Events and states related to signals

Generate a signal:

  1. Generated by keystroke, such as: Ctrl+c, Ctrl+z, Ctrl+\
  2. System calls are generated, such as: kill, raise, abort
  3. Software conditions are generated, such as: timer alarm
  4. Hardware exceptions occur, such as: illegal access to memory (segmentation fault), division by 0 (except for floating point numbers), memory alignment error (bus error)
  5. Command generation, such as: kill command
    delivery: delivery and reach the process.
    Pending: The state between generation and delivery. This state is mainly caused by blocking (shielding).

How the signal is handled:

  1. execute default action
  2. ignore (discard)
  3. Capture (call user processing function)

4. Signal collection

The process control block PCB of the Linux kernel is a structure, task_struct, in addition to including process id, status, working directory, user id, group id, file descriptor table, it also contains signal-related information, mainly referring to blocked signal sets and unblocked decision signal set.

insert image description here

Blocking signal set (signal mask word)

Add some signals to the set and set shields for them. When the x signal is shielded and the signal is received again, the processing of the signal will be postponed (after unmasking)

pending signal set

  1. When the signal is generated, the bit describing the signal in the pending signal set is immediately flipped to 1, indicating that the signal is in a pending state. The corresponding bit flips back to 0 when the signal is processed. This moment is often very short.
  2. After the signal is generated, it cannot arrive due to some reasons (mainly blocking). A collection of such signals is called a pending signal set. The signal remains pending until the mask is released.

5. Signal number

Use kill -lview:

Please add a picture description
No signal with number 0 exists. Among them, No. 1-31 signals are called regular signals (also called normal signals or standard signals), No. 34-64 are called real-time signals, and the driver programming is related to hardware. There is not much difference in name. And the top 32 names vary.

6. Four elements of a signal

1. Number 2. Name 3. Event 4. Default action

Can man 7 signalbe obtained by viewing the help documentation

   	   Signal      Standard   Action   Comment
       ────────────────────────────────────────────────────────────────────────
       SIGABRT      P1990      Core    Abort signal from abort(3)
       SIGALRM      P1990      Term    Timer signal from alarm(2)
       SIGBUS       P2001      Core    Bus error (bad memory access)
       SIGCHLD      P1990      Ign     Child stopped or terminated
       SIGCLD         -        Ign     A synonym for SIGCHLD
       SIGCONT      P1990      Cont    Continue if stopped
       SIGEMT         -        Term    Emulator trap
       SIGFPE       P1990      Core    Floating-point exception
       SIGHUP       P1990      Term    Hangup detected on controlling terminal
                                       or death of controlling process
       SIGILL       P1990      Core    Illegal Instruction
       SIGINFO        -                A synonym for SIGPWR
       SIGINT       P1990      Term    Interrupt from keyboard
       SIGIO          -        Term    I/O now possible (4.2BSD)
       SIGIOT         -        Core    IOT trap. A synonym for SIGABRT
       SIGKILL      P1990      Term    Kill signal
       SIGLOST        -        Term    File lock lost (unused)
       SIGPIPE      P1990      Term    Broken pipe: write to pipe with no
                                       readers; see pipe(7)
       SIGPOLL      P2001      Term    Pollable event (Sys V);
                                       synonym for SIGIO
       SIGPROF      P2001      Term    Profiling timer expired
       SIGPWR         -        Term    Power failure (System V)
       SIGQUIT      P1990      Core    Quit from keyboard
       SIGSEGV      P1990      Core    Invalid memory reference
       SIGSTKFLT      -        Term    Stack fault on coprocessor (unused)
       SIGSTOP      P1990      Stop    Stop process
       SIGTSTP      P1990      Stop    Stop typed at terminal
       SIGSYS       P2001      Core    Bad system call (SVr4);
                                       see also seccomp(2)
       SIGTERM      P1990      Term    Termination signal
       SIGTRAP      P2001      Core    Trace/breakpoint trap
       SIGTTIN      P1990      Stop    Terminal input for background process
       SIGTTOU      P1990      Stop    Terminal output for background process
	   SIGUNUSED      -        Core    Synonymous with SIGSYS
       SIGURG       P2001      Ign     Urgent condition on socket (4.2BSD)
       SIGUSR1      P1990      Term    User-defined signal 1
       SIGUSR2      P1990      Term    User-defined signal 2
       SIGVTALRM    P2001      Term    Virtual alarm clock (4.2BSD)
       SIGXCPU      P2001      Core    CPU time limit exceeded (4.2BSD);
                                       see setrlimit(2)
       SIGXFSZ      P2001      Core    File size limit exceeded (4.2BSD);
                                       see setrlimit(2)
       SIGWINCH       -        Ign     Window resize signal (4.3BSD, Sun)

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

Among standard signals, some signals have three "Values". The first value is usually valid for alpha and sparc architectures, the middle value is for x86, arm and other architectures, and the last one is applied to mips architecture. A '-' indicates that the signal has not been defined on the corresponding architecture.

Different operating systems define different system signals. So some signals that appear on Unix systems also appear on Linux, and some signals that appear on FreeBSD or Mac OS do not appear under Linux. Here we only study signals in Linux systems.

Default action:

Term: Terminate the process
Ign: Ignore the signal (the default is to ignore the signal immediately)
Core: Terminate the process and generate the Core file. (Check the cause of death of the process, used for gdb debugging)
Stop: stop (pause) the process
Cont: continue to run the process

Note that you can see from the man 7 signal help documentation:

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

9) SIGKILLThe and signals are specially emphasized here 19) SIGSTOP, and ignore and capture are not allowed, and only default actions can be performed. It can't even be set to block.

In addition, it needs to be clear that the signal will be delivered (but not necessarily delivered) only when the event corresponding to each signal occurs, and the signal should not be sent indiscriminately! !

7. List of common signals

  1. SIGHUP: When the user exits the shell, all processes started by the shell will receive this signal, and the default action is to terminate the process
  2. SIGINT: When the user presses the <Ctrl+C> key combination, the user terminal sends this signal to the running program started by the terminal. The default action
    is to terminate the process.
  3. SIGQUIT: This signal is generated when the user presses the <ctrl+> key combination, and the user terminal sends some signals to the running programs started by the terminal
    . The default action is to terminate the process.
  4. SIGILL: The CPU detected that a process executed an illegal instruction. The default action is to terminate the process and generate a core file
  5. SIGTRAP: This signal is generated by a breakpoint instruction or other trap instructions. The default action is to terminate the mileage and generate a core file.
  6. SIGABRT: This signal is generated when the abort function is called. The default action is to terminate the process and generate a core file.
  7. SIGBUS: Illegal access to memory addresses, including memory alignment errors, the default action is to terminate the process and generate a core file.
  8. SIGFPE: Emitted when a fatal arithmetic error occurs. Not only floating-point arithmetic errors are included, but all arithmetic errors such as overflow and division by zero.
    The default action is to terminate the process and generate a core file.
  9. SIGKILL: Unconditionally terminate the process. This signal cannot be ignored, processed and blocked. The default action is to terminate the process. It provides
    a way for system administrators to kill any process.
  10. SIGUSE1: User-defined signal. That is, the programmer can define and use the signal in the program. The default action is to terminate the process.
  11. SIGSEGV: Indicates that the process made an invalid memory access. The default action is to terminate the process and generate a core file.
  12. SIGUSR2: Another user-defined signal, the programmer can define and use this signal in the program. The default action is to terminate the process.
  13. SIGPIPE: Broken pipe writes data to a pipe with no read end. The default action is to terminate the process.
  14. SIGALRM: The timer is timed out, and the timeout time is set by the system call alarm. The default action is to terminate the process.
  15. SIGTERM: Program end signal, unlike SIGKILL, this signal can be blocked and terminated. Usually used to indicate that the program exits normally.
    This signal is generated by default when the shell command Kill is executed. The default action is to terminate the process.
  16. SIGSTKFLT: A signal that appeared in earlier versions of Linux and is still backwards compatible. The default action is to terminate the process.
  17. SIGCHLD: When the state of the child process changes, the parent process will receive this signal. The default action is to ignore this signal.
  18. SIGCONT: Keeps the process running if it is stopped. The default action is continue/ignore.
  19. SIGSTOP: Stop the execution of the process. Signals cannot be ignored, handled and blocked. The default action is to suspend the process.
  20. SIGTSTP: Stop the running of the terminal interactive process. This signal is emitted when the <ctrl+z> key combination is pressed. The default action is to suspend the process.
  21. SIGTTIN: The background process reads the terminal console. The default action is to suspend the process.
  22. SIGTTOU: This signal is similar to SIGTTIN and occurs when the background process wants to output data to the terminal. The default action is to suspend the process.
  23. SIGURG: When there is urgent data on the socket, send some signals to the currently running process to report the arrival of urgent data. If the network out-of-band
    data arrives, the default action is to ignore the signal.
  24. SIGXCPU: The process execution time exceeds the CPU time allocated to the process, the system generates this signal and sends it to the process. The default action is to
    terminate the process.
  25. SIGXFSZ: Exceeded the maximum length setting for the file. The default action is to terminate the process.
  26. SIGVTALRM: This signal is generated when the virtual clock times out. Similar to SIGALRM, but this signal only counts the CPU usage time of the process
    . The default action is to terminate the process.
  27. SGIPROF: Similar to SIGVTALRM, it does not include the CPU time occupied by the process and the execution system call time. The default action is to terminate the process.
  28. SIGWINCH: Emitted when the window changes size. The default action is to ignore the signal.
  29. SIGIO: This signal indicates to the process that an asynchronous IO event was issued. The default action is ignore.
  30. SIGPWR: Power off. The default action is to terminate the process.
  31. SIGSYS: Invalid system call. The default action is to terminate the process and generate a core file.
  32. SIGRTMIN~ (64) SIGRTMAX: LINUX real-time signals, they have no fixed meaning (can be defined by the user). The default action for all real-time signals is to terminate the process.

Guess you like

Origin blog.csdn.net/Goforyouqp/article/details/132335651