Signal processing mechanism in Linux

Source: Internet
Author: User
Article title: Linux signal processing mechanism. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

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

The signal mechanism is a method for transmitting messages between processes. all signals are called soft interrupt signals, or soft interruptions. From its name, we can see that its essence and use are very interrupted. Therefore, signals are part of process control.

I. basic concepts of signals

This section first introduces some basic concepts of signals, and then provides some basic signal types and events corresponding to signals. Basic concepts are important for understanding and using signals. Next let's take a look at what a signal is.

1. Basic concepts

The soft interrupt signal (signal) is used to notify the process of an asynchronous event. Processes can call kill to send soft interrupt signals to each other. The kernel can also send a signal to the process due to internal events to notify the process of an event. Note that the signal is only used to notify a process of events and does not transmit any data to the process.

Processes that receive signals have different processing methods for various signals. There are three types of processing methods: The first is an interrupt-like processing program. for signals to be processed, the process can specify a processing function, which is used for processing. The second method is to ignore a signal and do not process it any more, just as it has never happened. The third method is to retain the default value of the system for processing the signal. the default operation for most mail numbers is to terminate the process. A process calls signal to specify the process's processing behavior for a signal.

There is a soft interrupt signal field in the table of the progress table. each digit in the field corresponds to a signal. when a signal is sent to the process, it corresponds to a position. From this we can see that the process can retain different signals at the same time, but for the same signal, the process does not know how many signals have come before processing.

2. signal type

There are many reasons for sending signals. here we simply classify the reasons for sending signals to learn about various signals:

(1) signals related to process termination. This type of signal is sent when the process exits or the sub-process is terminated.
(2) signals related to process exception events. For example, if a process crosses the border, attempts to write a read-only memory area (such as the program Body area), or executes a privileged command and other hardware errors.
(3) signals related to unrecoverable conditions during system calls. For example, when the system calls exec, the original resources have been released, and the system resources have been exhausted.
(4) signals related to non-predicted error conditions during system calls. For example, execute a non-existing system call.
(5) signals sent by processes in user mode. For example, a process call system calls kill to send signals to other processes.
(6) signals related to terminal interaction. For example, you can close a terminal or press the break key.
(7) signal of process execution.

The signal list supported by Linux is as follows. Many signals are related to the machine's architecture. First, the signals listed in POSIX.1 are listed as follows:

Signal value processing
----------------------------------------------------------------------
SIGHUP 1 A terminal suspension or control process termination
SIGINT 2 A keyboard interruption (for example, the break key is pressed)
The exit key of the SIGQUIT 3 C keyboard is pressed
Invalid SIGILL 4 C command
SIGABRT 6 C exit command 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 rupture: write an pipe without A read port
SIGALRM 14 A signal sent by alarm (2)
SIGTERM 15 A termination signal
SIGUSR1 30,10, 16 A user-defined Signal 1
SIGUSR2, 17 A user-defined signal 2
SIGCHLD, 18 B sub-process end signal
SIGCONT 19,18, 25 process continued (previously stopped process)
SIGSTOP 17,19, 23 DEF terminate the process
SIGTSTP 18, 20, 24 D control terminal (tty) press the stop key
SIGTTIN, 26 D background process attempted to read from control terminal
SIGTTOU, 27 D background process attempted to write from control terminal

The following signals are not listed in POSIX.1, but in SUSv2

Signal value processing
--------------------------------------------------------------------
SIGBUS, 7, and 10 C bus errors (wrong memory access)
The Pollable event defined by sigpoll a Sys V, which is synonymous with SIGIO
SIGPROF 27,27, 29 A Profiling timer
SIGSYS 12,-, 12 C invalid system call (SVID)
SIGTRAP 5 C trace/breakpoint capture
SIGURG 4.2, 23, 21 B Socket emergency condition (BSD)
SIGVTALRM 26,26, 28 A real-time alarm clock signal (4.2 BSD)
SIGXCPU 24, 24, and 30 C exceeds the set CPU time limit (4.2 BSD)
SIGXFSZ 4.2, 25, 31 C exceeds the set file size limit (BSD)

(For SIGSYS, SIGXCPU, SIGXFSZ, and SIGBUS in some machine architectures, the default action for Linux is A (terminate), and for SUSv2 is C (terminate and dump core )).

Below are some other signals

Signal value processing
----------------------------------------------------------------------
SIGIOT 6 c io capture command, synonymous with SIGABRT
SIGEMT 7,-, 7
SIGSTKFLT-, 16,-A coprocessor stack error
SIGIO 4.2, 22 a I/O operation can now be performed (BSD)
SIGCLD-,-, 18 A is synonymous with SIGCHLD
SIGPWR 29,30, 19 A power supply fault (System V)
SIGINFO 29,-,-A is synonymous with SIGPWR
SIGLOST-,-,-A lost file lock
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. three values indicate that the first value is always valid on Alpha and iSCSI, and the intermediate value corresponds to i386, ppc, and sh, the last value corresponds to mips. Signal 29 is SIGINFO/SIGPWR on Alpha and SIGLOST on iSCSI .)

The letter meanings in the action item are as follows:
The default action of A is to terminate the process.
The default action of B is to ignore this signal.
The default action of C is to terminate the process and perform a kernel image dumping (dump core)
D. the default action is to stop the process.
E signal cannot be captured
F signal cannot be ignored

Signals described above are supported by common systems. The following table describes the names, functions, and processing actions of various signals by default. The meaning of various default processing actions is: terminating a program means that the process is exited; ignoring the signal means that the signal is discarded without processing; stopping a program means that the program is suspended, it can be re-executed after it enters the stopped state, usually in the debugging process (for example, ptrace system calling ); kernel image dumping refers to dumping the image of process data in the memory and part of the content stored by the process in the kernel structure to the file system in a certain format and exiting the execution, the advantage of doing so is to provide programmers with convenience, so that they can get the data value at the time of execution of the process, allow them to determine the cause of the dump, and debug their program.

Note that the signal SIGKILL and SIGSTOP cannot be captured or ignored. SIGIOT and SIGABRT are signals. We can see that the same signal may have different values in different systems. Therefore, we recommend that you use a name defined for the signal instead of using the signal value directly.

[1] [2] [3] [4] Next page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.