Chapter 13: I/O Systems

Language: Other Language

1. I/O Hardware

1.1 设备连接与总线

  • Port (端口)
    • The device communicates with the machine via a connection point, i.e., port.
    • 设备通过连接点(即端口)与机器通信。
  • Bus (总线)
    • If devices share a common set of wires, the connection is called a bus (wires + protocols).
    • 如果设备共享一组导线,这种连接称为总线(导线 + 协议)。
  • Daisy Chain (菊花链)
    • When device A plugs into device B, which plugs into device C, and C plugs into a port, this is a daisy chain. It usually operates as a bus.
    • 设备A连接设备B,B连接设备C,C连接到计算机端口,这种结构称为菊花链,通常作为总线运行。

1.2 常见总线类型

  • PCI Bus (PCI总线)
    • Connects the processor-memory subsystem to fast devices.
    • 连接处理器-内存子系统与高速设备。
  • Expansion Bus (扩展总线)
    • Connects slow devices (e.g., keyboard, USB ports).
    • 连接低速设备(如键盘、USB端口)。
  • SCSI Bus (SCSI总线) Small Computer System Interface
    • Connects disks via a SCSI controller.
    • 通过SCSI控制器连接磁盘。
  • PCIe (PCI Express)
    • PCIe: Throughput up to 16 GB/s.
  • HyperTransport
    • HyperTransport: Throughput up to 25 GB/s.

1.3 控制器(Controller)

  • A controller operates a port, bus, or device.
  • 控制器是控制端口、总线或设备的电子组件。
  • Example:
    • SCSI bus controller is often implemented as a separate circuit board.
    • 某些设备(如磁盘)内置控制器(e.g., SATA控制器)。

1.4 标准设备结构(Canonical Device)

  • A device has two important components.
    • The first is the hardware interface it presents to the rest of the system
    • The second part of any device is its internal structure, which is implementation specific.
  • 硬件接口(Hardware Interface)
    • Registers:
      • Status Register (状态寄存器): 读取设备状态。
      • Command Register (命令寄存器): 发送任务指令。
      • Data Register (数据寄存器): 传输数据。
  • 内部结构(Internal Structure)
    • Micro-controller, memory (DRAM/SRAM), hardware-specific chips.

1.5 轮询 vs. 中断(Polling vs. Interrupts)

  • Polling (轮询)
    while (STATUS == BUSY); // 等待设备空闲  
    Write data to DATA register;  
    Write command to COMMAND register;  
      (Doing so starts the device and executes the command.)
    while (STATUS == BUSY); // 等待操作完成  
    • 效率低(Inefficient)。
  • Interrupt-Driven (中断驱动)
    • OS issues a request, sleeps the process, and switches tasks.
    • 操作系统可以发出请求,让调用进程进入睡眠状态,然后通过上下文切换执行其他任务,而不必反复轮询设备。当设备最终完成操作时,会触发一个硬件中断,使CPU跳转到操作系统中预先设定的中断服务程序(ISR),或者更简单地说,跳转到中断处理程序。
    • 避免CPU空转。
  • Hybrid Approach (混合方法)
    • 如果设备速度快,轮询可能是最佳选择;如果设备速度慢,中断则更为合适。
    • 如果设备的速度未知,或者时快时慢,最好采用混合方式:先轮询一段时间,若设备尚未完成操作,则改用中断方式。
  • Livelock (活锁)
    • 高频率中断导致OS无法处理用户请求(e.g., 网络流量激增)。
    • Coalescing (合并中断): 将多个中断合并为单个。

1.6 直接内存访问(Direct Memory Access, DMA)

  • Problem: CPU负担过重(如大数据传输)。
  • DMA引擎本质上是系统内一种特殊的设备,它能够在无需CPU过多干预的情况下,协调 orchestrate 设备与主内存之间的数据传输。
  • Solution: DMA引擎协调设备与内存间的传输,无需CPU干预。
    CPU -> DMA -> Disk (高效传输)  
  • 设备通信方式
    • I/O Instructions (I/O指令)
      • e.g., x86的in/out指令。
      • Such instructions are usually privileged.
    • Memory-Mapped I/O (内存映射I/O)
      • 设备寄存器映射为内存地址,直接读写。
      • The hardware makes device registers available as if they were memory locations.

1.7 设备驱动(Device Driver)

  • Lowest-level OS software that knows device details.
  • 最底层的OS软件,负责设备的具体操作。

2. Application I/O Interface

2.1 设备分类维度

  • Character-stream vs. Block
    • 字符流设备(如键盘)逐字节传输,块设备(如磁盘)按块传输。
  • Sequential vs. Random Access
    • 顺序设备(如磁带)按固定顺序传输,随机访问设备(如CD-ROM)可寻址。
  • Synchronous vs. Asynchronous
    • 同步设备(如磁带)响应时间可预测,异步设备(如键盘)响应不规则。
  • Sharable vs. Dedicated
    • 共享设备(如键盘)允许多进程并发使用,独占设备(如磁带)不能。

