中断 | Interrupts in Operating Systems

定义 | Definition

中断是一种信号,用于通知处理器需要立即注意的事件,使处理器暂停当前的指令执行并处理该事件。

An interrupt is a signal that informs the processor of an event that needs immediate attention, causing the processor to pause the current instruction execution and handle the event.

中断类型 | Types of Interrupts

硬件中断 | Hardware Interrupts

由外部设备生成的信号,通知处理器需要处理外部事件。

Signals generated by external devices, informing the processor to handle an external event.

示例 | Examples

  • 时钟中断 | Timer Interrupt:由系统时钟定期生成,用于操作系统的时间管理。 Generated periodically by the system clock for time management in the operating system.
  • I/O 中断 | I/O Interrupt:由输入/输出设备生成,通知数据传输完成或设备状态改变。 Generated by input/output devices to notify the completion of data transfer or a change in device status.
  • 电源中断 | Power Interrupt:由电源管理单元生成,用于处理电源状态的变化。 Generated by the power management unit to handle changes in power state.
  • 缺页中断 | Page Fault:当程序访问不在物理内存中的页面时触发。 Triggered when a program accesses a page that is not in physical memory. 缺页中断 Page Fault

软件中断 | Software Interrupts

由程序生成的信号,通常用于系统调用或异常处理。

Signals generated by programs, typically for system calls or exception handling.

示例 | Examples

  • 系统调用 | System Calls:由应用程序发起,用于请求操作系统服务。 Initiated by application programs to request services from the operating system.

  • 异常 | Exceptions:由处理器检测到异常条件(如除零错误)时生成。 Generated when the processor detects abnormal conditions (e.g., divide by zero error).

中断请求 | Interrupt Requests (IRQ)

中断请求线用于传递硬件中断信号。

Interrupt request lines are used to transmit hardware interrupt signals.

每个设备分配一个唯一的IRQ线。
Each device is assigned a unique IRQ line.

可屏蔽中断和不可屏蔽中断 | Maskable and Non-Maskable Interrupts

  • 可屏蔽中断 | Maskable Interrupts (IRQ):可以被处理器屏蔽或忽略。 Can be masked or ignored by the processor.
  • 不可屏蔽中断 | Non-Maskable Interrupts (NMI):无法被屏蔽,通常用于高优先级事件。 Cannot be masked, typically used for high-priority events.

中断诱因 | Causes of Interrupts

硬件中断 | Hardware Causes

  • 外设请求 | Peripheral Requests:如键盘、鼠标、网络接口等外设需要处理器的服务。 Peripherals like keyboard, mouse, network interface requiring processor service.

  • 定时器事件 | Timer Events:用于周期性任务调度和时间管理。 Used for periodic task scheduling and time management.

  • 硬件故障 | Hardware Failures:如内存错误、电源故障等。 Hardware errors such as memory errors, power failures.

  • 内存管理事件 | Memory Management Events

    • 缺页中断 | Page Fault:程序访问不在物理内存中的页面时,由内存管理单元(MMU)触发。 Triggered by the Memory Management Unit (MMU) when a program accesses a page not in physical memory.
    • 内存保护违规 | Memory Protection Violation:程序试图访问没有权限的内存区域时触发。 Triggered when a program attempts to access a memory area without proper permissions.

软件中断 | Software Causes

  • 系统调用 | System Calls:应用程序请求操作系统执行特定任务。 Application programs requesting the operating system to perform specific tasks.
  • 异常 | Exceptions:如非法指令、算术错误等异常条件。 Abnormal conditions such as illegal instructions, arithmetic errors.

中断传导方法 | Interrupt Propagation

中断请求线 | Interrupt Request Lines

外部设备通过中断请求线向中断控制器发送信号。

External devices send signals to the interrupt controller via interrupt request lines.

中断控制器 | Interrupt Controller

中断控制器管理多个中断请求,并根据优先级将请求发送到处理器。

The interrupt controller manages multiple interrupt requests and sends them to the processor based on priority.

可编程中断控制器 | Programmable Interrupt Controller (PIC)

允许编程设置中断优先级和屏蔽特定中断。

Allows programming of interrupt priorities and masking of specific interrupts.

处理器响应 | Processor Response

处理器响应中断请求,保存当前状态,并转移到中断处理程序。

The processor responds to an interrupt request, saves the current state, and transfers control to the interrupt handler.

中断处理方法 | Interrupt Handling

保存处理器状态 | Saving Processor State

处理器保存当前的寄存器状态和程序计数器,以便中断处理完成后恢复。

The processor saves the current register state and program counter to restore after interrupt handling.

中断向量表 | Interrupt Vector Table

中断向量表是一个存储在内存中的数据结构,包含了各个中断对应的中断处理程序的入口地址。处理器在接收到中断信号时,通过查找中断向量表来确定中断处理程序的地址,从而转移控制权到相应的处理程序。

The interrupt vector table is a data structure stored in memory, containing the entry addresses of interrupt handlers for each interrupt. When the processor receives an interrupt signal, it looks up the interrupt vector table to determine the address of the interrupt handler and transfers control to the appropriate handler.

结构 | Structure

中断向量表的每个条目对应一个中断号(Interrupt Number),并包含该中断号对应的中断处理程序的入口地址。

Each entry in the interrupt vector table corresponds to an interrupt number and contains the entry address of the interrupt handler for that interrupt number.

+----------------------+--------------------------+
| Interrupt Number (n) | Interrupt Handler Address|
+----------------------+--------------------------+
| 0                    | Address of Handler 0     |
| 1                    | Address of Handler 1     |
| 2                    | Address of Handler 2     |
| ...                  | ...                      |
| n                    | Address of Handler n     |
+----------------------+--------------------------+

