Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the basic knowledge of Linux signal mechanism

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "introduction to the basic knowledge of Linux signal mechanism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

As mentioned in the article Linux process fundamentals, Linux executes programs on a process-by-process basis. 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 (which belongs to the process's memory space), and each room is not allowed to enter outside the process. In this way, each process only focuses on what it is doing, regardless of other processes, and does not allow other processes to see inside their own room. This is a protection mechanism for each process. Imagine hundreds of processes always interfering with each other, how chaotic it would be, or hundreds of processes peeping 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 finds that a process is smashing a wall (hardware error) and needs to make the process realize that going on like this will destroy the entire building. For example, we want multiple processes to work together. In this way, we need a certain way of communication. A signal is a way to convey information to a process. We can think of the signal as the manager of the building stuffing a note into the mailbox of the room. Then the process takes out a small note and takes certain actions according to the contents of the note, such as a broken light and reminding the process to use a flashlight. (of course, you can completely ignore this note, but in an emergency like a fire, ignoring the signal is not a good choice.) Compared with other inter-process communication methods (interprocess communication, such as pipe, shared memory), the information that the signal can convey is relatively rough, just an integer. However, it is precisely because of the small amount of information transmitted, the signal is also easy to manage and use. As a result, signals are often used for system administration-related tasks, such as notifying process termination, abort, or resumption, and so on.

Signals are managed by the kernel (kernel). Signals can be generated in a variety of ways, such as hardware errors (such as division with denominator 0 or segmentation fault) that the kernel needs to notify a process, or it can be generated by other processes and sent to the kernel and then passed on by the kernel to the target process. In the kernel, there is a table for each process to store relevant information (the mailbox of the room). When the kernel needs to pass a signal to a process, the signal is written (stuffed with a note) in the corresponding table of the process, and the signal is generated (generate). When the process performs a system call and exits the kernel after the system call is completed, it will check the information in the mailbox by the way. If there is a signal, the process performs an operation on the signal (signal action, also known as signal processing signal disposition), which is called deliver signal. The time from the generation of the signal to the transmission of the signal, the signal is in a pending state (the note has not yet been viewed). We can also design programs to block certain signals generated by the process, that is, to keep them waiting until the process ununblock or ignore the signal.

Common signal

Each integer transmitted by the signal is given a special meaning, and there is a signal name corresponding to the integer. The common signals are SIGINT, SIGQUIT, SIGCONT, SIGTSTP, SIGALRM and so on. These are the names of the signals. You can pass

The code is as follows:

$man 7 signal

To check for more signals.

Among the above signals

SIGINT when the keyboard presses CTRL+C to send a signal from the shell, the signal is passed to the process running in the foreground of the shell, and the default action for the signal is to INTERRUPT the process.

SIGQUIT when the keyboard presses CTRL+\ to send a signal from the shell, the signal is passed to the process running in the foreground in the shell, and the default action for the signal is to QUIT the process.

SIGTSTP when the keyboard presses CTRL+Z to send a signal from the shell, the signal is passed to the process running in the foreground of the shell, and the default action for the signal is to STOP the process.

SIGCONT is used to notify the paused process to continue.

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

Using signals in shell

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

The code is as follows:

$ping localhost

At this point we can pass the SIGTSTP to the process through 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), which is 27397 on my machine

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

The code is as follows:

$kill-SIGCONT 27397

To pass the SIGCONT signal to the ping process.

Signal processing (signal disposition)

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

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

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

3) Custom operation. Also known as catch signal. Performs a preset operation corresponding to the signal in the process.

What kind of action the process will take depends on the programming of the process. Especially in the case of getting a signal, the program tends to set some long and complex operations (usually put them into a function).

Signals are often used for system management, so their content is quite complex. In-depth understanding of signals requires some programming knowledge of Linux environment.

This is the end of the introduction to the basics of Linux signaling mechanism. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report