2.2 块设备接口(Block-Device Interface)

  • Raw I/O (原始I/O): 操作系统本身以及数据库管理系统等特殊应用程序,可能更倾向于将块设备作为简单的线性块数组来访问。
  • Direct I/O (直接I/O): 操作系统允许对文件采用一种禁用缓冲和锁定的操作模式。

2.3 网络设备(Network Devices)

  • Socket Interface (套接字接口): 进程通过套接字发送/接收数据包。

2.4 时钟与定时器(Clocks and Timers)

  • 功能:
    • Give the current time.
    • Give the elapsed time.
    • Set a timer to trigger operation X at time T
  • Programmable Interval Timer (可编程间隔定时器)
    • The hardware to measure elapsed time 持续时间 and to trigger operations is called a programmable interval timer.
    • On many computers, the interrupt rate generated by the hardware clock is 18 ∼ 60 ticks per second.

2.5 非阻塞与异步I/O(Nonblocking & Asynchronous I/O)

  • Asynchronous I/O (异步I/O)
    • 接口使应用程序能够发出输入/输出(I/O)请求,并在I/O完成之前立即将控制权返回给调用方
    • 附加接口(I/O中断)使应用程序能够确定各种I/O操作是否已完成
  • AIO Control Block (异步I/O控制块):
    struct aiocb {  
      int aio_fildes;     // 文件描述符  
      off_t aio_offset;   // 文件偏移量  
      void* aio_buf;      // 缓冲区地址  
      size_t aio_nbytes;  // 传输长度  
    };  

2.6 向量I/O(Vectored I/O)

  • Vectored I/O allows one system call to perform multiple I/O operations involving multiple locations.
  • 单次系统调用执行多缓冲区I/O操作(如UNIX的readv/writev)。

3. Kernel I/O Subsystem

3.1 I/O调度(I/O Scheduling)

  • 决定I/O请求的执行顺序以优化性能。
    3.2 缓冲(Buffering)
  • 解决生产者与消费者的速度不匹配问题。
  • 支持不同传输大小的设备适配。
    3.3 缓存(Caching)
  • 快速内存区域存储数据副本,加速访问。
    3.4 假脱机与设备预留(Spooling & Device Reservation)
  • Spooling: 缓冲输出到磁盘(如打印机无法交错数据流)。
  • Device Reservation: 协调并发应用对独占设备的访问。
    3.5 错误处理(Error Handling)
  • 系统调用返回状态位(成功/失败)。
    3.6 I/O保护(I/O Protection)
  • I/O指令为特权指令,内存映射I/O受内存保护系统限制。
    3.7 内核数据结构(Kernel Data Structures)
  • UNIX: 文件系统统一访问文件、原始设备、进程地址空间。
  • Windows: 基于消息传递的I/O实现(请求转为消息传递给I/O管理器)。
    3.8 性能优化(Performance Optimization)
  • Reducing Context Switches (减少上下文切换)
    • Minimize switches between user and kernel modes during I/O.
    • 减少I/O过程中用户态与内核态的切换。
  • Zero-Copy I/O (零拷贝I/O)
    • Data is copied directly from disk to network buffers without intermediate kernel buffers.
    • 数据直接从磁盘到网络缓冲区,避免内核缓冲区的中间拷贝。
  • Kernel Bypass (内核旁路)
    • Allow user-space applications to directly access hardware (e.g., RDMA in high-speed networks).
    • 用户态程序直接访问硬件(如高速网络中的RDMA)。
      3.9 设备管理(Device Management)
  • Plug and Play (PnP, 即插即用)
    • Automatically detect and configure devices without user intervention.
    • 自动检测和配置设备,无需用户干预。
  • Hot Swapping (热插拔)
    • Devices can be added/removed while the system is running (e.g., USB drives).
    • 系统运行时动态添加/移除设备(如USB驱动器)。
      3.10 电源管理(Power Management)
  • Advanced Configuration and Power Interface (ACPI)
    • OS controls device power states (e.g., sleep, hibernation) to save energy.
    • 操作系统控制设备电源状态(如睡眠、休眠)以节省能耗。

4. Summary of Key Concepts

ConceptDescription
Port vs. BusPort: Single connection point; Bus: Shared wires with protocols.
DMAOffloads data transfer from CPU to dedicated engine.
Interrupts vs. PollingInterrupts efficient for slow devices; polling for fast devices.
Block vs. CharacterBlock devices (e.g., disks) transfer fixed-size units; character devices (e.g., keyboards) stream bytes.
Asynchronous I/ONon-blocking calls with completion notifications.
Kernel BufferingResolves speed mismatches between producers and consumers.