Linux: Signal (UP)

Source: Internet
Author: User
Tags sigint signal

From the previous articles in my blog, it has been emphasized that the operating system is a multi-process collaborative operation to achieve the logical purpose of the entire operating system, to achieve an artificially manipulated system:

The previous blog post has been describing process control, communication between processes, and thread control and manipulation, but it has not been described how multiple processes communicate with each other to inform the entire system. For many different processes, there is an exception or non-process communication between processes how to implement, so the Linux system implemented a concept called signal:

Signal is the equivalent of our real life notification information, to inform ourselves or others in a certain state, is not very humane, the concept of signal appears, resolved the process and process notification:


so about the signal, we can first look at what the signal is in Linux:650) this.width=650; "Src=" http://s5.51cto.com/wyfs02/M01/7F/C7/ Wkiol1csv9wr7d7naadwwa-3jew126.png "title=" qq picture 20160506163355.png "alt=" Wkiol1csv9wr7d7naadwwa-3jew126.png "/>


First we observe that signal number 1-31 is a common signal, and we are mostly familiar with the signals discussed. 32-33 does not occupy the signal table, represents a differentiated version, 34-64 is a real-time signal. We do not discuss, 1-31 signal we can find his macro definition in signal.h. The number represents the signal number, and the back represents the signal meaning.


Then the specific meaning of the signal is not too much to explain, let us look at the basic process of generating the signal:

1. When the user presses certain keys at the terminal, the terminal driver sends a signal to the foreground process, for example CTRL-C generates SIGINT signal, Ctrl-\ generates SIGQUIT signal, CTRL-Z generates SIGTSTP signal.

2. Hardware anomalies generate signals that the hardware detects and notifies the kernel, and the kernel sends the appropriate signal to the current process. For example, the current process executes the instruction divided by 0, the CPU unit of operation will produce an exception, the kernel interprets this exception as SIGFPE signal sent to the process, such as the current process to access the illegal memory address, the MMU will produce an exception, the kernel interprets this exception as a SIGSEGV signal sent to the process.

3. A process call Kill (2) function can send a signal to another process. The Kill (1) command can be used to send a signal to a process, and the Kill (1) command is also called The Kill (2) function, if the signal is not explicitly specified to send a sigterm signal, the signal's default processing action is to terminate the process. When the kernel detects that a certain software condition occurs, it can also notify the process by signaling, for example, the alarm time-out generates a SIGALRM signal and generates a sigpipe signal when writing data to a pipe that has been closed by the reader. If you do not want to process the signal by default action, the user program can call the Sigaction (2) function to tell the kernel how to handle some kind of signal.

(The Sigaction function is detailed later), there are three types of optional processing actions:

1. Ignore this signal.

2. Perform the default processing action for this signal.

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


How the signal is generated:

    1. Signal Generation via terminal:

      Through the keyboard command to generate a signal, CTRL-C generate SIGINT signal, ctrl-\ generate sigquit signal, ctrl-z generate SIGTSTP signal (can make foreground process work, go to Background job).

    2. Call the system function to send a signal to the process:

In the shell command line, enter:

Kill-signnumber pid;

The process can be killed by the command line, which can be processed immediately, either in the foreground process or in the background process.

Where the KILL command is implemented by invoking the Kill function;

#include <signal.h>

int Kill (pid_t pid, int signo);

int raise (int signo);

Both functions return 0 successfully, and the error returns-1.

The abort function causes the current process to receive an SIGABRT signal and terminates abnormally.

#include <stdlib.h>

void abort (void);

Just like the Exit function, the Abort function is always successful, so there is no return value.


3. Signals generated by the software condition:

One of the sigpipe is a soft-condition signal, which has been mentioned in the Process pipeline blog post. To pave the way, let's talk about the alarm function and the SIGALRM signal,

Call the alarm function to set an alarm that tells the kernel to send a SIGALRM signal to the current process after seconds seconds, and the default processing action is to terminate the current process. The return value of this function is 0 or the number of seconds remaining for the previously set alarm time. For example, someone has to take a nap, set the alarm to ring after 30 minutes, wake up after 20 minutes, and want to sleep a little longer, then reset the alarm to 15 minutes later, "The time left before the alarm clock time" is 10 minutes. If the seconds value is 0, which means that the previously set alarm is canceled, the return value of the function is still the number of seconds remaining for the previously set alarm time.

Code:

#include <stdio.h> #include <unistd.h>int main () {int ret = 9;ret = Alarm (Ten);p rintf ("Alarm 1:%d\n", ret); Sleep (3), ret = alarm (Ten);p rintf ("Alarm 2:%d\n", ret); return 0;}

Operation Result:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/CC/wKioL1ctqErgQrDWAAATJicmLLc308.png "title=" QQ picture 20160507162921.png "alt=" Wkiol1ctqergqrdwaaatjicmllc308.png "/>



Blocking signal:

A. The signal is represented in the kernel:

There are 3 types of signals:

1. Recursion: The actual execution signal processing action;

2. Pending: The status between the delivery to the recursion is called the signal is pending, to be able to cooperate with the blocking can produce the pending state.

3. Blocking: The process chooses to block the waiting signal and does not allow it to execute.

The signal in the kernel indicates:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/CF/wKiom1ctqMDAQdiEAAA6qNpLF0g861.png "title=" QQ picture 20160507163500.png "alt=" Wkiom1ctqmdaqdieaaa6qnplf0g861.png "/>

The signal-related table values are stored in the PCB, using BITS to indicate that each signal has two flag bits representing block and pending (pending), and then a function that represents the processing action of the signal.


The 1.SIGHUP signal is not blocked or generated, and executes the default processing action when it is passed.

2. SIGINT signal has been generated, but is being blocked, so temporarily can not reach. Although its processing action is ignored, this signal cannot be ignored until it is unblocked because the process still has the opportunity to change the processing action before unblocking.

3. The sigquit signal is not produced, once the resulting sigquit signal will be blocked, its processing action is a user-defined function Sighandler.

Not finished adding.

This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1771055

Linux: Signal (UP)

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.