Home System Tutorial LINUX Linux CPU Interrupts: Asynchronous Events and Common Handling Mechanisms

Linux CPU Interrupts: Asynchronous Events and Common Handling Mechanisms

Feb 11, 2024 pm 03:06 PM
linux linux tutorial linux system Linux operating system linux command shell script NIC driver embeddedlinux Getting started with linux linux learning

Linux Interrupt is an asynchronous event that can occur at any time and can break the normal execution flow of the program. In order to handle these interrupts, the Linux kernel provides a general interrupt handling mechanism.

Linux CPU 中断:异步事件和通用处理机制

Interrupt is actually a signal called IRQ (interrupt request) sent by hardware or software.

Interrupts allow devices such as keyboards, serial cards, parallel ports, etc. to indicate that they require the CPU.

Once the CPU receives an interrupt request, the CPU will temporarily stop executing the running program and call a specific program called an interrupt handler or interrupt service routine (interrupt service routine).

The interrupt service routine or interrupt handler can be found in the interrupt vector table, which is located at a fixed address in memory. After the interrupt is processed by the CPU, execution of the previously interrupted program will resume.

In fact, when the machine starts, the system has already identified all devices and loaded the corresponding interrupt handlers into the interrupt table.

The following are two ways to request CPU attention:
\1. Based on interrupt
\2. Based on polling

All Linux operating systems are based on interrupt drivers.

When we press a key on the keyboard, the keyboard will tell the CPU that a key has been pressed. In this case, the voltage in the keyboard's IRQ line will change once, and this voltage change is a request from the device, which is equivalent to saying that the device has a request that needs to be processed.

/proc/interrupts file
On Linux machines, the file /proc/interrupts contains information about which interrupts are in use and how many times each processor has been interrupted.

\# cat /proc/interrupts 
     CPU0 CPU1 CPU2 CPU3 
 0: 3710374484   0  0  0 IO-APIC-edge timer 
 1:    20   0  0  0 IO-APIC-edge i8042 
 6:     5   0  0  0 IO-APIC-edge floppy 
 7:     0   0  0  0 IO-APIC-edge parport0 
 8:     0   0  0  0 IO-APIC-edge rtc 
 9:     0   0  0  0 IO-APIC-level acpi 
 12:    240   0  0  0 IO-APIC-edge i8042 
 14: 11200026   0  0  0 IO-APIC-edge ide0 
 51: 61281329   0  0  0 IO-APIC-level ioc0 
 59:     1   0  0  0 IO-APIC-level vmci 
 67: 19386473   0  0  0 IO-APIC-level eth0 
 75: 94595340   0  0  0 IO-APIC-level eth1 
NMI:     0   0  0  0
LOC: 3737150067 3737142382 3737145101 3737144204
ERR:     0
MIS:     0
Copy after login

The output of the above file is explained as follows:
● The first column indicates the IRQ number
● The second, third, and fourth columns indicate the number of times the corresponding CPU core was interrupted. In the above example, timer represents the interrupt name (which is the system clock). 3710374484 means that CPU0 was interrupted 3710374484 times. i8042 represents the keyboard controller that controls the keyboard and mouse.
● For interrupts like rtc (real time clock), the CPU will not be interrupted. Because RTC exists in electronic devices and is used to track time.
● NMI and LOC are drivers used by the system and cannot be accessed and configured by users.

The IRQ number determines the priority that needs to be processed by the CPU. The smaller the IRQ number means the higher the priority.
For example, if the CPU receives interrupts from both the keyboard and the system clock, the CPU will first service the system clock because its IRQ number is 0.
● IRQ0: System clock (cannot be changed)
● IRQ1: Keyboard controller (cannot be changed)
● IRQ3: Serial port controller of serial port 2 (if there is serial port 4, it also uses this interrupt)
● IRQ4: Serial port controller of serial port 1 (if there is serial port 3, it also uses this interrupt)
● IRQ5: Parallel port 2 and 3 or sound card
● IRQ6: Floppy disk controller
● IRQ7: Parallel port 1. It is used with printers or if there is no printer, can be used with any parallel port.

For the CPU on a joystick (or game controller), it does not wait for the device to send an interrupt. Because the joystick is mainly used for games, the movement of the joystick must be very fast, so it is ideal to use polling to detect whether the device needs the attention of the CPU. The disadvantage of using the polling method is that the CPU is in a busy waiting state because the CPU will check the device multiple times. But it should be noted that in Linux, this way of handling signals is also essential.