示例 | Example

假设有一个简单的中断向量表,包含三个中断处理程序的入口地址:

Assume a simple interrupt vector table with entry addresses for three interrupt handlers:

中断号 | Interrupt Number: 0, 1, 2
中断处理程序地址 | Interrupt Handler Address:
0x0000_1000 (Handler for Interrupt 0)
0x0000_2000 (Handler for Interrupt 1)
0x0000_3000 (Handler for Interrupt 2)

初始化 | Initialization

在系统启动时,操作系统会初始化中断向量表,将各个中断处理程序的地址填入对应的条目。

At system startup, the operating system initializes the interrupt vector table, filling in the addresses of the interrupt handlers into the corresponding entries.

// Example in C
#define NUM_INTERRUPTS 256
void (*interrupt_vector_table[NUM_INTERRUPTS])();
 
void init_interrupt_vector_table() {
    interrupt_vector_table[0] = handler0;
    interrupt_vector_table[1] = handler1;
    interrupt_vector_table[2] = handler2;
    // Initialize other handlers...
}
 
void handler0() {
    // Handle interrupt 0
}
 
void handler1() {
    // Handle interrupt 1
}
 
void handler2() {
    // Handle interrupt 2
}

查找和跳转 | Lookup and Jump

处理器接收到中断请求后,通过中断号查找中断向量表,并跳转到对应的中断处理程序。

Upon receiving an interrupt request, the processor uses the interrupt number to look up the interrupt vector table and jumps to the corresponding interrupt handler.

1. 保存当前处理器状态 | Save the current processor state
2. 从中断控制器获取中断号 | Get the interrupt number from the interrupt controller
3. 查找中断向量表获取中断处理程序地址 | Lookup the interrupt vector table to get the handler address
4. 跳转到中断处理程序 | Jump to the interrupt handler
5. 执行中断处理程序 | Execute the interrupt handler
6. 恢复处理器状态并继续执行被中断的程序 | Restore the processor state and continue executing the interrupted program

中断向量表在不同系统中的实现 | Implementation in Different Systems

  • x86 架构 | x86 Architecture:在 x86 系统中,中断向量表称为中断描述符表(Interrupt Descriptor Table, IDT),每个条目包含中断门(Interrupt Gate)的信息,用于保护模式下的中断处理。 In x86 systems, the interrupt vector table is known as the Interrupt Descriptor Table (IDT), with each entry containing information about an interrupt gate for interrupt handling in protected mode.
  • ARM 架构 | ARM Architecture:在 ARM 系统中,中断向量表通常位于固定地址,处理器在中断时通过偏移量查找中断处理程序地址。 In ARM systems, the interrupt vector table is usually located at a fixed address, and the processor uses offsets to find the interrupt handler addresses during interrupts.
  • 嵌入式系统 | Embedded Systems:嵌入式系统中的中断向量表通常根据特定的微控制器架构进行定制,可能包含直接跳转指令或处理程序入口地址。 In embedded systems, the interrupt vector table is typically customized based on the specific microcontroller architecture, possibly containing direct jump instructions or handler entry addresses.

执行中断处理程序 | Executing Interrupt Handler

处理器转移控制权给中断处理程序,中断处理程序执行相应的任务。

The processor transfers control to the interrupt handler, which performs the necessary tasks.

示例 | Example

// Example in C
void interrupt_handler() {
    // Save processor state
    save_state();
 
    // Handle the interrupt
    if (interrupt_number == TIMER_INTERRUPT) {
        handle_timer_interrupt();
    } else if (interrupt_number == IO_INTERRUPT) {
        handle_io_interrupt();
    }
 
    // Restore processor state
    restore_state();
}

恢复处理器状态 | Restoring Processor State

中断处理程序完成后,恢复处理器的寄存器状态和程序计数器,继续执行被中断的程序。

After the interrupt handler completes, it restores the processor’s register state and program counter to continue executing the interrupted program.

中断嵌套 | Interrupt Nesting

处理高优先级中断可以嵌套低优先级中断。

Higher-priority interrupts can nest lower-priority interrupts.

通过设置中断优先级级别来管理嵌套。

Managed by setting interrupt priority levels.

中断处理的优化 | Optimization of Interrupt Handling

快速中断处理 | Fast Interrupt Handling

减少中断处理程序的执行时间,提高系统响应速度。

Reduce the execution time of interrupt handlers to improve system responsiveness.

中断分离 | Interrupt Separation

将复杂的中断处理任务分离到延迟处理程序中执行。

Separate complex interrupt handling tasks into deferred procedures.

中断聚合 | Interrupt Aggregation

合并多个中断请求,减少处理器的中断负载。

Combine multiple interrupt requests to reduce the processor’s interrupt load.

中断的应用 | Applications of Interrupts

实时系统 | Real-Time Systems

中断用于处理高优先级实时事件。

Interrupts are used to handle high-priority real-time events.

操作系统 | Operating Systems

中断用于实现多任务调度、设备驱动程序和异常处理。

Interrupts are used for multitasking scheduling, device drivers, and exception handling.

嵌入式系统 | Embedded Systems

中断用于响应外部事件和传感器信号。

Interrupts are used to respond to external events and sensor signals.


中断是操作系统和嵌入式系统中至关重要的机制,通过高效的中断处理方法,可以显著提高系统的性能和响应速度。

Interrupts are crucial mechanisms in operating systems and embedded systems, and efficient interrupt handling can significantly improve system performance and responsiveness.