Essentially, an interrupt is an electrical signal. When a certain event occurs in a device, it generates an interrupt and sends the electrical signal to the interrupt controller via the bus.
If the interrupt line is active, the interrupt controller sends an electrical signal to a specific pin on the processor. The processor then immediately stops what it is doing and jumps to the entry point of the interrupt handler to process the interrupt.
Hard interrupt
Interrupts are automatically generated by peripherals connected to the system (such as network cards and hard drives). They are primarily used to notify the operating system of changes in the status of system peripherals. For example, when a network card receives a data packet, it will issue an interrupt. The interrupts we usually refer to are hardware interrupts (hardirqs).
Hardware interrupts are generated by hardware, such as disks, network cards, keyboards, and clocks. Each device or set of devices has its own IRQ (Interrupt Request). Based on the IRQ, the CPU can dispatch the corresponding request to the appropriate hardware driver (note: a hardware driver is usually a subroutine in the kernel, not a separate process).
Interrupt handling drivers need to run on the CPU. Therefore, when an interrupt occurs, the CPU will suspend the currently running task to handle the interrupt. On multi-core systems, an interrupt can usually only interrupt one CPU (there is a special case: on mainframes, there are hardware channels that can handle multiple interrupts simultaneously without the support of the main CPU).
A hardware interrupt can directly interrupt the CPU. It triggers relevant code in the kernel. For processes that require some time to process, the interrupt code itself can also be interrupted by other hardware interrupts.
In the event of a clock interrupt, the kernel scheduling code suspends the currently running process, allowing other processes to run. Its purpose is to enable the scheduling code (or scheduler) to schedule multiple tasks.
Soft interrupt
To meet the requirements of real-time systems, interrupt handling should be as fast as possible. To achieve this, Linux uses hardware interrupts to handle tasks that can be completed quickly when an interrupt occurs, while tasks that take longer to process are handled by software interrupts (softirqs).
Soft interrupts are handled much like hard interrupts. However, they are only generated by currently running processes.
Typically, software interrupts are requests for I/O. These requests invoke kernel programs that can schedule I/O operations. For some devices, I/O requests need to be processed immediately, while disk I/O requests can usually be queued and processed later. Depending on the I/O model, a process may be suspended until the I/O is complete, at which point the kernel scheduler will select another process to run. I/O can occur between processes, and the scheduling process is generally the same as for disk I/O.
Software interrupts are only associated with the kernel. The kernel is primarily responsible for scheduling any other processes that need to run. Some kernels allow parts of a device driver to exist in user space, and the kernel will schedule that process to run when needed.
Soft interrupts do not directly interrupt the CPU. Only currently running code (or process) generates soft interrupts. This type of interrupt is a request for the kernel to perform some action (usually I/O) for the running process. A special type of soft interrupt is the `Yield` call, which requests the kernel scheduler to check if there are any other processes that can run.
Interruption Nesting
In Linux, hardware interrupts can be nested, but there is no concept of priority. This means that any new interrupt can interrupt an ongoing interrupt, except for interrupts of the same type. Software interrupts cannot be nested, but software interrupts of the same type can be executed in parallel on different CPUs.
Soft interrupt instructions
int is a software interrupt instruction.
The interrupt vector table is a table that maps interrupt numbers to the addresses of interrupt handler functions.
`int n` triggers a software interrupt `n`. The address of the corresponding interrupt handler function is: interrupt vector table address + 4 * `n`.
The difference between hardware interrupts and software interrupts
Software interrupts are generated by executing interrupt instructions, while hardware interrupts are triggered by peripheral devices.
The interrupt number for a hardware interrupt is provided by the interrupt controller, while the interrupt number for a software interrupt is directly specified by the instruction and does not require the use of an interrupt controller.
Hardware interrupts are maskable, while software interrupts are not.
The hardware interrupt handler must ensure that it can complete its task quickly so that the program execution does not wait for a long time; this is called the top half.
Soft interrupts handle tasks not completed by hard interrupts; this is a mechanism for postponing execution and belongs to the bottom half.
switch
(1) Hardware interrupt switch
Simply disable and enable local interrupts on the current processor: `local_irq_disable(); local_irq_enable();`
Saving the disabled and enabled states of the local interrupt system: `unsigned long flags; local_irq_save(flags); local_irq_restore(flags);`
(2) Soft interrupt switch
Disable bottom halves, such as softirqs, tasklets, and workqueues: `local_bh_disable(); local_bh_enable();`
It should be noted that even when the bottom half is disabled, it can still be preempted by a hard interrupt.
(3) Determine the interrupt status
#define in_interrupt()(irq_count()) // Whether the system is in an interrupt state (hard interrupt or soft interrupt) #define in_irq()(hardirq_count()) // Whether the system is in a hard interrupt state #define in_softirq()(softirq_count()) // Whether the system is in a soft interrupt state
Disclaimer: This article is a reprint. If there are any copyright issues, please contact us promptly for deletion (QQ: 2737591964). We apologize for any inconvenience.