In continuation of the previous text 第10章:中断处理-13: The /proc Interface and Shared Interrupts, let's GO ahead.
Interrupt-Driven I/O
Whenever a data transfer to or from the managed hardware might be delayed for any reason, the driver writer should implement buffering. Data buffers help to detach data transmission and reception from the write and read system calls, and overall system performance benefits.
当与所管理硬件之间的数据传输可能因任何原因延迟时,驱动开发者应实现缓冲机制。数据缓冲区有助于将数据发送 / 接收与读写系统调用解耦,从而提升整体系统性能。
A good buffering mechanism leads to interrupt-driven I/O, in which an input buffer is filled at interrupt time and is emptied by processes that read the device; an output buffer is filled by processes that write to the device and is emptied at interrupt time. An example of interrupt-driven output is the implementation of /dev/shortprint.
良好的缓冲机制会催生中断驱动 I/O:输入缓冲区在中断发生时被填充,由读取设备的进程清空;输出缓冲区由写入设备的进程填充,在中断发生时被清空。/dev/shortprint 的实现就是中断驱动输出的一个例子。
For interrupt-driven data transfer to happen successfully, the hardware should be able to generate interrupts with the following semantics:
• For input, the device interrupts the processor when new data has arrived and is ready to be retrieved by the system processor. The actual actions to perform depend on whether the device uses I/O ports, memory mapping, or DMA.
• For output, the device delivers an interrupt either when it is ready to accept new data or to acknowledge a successful data transfer. Memory-mapped and DMAcapable devices usually generate interrupts to tell the system they are done with the buffer.
The timing relationships between a read or write and the actual arrival of data were introduced in the section “Blocking and Nonblocking Operations” in Chapter 6.
要成功实现中断驱动的数据传输,硬件需能产生具备以下语义的中断:
-
输入场景:当新数据到达且可被系统处理器读取时,设备触发处理器中断。具体执行的操作取决于设备使用 I/O 端口、内存映射还是 DMA 方式。
-
输出场景:设备在两种情况下触发中断 —— 一是准备好接收新数据,二是确认数据传输成功。支持内存映射和 DMA 的设备,通常会在缓冲区处理完成后产生中断,告知系统操作结束。
关于读写操作与数据实际到达时间的关系,已在第 6 章 “阻塞与非阻塞操作” 一节中介绍过。
补充说明:
-
中断驱动 I/O 的核心优势
相比轮询(Polling)方式,中断驱动 I/O 无需处理器持续查询设备状态,仅在设备就绪时才响应,大幅降低 CPU 占用率,尤其适合数据传输频率低、延迟不确定的场景(如键盘、鼠标等外设)。
-
缓冲区的关键作用
缓冲区实现了 “生产者 - 消费者” 模型:
-
输出场景:用户进程(生产者)将数据写入缓冲区,中断处理程序(消费者)将缓冲区数据发送到硬件。这种解耦让进程无需等待硬件就绪,提升了系统并发能力。
-
输入场景:中断处理程序(生产者)将硬件数据写入缓冲区,用户进程(消费者)从缓冲区读取数据;
-
-
不同硬件传输方式的中断处理差异
-
I/O 端口:中断处理程序通过
inb/outb等指令读写端口数据; -
内存映射:通过读写内存地址(设备寄存器映射地址)处理数据,效率更高;
-
DMA:中断仅用于通知 DMA 传输完成,数据无需 CPU 中转,适合大数据量传输(如磁盘、网卡)。
-
-
中断驱动 I/O 的局限性
若设备中断频率极高(如高速网卡),频繁的中断响应会带来上下文切换开销,此时可结合 “中断聚合”(批量处理多个中断)或 “轮询 - 中断混合模式” 优化性能。
2423

被折叠的 条评论
为什么被折叠?



