操作系统笔记 CH13 I/O Systems

the kernel is structured to use device-driver modules.

device driver为IO子系统提供了统一设备访问接口

两个趋势:接口日益标准化,设备多样性

IO 硬件

port

bus

controller

Controller usually have registers, where device driver places commands, addresses, and data to write, or read data from registers after command execution. Data-in register, data-out register, status register, control register

设备有地址

  • Direct I/O instructions

  • Memory-mapped I/O 内存映射IO

    设备控制寄存器 被映射到 处理器的地址空间

    Especially for large address spaces (graphics)

IO端口有4种寄存器:

  • status
  • control
  • data-in
  • data-out

Techniques for Performing I/O

  • Programmed I/O
  • Interrupt-driven I/O
  • Direct Memory Access (DMA)

polling 轮询

控制器通过状态寄存器的busy bit显示其状态

主机通过命令寄存器中的command-ready bit表示其意愿

busy-waiting / polling 主机不断读状态寄存器直到忙位被清除

Polling can happen in 3 instruction cycles, Read status, logical-and to extract status bit, branch if not zero

interrupts

Interrupt-request line

Interrupt handler receives interrupts. Maskable to ignore or delay some interrupts

Interrupt vector to dispatch interrupt to correct handler

中断向量:中断处理程序的内存地址

计算机设备比向量内地址多。interrupt chaining 中断向量内的每个元素都指向中断处理程序列表的头

实现了中断优先级

Interrupt mechanism also used for exceptions

Good uses: system calls, page fault

Used for time-sensitive processing, frequent, must be fast.

DMA

Requires DMA controller, a special-purpose processor.

Bypasses CPU to transfer data directly between I/O device and memory.

Host writes a DMA command block into memory.

CPU writes the address of this command block to the DMA controller.

DMA controller operates the memory bus directly, placing address on the bus to perform transfers.

When the entire transfer is finished, the DMA controller interrupts the CPU.

Handshaking between the DMA controller and the device controller:

​ performed via a pair of wires called DMA-request and DMA-acknowledge.

DMA控制器抓住内存总线时,CPU暂时不能访问主存,但可以访问一级或二级高速缓存中的数据项。

周期挪用cycle stealing可能放慢CPU计算,但数据传输工作交给DMA控制器改善整体性能

DVMA (direct virtual memory access) can perform a transfer between two memory-mapped devices without the intervention of the CPU or the use of main memory. 直接实现两个内存映射设备之间的传输,无需CPU的干涉或使用主存

IO应用接口

设备驱动程序层device-driver layer为内核IO子系统隐藏设备控制器之间的差异

Broadly I/O devices can be grouped by the OS into

  • block IO
  • character-stream IO
  • memory-mapped file access
  • network sockets

OS存在后门(escape / back door),允许应用程序将任何命令透明地传输到设备控制器

Unix ioctl()

块与字符设备

Block devices

include disk drives

Commands include read(), write(), seek()

Raw I/O, direct I/O, or file-system access

Memory-mapped file access possible

DMA

Character devices

include keyboards, mice, serial ports

Commands include get(), put()

网络设备

The socket interface, Linux, Unix and Windows

Includes select functionality: manages a set of sockets

clocks and timers

Provide current time, elapsed time, timer

Programmable interval timer used for timings, periodic interrupts.

Blocking I/O and Nonblocking I/O

Blocking - process suspended until I/O completed

Nonblocking - I/O call returns as much as available

Asynchronous - process runs while I/O executes 不必等待IO完成就可立即返回

Kernel I/O Subsystem

IO scheduling

为每个设备维护一个请求队列

buffering

保存两个设备之间 或 设备和应用程序之间 所传输数据的内存区域

caching

高速缓存是可以保留数据副本的高速存储器

buffer可能是数据的唯一副本,而cache只是提供了一个 驻留在其他地方的数据 在高速存储上的一个副本

有时同一内存区域可同时用作cache和buffer

Spooling假脱机 & Device reservation

Spooling - hold output for a device

Device reservation( 预约) - provides exclusive access to a device

error handling

Most return an error number or code when I/O request fails.

System error logs hold problem reports

IO protection

所有IO指令为特权指令

IO必须通过system call执行

内核数据结构

Kernel keeps state information for I/O components, including open file tables, network connections, character device state.

Many complex data structures to track buffers, memory allocation, “dirty” blocks.

Some use object-oriented methods and message passing to implement I/O.

把IO操作转换成硬件操作


最后修改于 2020-02-28