Linux system Programming signal (one) signal overview

Source: Internet
Author: User
Tags sigint signal

First, in order to understand the signal, start from our most familiar with the scene:

1. User input command to start a foreground process under the shell.

2. The user presses the CTRL-C, this keyboard input produces a hardware interrupt.

3. If the CPU is currently executing the code for this process, the user space code for the process is paused, and the CPU switches from user state to kernel state to process hardware interrupts.

4. The terminal driver interprets ctrl-c as a SIGINT signal, which is recorded in the PCB of the process (or it can be said that a SIGINT signal is sent to the process).

5. When the user space code to return from the kernel to the process continues to execute, first process the signal recorded in the PCB, and find that there is a SIGINT signal to be processed, and the default processing action of this signal is to terminate the process, so that the process is terminated directly and no longer returns its user space code execution.

Use the Kill-l command to view a list of system-defined signals:

Each signal has a number and a macro definition name that can be found in signal.h, such as the definition of #define SIGINT 2. More than 34 is the real time signal, these signals are generated under what conditions, the default processing action is what (term means terminate the current process, core means to terminate the current process and the core dump,ign to ignore the signal, stop means to stop the current process, Cont indicates that the previously stopped process was continued, and is described in signal (7).

0~31 unreliable signals, multiple signals will not be queued to keep only one, that is, the signal may be lost.

34~64 reliable (real-time signal), support the queuing signal will not be lost, can use Sigqueue to send signals, unlike 0~31 has a default definition.

Second, the conditions for generating signals are:

1, the user in the terminal press some keys, the terminal driver will send signals to the foreground process, such as CTRL-C produce SIGINT signal, ctrl-\ produce sigquit signal, ctrl-z generate SIGTSTP signal.

2, the hardware abnormal signal, these conditions by the hardware detected and notify the kernel, and then the kernel to the current process to send the appropriate signal. For example, the current process executes an instruction divided by 0, and the CPU's unit of operations generates an exception, which the kernel interprets as a SIGFPE signal sent to the process.

3, again such as the current process access to the illegal memory address, MMU will produce an exception, the kernel will interpret this exception as a SIGSEGV signal sent to the process.

4, a process call Kill (2) function can send signals to another process.

5, can use Kill (1) command to send a signal to a process, kill (1) command is also called Kill (2) function implementation, if not explicitly specify the signal to send sigterm signal, the signal of the default processing action is to terminate the process.

6, raise: To send their own signals. Raise (SIG) is equivalent to Kill (Getpid (), SIG);

7, KILLPG: Send signal to process group. KILLPG (Pgrp, SIG) is equivalent to Kill (-pgrp, SIG);

8, Sigqueue: Send signal to the process, support queuing, can be accompanied by information.

9. When the kernel detects that a software condition is occurring, it can also notify the process by signal, such as the alarm clock timeout generates SIGALRM signal, the sigpipe signal is generated when writing data to the closed pipe of the reading end.

Third, the user program can call signal (2)/sigaction (2) function to tell the kernel how to handle some kind of signal (if not registered by default processing), the optional processing action has three kinds:

1. Ignore this signal. There are two signals that cannot be ignored: Sigkill and Sigstop.

2. Perform the default processing action for the signal.

3. Provides a signal processing function that requires the kernel to switch to the user state to perform this processing function when processing the signal, which is called a catch (catch) signal.

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.