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

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!

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

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.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

Introduction: Securing the Digital Frontier with Linux-Based Ethical Hacking In our increasingly interconnected world, cybersecurity is paramount. Ethical hacking and penetration testing are vital for proactively identifying and mitigating vulnerabi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor