Linux signal Basics

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

As mentioned in Linux Process basics, Linux runs programs in process units. We can regard computers as a building. The kernel is the building administrator and the process is the tenants of the building. Each process has an independent room (the memory space of the process), and every room is not allowed to access people outside the process. In this way, each process only focuses on what it does, regardless of other processes, and does not allow other processes to view the interior of the room. This is a protection mechanism for each process. (Imagine how chaotic it will be when hundreds of processes are always interfering with each other, or hundreds of processes are peeking at each other ......)

 

However, in some cases, we need to break the closed room to communicate with the process. For example, if the kernel finds that a process is hitting the wall (hardware error), it is necessary to make the process realize that this will continue to destroy the entire building. For example, we want to allow cooperation between multiple processes. In this way, we need certain communication methods. A signal is a method of transmitting information to a process. We can imagine that the administrator of the big building would put a small note in the mailbox of the room. Then the process will take some action based on the content of the paper, such as the light is broken, reminding the process to use the flashlight. (Of course, you can ignore this paper. However, in an emergency like fire, ignoring the signal is not a good choice ). Compared with other interprocess communication methods (for example, pipe and shared memory), the information that the signal can transmit is rough and only an integer. However, because the amount of information transmitted is small, the signal is easy to manage and use. Therefore, signals are often used for system management related tasks, such as notifying the process to terminate, stop, or recover.

 

Give me a signal

 

Signals are managed by the kernel. Signal generation methods are diverse. It can be generated by the kernel itself, such as hardware errors (for example, Division operations with a denominator of 0 or segmentation fault ), the kernel needs to notify a process. It can also be generated by other processes, sent to the kernel, and then transmitted to the target process by the kernel. In the kernel, there is a table for each process to store related information (room mailbox ). When the kernel needs to pass the signal to a process, it writes the signal (inserted into the paper) at the appropriate position in the table corresponding to the process, and generate the signal. When the process executes a system call and exits the kernel after the system call is completed, the information in the mailbox is checked by the way. If there is a signal, the process will execute the corresponding signal operation (signal action, also called signal processing signal disposition), this is called the execution (deliver) signal. From the time when the signal is generated to the time when the signal is transmitted, the signal is in the pending state (the note has not been viewed ). We can also design a program to keep some signals generated by the process blocks, that is, to keep these signals in the waiting state until the process unblocks or ignores the signal.

 

1. Common Signals

Each integer transmitted by the signal is given a special meaning, and a signal name pair should be an integer. Common signals include SIGINT, sigquit, sigcont, sigtstp, and sigalrm. These are the names of signals. You can use

$ MAN 7 signal

To view more signals.

 

Among the above signals,

SIGINT when the keyboard presses Ctrl + C to send a signal from the shell, the signal is sent to the process running on the shell foreground, the default operation on the signal is to interrupt (Interrupt) the process.

Sigquit: When you press Ctrl + \ on the keyboard to send a signal from the shell, the signal is sent to the process running on the shell foreground. The default signal operation is to exit the process.

When you press Ctrl + Z on the keyboard to send a signal from the shell, the signal is sent to the process running on the shell foreground. The default operation on the signal is to stop the process.

Sigcont is used to notify the paused process to continue.

Sigalrm acts as a timer, which is usually generated by a program after a certain period of time.

 

2. Use signals in Shell

Next we will apply the signal. Run Ping in shell:

$ Ping localhost

In this case, we can pass the sigtstp to the process through Ctrl + Z. Shell:

[1]+  Stopped                 ping localhost

We use $ PS to query the PID of the ping process (PID is the room number of the ping process), which is 27397 in my machine

We can use the $ kill command in shell to send a signal to a process:

$ Kill-sigcont 27397

To send the sigcont signal to the ping process.

 

3. Signal Processing (signal disposition)

In the above example, all signals take the default operation of the corresponding signal. But this is not absolute. When the process determines the execution signal, there are several possibilities:

1) The ignore signal is ignored and the signal is cleared. The process itself does not take any special operations.

2) default operation. Each signal pair should have certain default operations. For example, the above sigcont is used to continue the process.

3) custom operations. It is also called a catch signal. Execute the preset operation corresponding to this signal in the process.

What kind of operation the process will take depends on the program design of the process. In particular, when obtaining signals, the program often sets some long and complex operations (usually put these operations in a function ).

 

In fact, the signal is often used for system management, so its content is quite complex. A deep understanding of the signal requires a certain degree of knowledge about programming in the Linux environment. Here is a summary.

 

Summary:

Signal mechanism; generate, deliver, pending, blocking

Signal action/Dispositon; ignore, default action, catch Signal

$ Kill

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.