Linux signal signal processing mechanism

  
 

Signal is a very important part of Linux programming. This article will introduce the basic concepts of the signal mechanism, the general implementation of the Linux signal mechanism, how to use the signal, and several system calls for the signal.

The signal mechanism is a method of transferring messages between processes. The signal is called soft interrupt signal, and some people call it soft interrupt. As can be seen from its naming, its essence and use is very similar to interruption. Therefore, the signal can be said to be part of the process control.

First, the basic concept of the signal

This section first introduces some basic concepts of the signal, and then gives some basic signal types and events corresponding to the signal. The basic concepts are especially important for understanding and using signals. Let's take a look at what is the signal.

1. Basic Concepts

A soft interrupt signal (signal, also referred to as a signal) is used to notify the process that an asynchronous event has occurred. Processes can send soft interrupt signals to each other through the system call kill. The kernel can also signal the process because of an internal event, notifying the process that an event has occurred. Note that the signal is only used to notify a process of what happened, and does not pass any data to the process.

The process of receiving signals has different processing methods for various signals. Processing methods can be divided into three categories: The first is a handler similar to interrupts. For signals that need to be processed, the process can specify a handler that is processed by the function. The second method is to ignore a signal and do nothing with it, as if it had not happened. A third method is to preserve the default value of the system for the processing of this signal. This default operation, the default operation for most signals, is to terminate the process. The process calls the signal through the system to specify the processing behavior of the process on a certain signal.

There is a soft interrupt signal field in the table of the process table. Each bit in the field corresponds to a signal. When a signal is sent to the process, the corresponding bit is set. It can be seen that the process can keep different signals at the same time, but for the same signal, the process does not know how many times before processing.

2. Types of Signals

There are many reasons for signaling. Here, you can easily classify the signals according to the reason for sending signals:

(1) and process termination Related signals. This type of signal is issued when the process exits or when the child process terminates. (2) Signals related to process exception events. If the process is out of bounds, or attempt to write a read-only memory area (such as the program body area), or execute a privileged instruction and various other hardware errors. (3) A signal related to an unrecoverable condition encountered during a system call. When the system call exec is executed, the original resources have been released, and the current system resources are exhausted. (4) A signal related to a non-predictive error condition encountered while executing a system call. For example, execute a system call that does not exist. (5) A signal from a process in user mode. For example, the process calls the system call kill to send signals to other processes. (6) Signals related to terminal interaction. For example, if the user closes a terminal or presses the break button. (7) Track the signal executed by the process.

The list of signals supported by Linux is as follows. Many signals are related to the architecture of the machine. The first listed are the signals listed in POSIX.1:

The reason why the signal value processing action signals ----------- -------------------------------------------------- --------- SIGHUP 1 A terminal suspends or the control process terminates SIGINT 2 A keyboard interrupt (if the break key is pressed) SIGQUIT 3 C keyboard exit key is pressed SIGILL 4 C illegal command SIGABRT 6 C Exit instruction issued by abort(3) SIGFPE 8 C Floating-point exception SIGKILL 9 AEF Kill signal SIGSEGV 11 C Invalid memory reference SIGPIPE 13 A Pipe break: Write a pipe SIGALRM 14 A without read port issued by alarm(2) Signal SIGTERM 15 A Termination signal SIGUSR1 30,10,16 A User-defined signal 1 SIGUSR2 31,12,17 A User-defined signal 2 SIGCHLD 20,17,18 B Sub-process end signal SIGCONT 19,18,25 Process continues ( Process that was stopped) SIGSTOP 17,19,23 DEF Terminate process SIGTSTP 18,20,24 D Press the stop button SIGTTIN 21,21,26 on the control terminal (tty). The background process attempts to read SIGTTOU from the control terminal 22,22 , 27 D backstage Attempting to write from the control terminal

The following signals are not listed in POSIX.1, and the reason for the

signal value processing action is listed in SUSv2-------- -------------------------------------------------- ---------- SIGBUS 10,7,10 C bus error (wrong memory access) SIGPOLL A Sys V defined Pollable event, synonymous with SIGIO SIGPROF 27,27,29 A Profiling timer to SIGSYS 12,-,12 C Invalid system call (SVID) SIGTRAP 5 C Trace/breakpoint capture SIGURG 16,23,21 B Socket emergency condition (4.2 BSD) SIGVTALRM 26,26,28 A Actual time alarm clock signal (4.2 BSD) SIGXCPU 24, 24, 30 C exceeds the set CPU time limit (4.2 BSD) SIGXFSZ 25, 25, 31 C exceeds the set file size limit (4.2 BSD)

(for SIGSYS, SIGXCPU, SIGXFSZ, and SIGBUS under certain machine architectures, Linux defaults to A (terminate) and SUSv2 to C (terminate and dump core).

The following are some other signals

The reason why the signal value processing action signals ------------------------ ---------------------------------------------- SIGIOT 6 C IO Capture instruction, synonymous with SIGABRT SIGEMT 7,-,7 SIGSTKFLT -,16,- A Coprocessor stack error SIGIO 23,29,22 A An I/O operation is now possible (4.2 BSD) SIGCLD -,-, 18 A Synonymous with SIGCHLD SIGPWR 29,30,19 A Power failure (System V) SIGINFO 29,-,- A Synonymous with SIGPWR SIGLOST -,-,- A File lock lost SIGWINCH 28,28,20 B Window size change (4.3 BSD, Sun) SIGUNUSED -, 31, - A Unused signal (will be SIGSYS)

(here, - indicates that the signal is not implemented; there are three values ​​giving the meaning, first Values ​​are usually valid on Alpha and Sparc, with values ​​in the middle for i386 and ppc and sh, and the last value for mips. Signal 29 is SIGINFO /SIGPWR on Alpha and SIGLOST on Sparc.)

Processing action The meaning of the letters in an item is as follows: A The default action is to terminate the process B. The default action is to ignore this signal. C The default action is to terminate the process and execute the kernel. Dump core D The default action is to stop the process. The E signal cannot be captured. The F signal cannot be ignored.

The signals described above are supported by common systems. The names, effects, and processing actions of the various signals are described in tabular form. The meaning of various default processing actions is: the termination of the program refers to the process exit; ignore the signal is to discard the signal, do not deal with; stop the program means that the program hangs, can continue to go after entering the stop condition, usually in During the debugging process (for example, ptrace system call); kernel image dump refers to dumping the image of the process data in memory and part of the content stored in the kernel structure to the file system in a certain format, and the process exits execution. The benefit is that it provides the programmer with the convenience of getting the data values ​​of the process at the time of execution, allowing them to determine the reason for the dump and debugging their program.

Copyright © Windows knowledge All Rights Reserved