Hard interrupt
The scenarios discussed above are all examples of hard interrupts. Hard interrupts are mainly divided into two categories:
\1. Non-maskable interrupts (NMI): Just like the literal meaning of this interrupt type, this interrupt cannot be ignored or canceled by the CPU. NMI is sent on a separate interrupt line and is usually used for critical hardware errors, such as memory errors, fan failures, temperature sensor failures, etc.
\2. Maskable interrupts: These interrupts can be ignored or delayed by the CPU. This type of interrupt will be generated when the external pin of the cache controller is triggered, and the interrupt mask register will mask such interrupts. We can set a bit to 0 to disable the interrupt triggered on this pin.

Soft interrupt
These interrupts are generated when the CPU executes instructions (that is, when the process is running), because when executing instructions, the CPU (specifically, the arithmetic unit in the CPU) itself will generate an exception (this Exceptions at can also be understood as soft interrupts).

For example, dividing a number by 0 (of course this is impossible) will result in a divide-by-zero exception, causing the computer to cancel the calculation or display an error message.

The file /proc/stat contains some statistical information about the system kernel and some interrupt information.

\# cat /proc/stat 
cpu 17028082 5536753 5081493 1735530500 42592308 90006 479750 0
cpu0 5769176 1170683 1495750 403368354 39406374 90006 284864 0
cpu1 3714389 1451937 1186134 444082258 1084780 0 64876 0
cpu2 3791544 1471013 1211868 443988514 1056981 0 64764 0
cpu3 3752971 1443119 1187740 444091373 1044172 0 65244 0
intr 417756956 --- Output Truncated
Copy after login

In the intr line, the number of interrupts generated since the system was started is displayed. The first column represents the number of all serviced interrupts. Each subsequent column represents the total number of interrupts for a particular interrupt.

SMP_AFFINITY
SMP是指对称多处理器。smp_affinity文件主要用于某个特定IRQ要绑定到哪个CPU核心上。在/proc/irq/IRQ_NUMBER/目录下都有一个smp_affinity文件,这个文件中,所表示的CPU核心以十六进制来表示的。例如,网卡的中断号是:

grep eth0 /proc/interrupts
67: 23834931 0 0 0 IO-APIC-level eth0

cat /proc/irq/67/smp_affinity
00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001

上面的十六进制对应的十进制是1,也就是说所有的和网卡驱动相关的中断都是有CPU0来提供服务的。

我们可以通过手动改变smp_affinity文件中的值来将IRQ绑定到指定的CPU核心上,或者启用irqbalance服务来自动绑定IRQ到CPU核心上。

IRQ Balance
Irqbalance是一个linux的实用程序,它主要是用于分发中断请求到CPU核心上,有助于性能的提升。它的目的是寻求省电和性能优化之间的平衡。你可以使用yum进行安装:

 \# rpm -qa | grep irqbalance 
irqbalance-0.55-15.el5 
\# yum search irqbalance 
\# yum install irqbalance.x86_64
Copy after login

启动irqbalance服务后,中断在CPU上的分布如下:

 \# cat /proc/interrupts 
     CPU0  CPU1   CPU2   CPU3 
 0: 950901695    0    0     0 IO-APIC-edge timer 
 1:    13    0    0     0 IO-APIC-edge i8042 
 6:    96  10989   470     0 IO-APIC-edge floppy 
 7:     0    0    0     0 IO-APIC-edge parport0 
 8:     1    0    0     0 IO-APIC-edge rtc 
 9:     0    0    0     0 IO-APIC-level acpi 
 12:    109  1787    0     0 IO-APIC-edge i8042 
 15:    99 84813914    0     0 IO-APIC-edge ide1 
 51:   17371    0 46689970     0 IO-APIC-level ioc0 
 67:   1741    0    0 225409160 PCI-MSI eth0 
 83:     0    0    0     0 PCI-MSI vmci 
NMI:     0    0    0     0
LOC: 950902917 950903742 950901202 950901400
ERR:     0
MIS:     0
Copy after login

Irqbalance对于包含多个核心的系统来说是非常有用的。因为通常中断只被第一个CPU核心服务。以上就是教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“Linux”,或扫描下方二维码进行关注,更多干货等着你 !

Linux CPU 中断:异步事件和通用处理机制

The above is the detailed content of Linux CPU Interrupts: Asynchronous Events and Common Handling Mechanisms. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1254
29
C# Tutorial
1228
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles