There is no doubt that the Linux kernel needs to manage all the hardware devices connected to the computer. If you want to manage these devices, you need to communicate with them first, there are generally two scenarios for this functionality:
1. Polling (polling) allows the kernel to periodically query the status of the device, and then make the corresponding processing;
2. Interrupt (interrupt) allows the hardware to signal to the kernel when needed (the kernel is active for the hardware).
The first scenario makes the kernel do a lot of work, because polling periodically repeats, consumes CPU time, and is inefficient, so it's usually a second scenario.
What is interruption?
From a physical standpoint, an interruption is an electrical signal generated by a hardware device and fed directly to an interrupt controller (such as 8259A) on an input pin, which then sends the corresponding signal to the processor by the interrupt controller. Once the processor detects the signal, it interrupts the work it is currently working on, instead of processing the interrupt. Thereafter, the processor notifies the OS that an interrupt has been generated. This allows the OS to properly handle the interrupt.
Different devices have different interrupts, each with a unique digital ID, which is often referred to as an interrupt request line (IRQ).
Interrupts can be divided into synchronous (synchronous) interrupts and asynchronous (asynchronous) interrupts:
1. Synchronous interrupts are generated by the CPU control unit when the instruction is executed, and are called synchronizations because the CPU interrupts only after one instruction has been executed, rather than during the execution of code directives, such as system calls.
2. Asynchronous interrupts are those that are randomly generated by other hardware devices according to the CPU clock signal, meaning that interrupts can occur between instructions, such as keyboard interrupts.
What is an exception?
Synchronous interrupts are also known as exceptions (Exception), and asynchronous interrupts are called interrupts (interrupt). Interrupts that we normally speak of are asynchronous interrupts.
1. Interrupts can be divided into masked interrupts (maskable interrupt) and unshielded interrupts (nomaskable interrupt).
2. The exception can be divided into faults (fault), traps (trap), termination (abort) three classes.
See table 1 for similarities and differences between these categories.
Table 1: Interrupt categories and their behavior
Category reason asynchronous/synchronous return behavior
Interrupt signal from I/O device asynchronous always returns to the next instruction
Trap Intentional exception synchronization always returns to the next instruction
Failure potential recoverable error synchronization return to current instruction
Terminating an unrecoverable error synchronization does not return
What is an interrupt handler?
In response to a specific interrupt, the kernel executes a function called an interrupt handler or interrupt service routine. Each device that produces interrupts has a corresponding interrupt handler, and if a device can produce many different interrupts, the device can correspond to multiple interrupt handlers. An interrupt handler for a device is part of its device driver.
What is the upper and lower half of the interrupt?
Interrupt handling is typically divided into two parts, and the interrupt handler is the upper half: it executes immediately when an interrupt is received, but only with Strictly time-limited work, which is done with all interrupts being blocked. The work that can be allowed to be done later is postponed to the lower half. Typically, the lower half executes immediately when an interrupt handler returns.