OS-12:I/O Systems

介绍I/O系统的相关概念。

Overview

Ports, buses, and device controllers connect to various devices and peripherals.

I/O Hardware

设备类型: Storage, Transmission, and Human-Computer Interface

端口是设备连接点;总线用于设备间数据传输,常见的有 PCI 总线、PCI Express(PCIe)等;控制器负责操作端口、总线和设备 ,部分集成,部分为独立电路板。

A Typical PC Bus Structure
1

Host-Controller Communication

有特殊 I/O 指令和内存映射 I/O 两种方式。数据传输控制方式包括轮询和中断,轮询适用于快速设备,中断可提高效率 。直接内存访问(DMA)用于大数据传输,可绕过 CPU 直接在 I/O 设备和内存间传输数据。

Application I/O Interface

I/O 系统调用将设备行为封装为通用类,让应用程序能以统一方式访问不同设备。设备驱动层则对内核隐藏了 I/O 控制器的差异。

aspectvariationexample
data-transfer modecharacter blockterminal disk
access methodsequential randommodem CD-ROM
transfer schedulesynchronous asynchronoustape keyboard
sharingdedicated sharabletape keyboard
device speedlatency seek time transfer rate delay between operations
I/O directionread only write only read-writeCD-ROM graphics controller disk

操作系统会对 I/O 设备进行分类:

  • 块 I/O 设备如磁盘驱动器,以数据块为单位进行数据传输和存储,支持诸如读取、写入、寻道等操作,还能实现原始 I/O、直接 I/O 或文件系统访问,并且内存映射文件访问可基于块设备驱动实现。- 字符 I/O 设备包含键盘、鼠标、串口等,以字符流的形式传输数据,相关命令有 get ()、put () ,通过上层库可实现行编辑功能。
  • 内存映射文件访问,能将文件映射到虚拟内存,通过按需分页获取数据块。
  • 网络套接字用于网络通信,其性能与块设备和字符设备不同,拥有独立的接口,常见于 Linux、Unix、Windows 等操作系统,通过 socket 接口实现网络协议与网络操作的分离,并包含 select () 函数,用于返回哪些套接字有数据包等待或有空间接受新数据包

1

Vectored I/O

Vectored I/O 是一种高效的 I/O 方式,允许在单个系统调用中传输多个数据块。它通过将多个缓冲区的地址和长度传递给内核来实现,内核会将数据从这些缓冲区中读取或写入,从而减少了系统调用的次数,提高了性能。

Kernel I/O Subsystem

1

  • I/O Scheduler: 负责调度 I/O 请求,决定请求的执行顺序和方式,以提高系统性能。
  • Buffering: 在内存中为 I/O 操作分配缓冲区,以提高数据传输效率。
  • Caching: 将频繁访问的数据存储在内存中,以减少对磁盘的访问,提高性能。
  • Spooling: 假脱机,在磁盘上开辟缓冲区暂存设备输出数据,常用于打印机等设备。
  • Device reservation: 设备保留,确保在 I/O 操作期间设备不会被其他进程使用。
  • Error handling: 错误处理,负责检测和处理 I/O 操作中的错误。

I/O 保护:为防止用户进程通过非法 I/O 指令干扰系统正常运行,将所有 I/O 指令设为特权指令,用户只能通过系统调用执行 I/O 操作。

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

Transforming I/O Requests to Hardware Operations

Consider reading a file from disk for a process:

  1. Determine the device holding the file
  2. Translate name to device representation
  3. Physically read data from disk into the buffer
  4. Make data available to the requesting process
  5. Return control to process

File System Layers and Operations
1

Life Cycle of An I/O Request
1