A brief introduction to the Linux signal mechanism

  

What is the Linux signal mechanism, the Linux signal mechanism is not so simple to explain, this article is just to explain the basics of the Linux signal mechanism, let you have some understanding of the Linux signal mechanism, one Get up and study.

Linux basic process has been mentioned in an article, Linux to process units to execute the program. We can think of the computer as a building, the kernel is the administrator of the building, and the process is the tenant of the building. Each process has a separate room (the memory space belonging to the process), and each room is not allowed to enter outside the process. In this way, each process focuses on what it does, regardless of other processes, and does not let other processes see inside the room. This is a protection mechanism for each process. (Imagine how hundreds of processes always interfere with each other, how confusing it is, or hundreds of processes peek at each other … …)

However, in some cases, we need to break the closed room, In order to exchange information with the process. For example, the kernel found that there is a process in the wall (hardware error), and the process needs to be made aware that this will continue to ruin the entire building. For another example, we want to work together across multiple processes. In this way, we need a certain way of communication. A signal is a way of passing information to a process. We can think of the signal as the manager of the building stuffing a small note into the mailbox of the room. The process then takes out the small note and takes certain actions according to the content on the note. For example, the lamp is broken and the process is used to remind the process to use the flashlight. (Of course, you can completely ignore this note, but in an emergency such as a fire, ignoring the signal is not a good choice). Compared with other interprocess communication methods (such as pipe, shared memory), the information that the signal can pass is rough, just an integer. But because of the small amount of information transmitted, the signal is also easy to manage and use. Signals are therefore often used for system management related tasks such as notification process termination, suspension or recovery, and so on.

Give me a signal

The signal is managed by the kernel. Signals can be generated in a variety of ways. They can be generated by the kernel itself, such as hardware errors (such as divisions with a denominator of 0 or a segmentation fault). The kernel needs to notify a process; it can also be generated by other processes. , sent to the kernel, and then passed to the target process by the kernel. There is a table in the kernel for each process to store related information (the mailbox of the room). When the kernel needs to pass a signal to a process, it writes a signal (plugged into the strip) at the appropriate position in the corresponding table of the process, and the signal is generated. When the process executes the system call, when the kernel is exited after the system call is completed, the information in the mailbox will be checked by the way. If there is a signal, the process will perform the signal action (also called signal disposition), which is called the delivery signal. From the time the signal is generated to the time the signal is transmitted, the signal is in a pending state (the strip has not been viewed yet). We can also design programs that cause the generated process to block certain signals, that is, keep these signals in a wait state until the process unblocks or ignores the signal.

Common Signals

Each integer passed by a signal is given a special meaning, and has a signal name that corresponds to an integer. Common signals are SIGINT, SIGQUIT, SIGCONT, SIGTSTP, SIGALRM, etc. These are the names of the signals. You can use the

code as follows:

$man 7 signal

to see more signals.

In the above signals,

SIGINT When the keyboard presses CTRL+C to send a signal from the shell, the signal is passed to the foreground running process in the shell. The default operation corresponding to the signal is Interrupt (INTERRUPT) The process.

SIGQUIT When the keyboard presses CTRL+\\ to signal from the shell, the signal is passed to the foreground running process in the shell. The default action for the corresponding signal is to exit (QUIT) the process.

SIGTSTP When the keyboard presses CTRL+Z to signal from the shell, the signal is passed to the foreground running process in the shell. The default action for the corresponding signal is to STOP the process.

SIGCONT is used to notify the suspended process to continue.

SIGALRM acts as a timer, usually after the program has generated the signal after a certain amount of time.

Using signals in the shell

Let's actually apply the signal. We run ping in the shell:

The code is as follows:

$ping localhost

At this point we can pass SIGTSTP to the process via CTRL+Z. The shell shows:

The code is as follows:

[1]+ Stopped ping localhost

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

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

The code is as follows:

$kill - SIGCONT 27397

to pass the SIGCONT signal to the ping process.

Signal Disposition

In the above example, all signals take the default action of the corresponding signal. But this is not absolute. When the process decides to execute the signal, there are several possibilities:

1) Ignore (ignore) signal, the signal is cleared, the process itself does not take any special operation

2) Default ( Default) operation. Each signal corresponds to a certain default operation. For example, SIGCONT above is used to continue the process.

3) Customize the operation. Also called a catch signal. The operation corresponding to the signal preset in the execution process.

What kind of operation the process will take, according to the program design of the process. Especially in the case of signal acquisition, programs tend to set up some long and complicated operations (usually put these operations into a function).

Signals are often used for system management, so its content is quite complex. In-depth understanding of the signal requires a certain knowledge of the Linux environment programming.

Summary

Signaling mechanism; generate, deliver, pending, blocking

signal action/dispositon; ignore, default action, catch signal

$kill< Br>

The above is the introduction of the Linux signal mechanism. The Linux signal mechanism is far more complicated than imagined. It is difficult to talk about a few pages. If you are interested in the Linux signal mechanism, you can find related books. To understanding.

Copyright © Windows knowledge All Rights Reserved