Chapter 13: I/O Systems 13.2 I/O Hardware I/O硬件 13.3 Application I/O Interface 应用程序I/O接口 13.4 Kernel I/O Subsystem 核心I/O子系统 13.5 Transforming I/O Requests to Hardware Operations 转换I/O请求为硬件操作 13.6 STREAMS 流 13.7 Performance 性能 Operating System Concepts 13.1 Silberschatz, Galvin and Gagne 2002 13.1 Overview Main job of a computer:I/O and processing Role of the I/O system Manage and control I/O operations and I/O devices Two conflicting trends: Increasing standardization of software and hardware interfaces Increasingly broad variety of I/O devices Operating System Concepts 13.2 Silberschatz, Galvin and Gagne 2002 外设的特点: 种类多 差异大(控制和速度) 外设管理目的包括: 外设资源的控制 外设资源的共享 提高外设资源的利用率。 Operating System Concepts 13.3 Silberschatz, Galvin and Gagne 2002 外部设备类型和特征 1. 按交互对象分类 人机交互设备:视频显示设备、键盘、鼠标、打印机 与计算机或其他电子设备交互的设备:磁盘、磁带、 传感器、控制器 计算机间的通信设备:网卡、调制解调器 Operating System Concepts 13.4 Silberschatz, Galvin and Gagne 2002 2. 按交互方向分类 输入(可读):键盘、扫描仪 输出(可写):显示设备、打印机 输入/输出(可读写):磁盘、网卡 3. 按外设特性分类 使用特征:存储、输入/输出、终端 数据传输率:低速(如键盘)、中速(如打印机)、高速( 如网卡、磁盘) 信息组织特征:单个字符或数据块 字符设备(如打印机) 块设备(如磁盘) Operating System Concepts 13.5 Silberschatz, Galvin and Gagne 2002 13.2 I/O Hardware Incredible variety of I/O devices 难以置信的I/O设备种类 Storage devices Transmission devices Human-interface devices Common concepts 基本概念 Port 端口 – a connection point between a device and a computer Bus (daisy chain or shared direct access) 总线:菊花链或者共享总线 Controller (host adapter) 控制器(主机适配器) A collection of electronics that can operate a port, a bus, or a device Serial-port controller, SCSI bus controller … Operating System Concepts 13.6 Silberschatz, Galvin and Gagne 2002 Fig 13.1 A Typical PC Bus Structure Operating System Concepts 13.7 Silberschatz, Galvin and Gagne 2002 13.2 I/O Hardware I/O控制技术 I/O instructions control devices I/O指令控制设备 Devices have addresses, used by 设备的寻址方式 Direct I/O instructions 直接I/O指令 Memory-mapped I/O 存储器映射I/O指令 Device-control registers are mapped into the address space of the CPU Such as screen buffer of the graphic controller An typical I/O port include 4 registers: Status Control Data-in Data-out Operating System Concepts 13.8 Silberschatz, Galvin and Gagne 2002 Fig 13.2 Device I/O Port Locations on PCs (partial) Operating System Concepts 13.9 Silberschatz, Galvin and Gagne 2002 I/O控制技术 1. 程序控制I/O(programmed I/O) I/O操作由程序发起,并等待操作完成。数据的每次读 写通过CPU。常用的技术:轮询(polling) 缺点:在外设进行数据处理时,CPU只能等待。 2. 中断驱动方式(interrupt-driven I/O) I/O操作由程序发起,在操作完成时(如数据可读或已 经写入)由外设向CPU发出中断,通知该程序。数据 的每次读写通过CPU。 优点:在外设进行数据处理时,CPU不必等待,可以 继续执行该程序或其他程序。 缺点:CPU每次处理的数据量少(通常不超过几个字 节),只适于数据传输率较低的设备。 Operating System Concepts 13.10 Silberschatz, Galvin and Gagne 2002 3. 直接存储访问方式(DMA, Direct Memory Access) 由程序设置DMA控制器中的若干寄存器值(如 内存始址,传送字节数),然后发起I/O操作。 而后者完成内存与外设的成批数据交换,在操作 完成时由DMA控制器向CPU发出中断。 优点:CPU只需干预I/O操作的开始和结束,而 其中的一批数据读写无需CPU控制,适于高速设 备。 System Bus Processor Operating System Concepts DMA Controller I/O Controller 13.11 ..... I/O Controller Memory Silberschatz, Galvin and Gagne 2002 4. 通道控制方式(channel control) 通道控制器(Channel Processor)有自己的专用存储 器,可以执行由通道指令组成的通道程序,因此可 以进行较为复杂的I/O控制,如网卡上信道访问控制 。通道程序通常由操作系统所构造,放在内存里。 优点:执行一个通道程序可以完成几批I/O操作。 选择通道(selector channel):可以连接多个外设 ,而一次只能访问其中一个外设 多路通道(multiplexor channel):可以并发访问 多个外设。分为字节多路(byte)和数组多路 (block)通道。 Operating System Concepts 13.12 Silberschatz, Galvin and Gagne 2002 13.2.1 Polling 轮询 Determines state of device 决定设备的状态 command-ready(等待命令) Busy(忙) Error(错误) Busy-wait cycle to wait for I/O from device 忙等待循环等待设备的I/O操作 I/O操作由程序发起,并等待操作完成。数据的每次读写通 过CPU。 缺点:在外设进行数据处理时,CPU只能等待。 Operating System Concepts 13.13 Silberschatz, Galvin and Gagne 2002 13.2.2 Interrupts 中断 CPU Interrupt request line triggered by I/O device CPU的中断需要有I/O设备的触发 Interrupt handler receives interrupts 中断处理例程接收中断 Maskable to ignore or delay some interrupts 通过屏蔽来忽略或者延迟某些中断 Some un-maskable 某些中断不可屏蔽 Maskable nonmaskable Operating System Concepts 13.14 Silberschatz, Galvin and Gagne 2002 Interrupts 中断 Interrupt vector to dispatch interrupt to correct handler 中断向量给中断分配正确的中断处理例程 Based on priority 以优先级为基础 Interrupt priority levels Interrupt mechanism also used for exceptions 中断机制也用于异常处理, 如:除零,访问不存在的内存 地址等 Software interrupt (or a trap) Operating System Concepts 13.15 Silberschatz, Galvin and Gagne 2002 Fig 13.3 Interrupt-Driven I/O Cycle 中断驱动的I/O循环 Operating System Concepts 13.16 Silberschatz, Galvin and Gagne 2002 Fig 13.4 Intel Pentium Processor Event-Vector Table Operating System Concepts 13.17 Silberschatz, Galvin and Gagne 2002 13.2.3 Direct Memory Access直接内存存取 Used to avoid programmed I/O for large data movement 用来避免编程I/O来传输大量的数据 Requires DMA controller 需要DMA控制器 Bypasses CPU to transfer data directly between I/O device and memory 绕过CPU在I/O设备和内存之间直接传输数据 Cycle stealing Memory access Physical memory addresses for DMA Direct virtual-memory access (DVMA), using virtual address Operating System Concepts 13.18 Silberschatz, Galvin and Gagne 2002 Fig 13.6 Six Step Process to Perform DMA Transfer 通过六步来完成DMA传输 Operating System Concepts 13.19 Silberschatz, Galvin and Gagne 2002 直接存储访问方式(DMA, Direct Memory Access) 由程序设置DMA控制器中的若干寄存器值(如内存 始址,传送字节数),然后发起I/O操作。而后者完 成内存与外设的成批数据交换,在操作完成时由 DMA控制器向CPU发出中断。 优点:CPU只需干预I/O操作的开始和结束,而其中 的一批数据读写无需CPU控制,适用于高速设备。 System Bus Processor Operating System Concepts DMA Controller I/O Controller 13.20 ..... I/O Controller Memory Silberschatz, Galvin and Gagne 2002 Data Count Data Lines Address Lines DMA Request DMA Acknowledge Interrupt Read Write Data Register Address Register Control Logic DMA方式下的I/O控制器结构 13.2.4 通道控制方式(channel control) 通道控制器(Channel Processor)有自己的专用存储 器,可以执行由通道指令组成的通道程序,因此可 以进行较为复杂的I/O控制,如网卡上信道访问控制 。通道程序通常由操作系统所构造,放在内存里。 优点:执行一个通道程序可以完成几批I/O操作。 选择通道(selector channel):可以连接多个外设 ,而一次只能访问其中一个外设 多路通道(multiplexor channel):可以并发访问 多个外设。分为字节多路(byte)和数组多路 (block)通道。 Operating System Concepts 13.22 Silberschatz, Galvin and Gagne 2002 System Bus Processor Channel Processor Memory I/O Bus I/O Controller I/O Controller I/O Controller I/O Controller 13.3 Application I/O Interface 应用程序I/O接口 I/O system calls encapsulate device behaviors in generic classes 将I/O子系统设备分为几个常用的类别 Device-driver layer hides differences among I/O controllers from kernel 设备驱动层对OS核心隐藏了I/O控制器的不同细节 Devices vary in many dimensions 设备变化范围非常大 Character-stream or block 字符流或者块设备 Sequential or random-access 顺序或随机设备 Sharable or dedicated 共享或独占设备 Speed of operation 操作速度的不同 read-write, read only, or write only 读写,只读,只写 Operating System Concepts 13.24 Silberschatz, Galvin and Gagne 2002 Fig 13.6 A Kernel I/O Structure Operating System Concepts 13.25 Silberschatz, Galvin and Gagne 2002 Fig 13.7 Characteristics of I/O Devices Operating System Concepts 13.26 Silberschatz, Galvin and Gagne 2002 13.3.1 Block and Character Devices 块或字符设备 Block devices include disk drives 块设备包括磁盘 Commands include read, write, seek 命令包括读,写,搜寻 Raw I/O or file-system access 原始的I/O或文件系统存取 Memory-mapped file access possible 存储器映射文件访问的可能性 Character devices include keyboards, mice, serial ports字符设备包括键盘,鼠标, Modem和串口设备 Commands include get, put 命令有get,put Libraries layered on top allow line editing 顶层的函数库允许行编辑 Operating System Concepts 13.27 Silberschatz, Galvin and Gagne 2002 13.3.2 Network Devices网络设备 Varying enough from block and character to have own interface 从块设备和字符设备变化而来,有独自的接口 Unix and Windows NT/9i/2000 include socket interface Unix和Windows/NT包括socket接口 Separates network protocol from network operation 分离网络协议于网络操作 Includes select functionality 包括select功能—manage a set of sockets Approaches vary widely (half-duplex pipes, full-duplex FIFOs, full-duplex streams, queues, mailboxes, socket) 方式广泛:管道,FIFOs,流,队列,邮箱 Operating System Concepts 13.28 Silberschatz, Galvin and Gagne 2002 13.3.3 Clocks and Timers时钟和记时器 Provide current time, elapsed time, timer 提供当前的时间,经过的时间,定时器 If programmable interval time used for timings, periodic interrupts 如果把可编程的时间间隔用于计时,那么定期中断 programmable interval timer -- the hardware to measure elapsed time and to trigger operations ioctl (on UNIX) covers odd aspects of I/O such as clocks and timers 在UNIX中,ioct1掩盖了I/O设备(象时钟和计时器)之 间区别 Operating System Concepts 13.29 Silberschatz, Galvin and Gagne 2002 13.3.4 Blocking and Nonblocking I/O 阻塞和非阻塞I/O Blocking - process suspended until I/O completed 阻塞—当执行可阻塞I/O系统调用时,进程挂起直到I/O完成 Easy to use and understand 容易使用和理解 Insufficient for some needs 在某些场合没有效率 Nonblocking - I/O call returns as much as available 非阻塞—I/O调用返回一个代表是否成功的值 User interface, data copy (buffered I/O) 用户界面(键盘、鼠标输入)的同时显示视频、数据拷贝(对I/O缓冲) Implemented via multi-threading 通过多线程来实现 Returns quickly with count of bytes read or written 迅速的返回读写的字节数 Asynchronous - process runs while I/O executes 异步:在I/O处理的时候进程同时运行 Difficult to use 难以使用 I/O subsystem signals process when I/O completed 当I/O操作完成的时候,I/O子系统向进程发信号 Operating System Concepts 13.30 Silberschatz, Galvin and Gagne 2002 13.4 Kernel I/O Subsystem 核心I/O子系统 Kernel provide many services related to I/O: Scheduling Buffering Caching Spooling Device reservation Error handling Operating System Concepts 13.31 Silberschatz, Galvin and Gagne 2002 13.4.1 I/O Scheduling Improve overall system performance Share device access fairly among processes Reduce the average waiting time for I/O to complete Example: disk arm serves for many processes at same time Rearrange: some I/O request ordering via perdevice queue to improve the overall system efficiency。为改善系统性能,某些I/O需要按设备队 列的进行排序 Some OSs try fairness 某些操作系统尝试着公平 Operating System Concepts 13.32 Silberschatz, Galvin and Gagne 2002 13.4.2 I/O Buffering Buffering - store data in memory while transferring between devices 缓冲—当设备间传输数据的时候,暂时存放在内存中 引入缓冲的理由: To cope with device speed mismatch 解决设备速度不匹配。例如:Modem/Disk To cope with device transfer size mismatch 解决设备传输块的大小不匹配。例如:网络的拆包和 装包 To maintain “copy semantics” 为了维持拷贝语句的语义要求。确保数据的一致性 Operating System Concepts 13.33 Silberschatz, Galvin and Gagne 2002 Fig 13.8 Sun Enterprise 6000 Device-Transfer Rates Operating System Concepts 13.34 Silberschatz, Galvin and Gagne 2002 缓冲技术 缓冲技术可提高外设利用率,尽可能使外设处于忙状态 ;但有一个限制:进程的I/O请求不能超过外设的处理 能力。 1. 引入缓冲技术的目的 匹配CPU或用户应用进程与外设的不同处理速度 减少对CPU的中断次数,提高CPU和I/O设备之间以及 各个I/O设备之间的处理并行性。因此,缓冲区所在的 位置:内存,控制器或外设。这些在不同位置的缓冲区 组合在一起,构成多级缓冲机制。 Operating System Concepts 13.35 Silberschatz, Galvin and Gagne 2002 2. 单方向缓冲 单缓冲(single buffer):一个缓冲区,CPU和外设轮流使用, 一方处理完之后接着等待对方处理。 双缓冲(double buffer):两个缓冲区,CPU和外设都可以连 续处理而无需等待对方。要求CPU和外设的速度相近。 环形缓冲(circular buffer):多个缓冲区,CPU和外设的处 理速度可以相差较大。 Operating System Concepts 13.36 Silberschatz, Galvin and Gagne 2002 3. 缓冲池(buffer pool) 这是一种双方向缓冲技术;缓冲区整体利用率高。 缓冲区队列:三种:空闲缓冲区,输入缓冲区,输出缓冲 区 操作:四种:设备输入,CPU读入,设备输出,CPU写 出。上述操作访问各个缓冲区队列时,需要进行相应的互 斥操作。 Buffer Pool in queue Device empty queue CPU out queue Operating System Concepts 13.37 Silberschatz, Galvin and Gagne 2002 13.4.3 Caching Caching – a region of fast memory holding copies of data 高速缓存—存放数据的快速存储器 Always just a copy 总是数据的拷贝 Key purpose: to improve I/O performance and efficiency Caching and buffering are distinct(截然不同的) functions ,but sometimes a region of memory can be used for both purposes Caching: to improve performance Buffering: to cope with device transfer speed and size mismatch 解决设备传输块的速度与大小的不匹配 Operating System Concepts 13.38 Silberschatz, Galvin and Gagne 2002 13.4.4 Spooling(假脱机技术) A spool is a buffer that holds output for a device, such as a printer, that cannot accept interleaved data streams Spooling - hold output for a device spooling—保存设备的输出 If device can serve only one request at a time 如果设备一次只能服务于一个请求 i.e., Printing 比如,打印机 Device reservation - provides exclusive access to a device设备预定—提供对设备的独占访问 System calls for allocation and deallocation 分配和再分配的系统调用 Watch out for deadlock 小心死锁 Operating System Concepts 13.39 Silberschatz, Galvin and Gagne 2002 假脱机技术 利用假脱机技术(SPOOLing, Simultaneous Peripheral Operation On Line, 也称为虚拟设备技术 )可把独享设备转变成具有共享特征的虚拟设备,从而 提高设备利用率。 引入:在多道批处理系统中,专门利用一道程序( SPOOLing程序)来完成对设备的I/O操作。无需使用 外围I/O处理机。 Virtual I/O Actual I/O Application A Application B Operating System Concepts SPOOLing Program 13.40 Device Silberschatz, Galvin and Gagne 2002 假脱机技术 假脱机的原理: SPOOLing程序和外设进行数据交换,可以称为“ 实际I/O”。一方面,SPOOLing程序预先从外设输入 数据并加以缓冲,在以后需要的时候输入到应用程 序;另一方面,SPOOLing程序接受应用程序的输 出数据并加以缓冲,在以后适当的时候输出到外设 。 在SPOOLing程序中,需要管理两级缓冲区:内存 缓冲区和快速外存上的缓冲池,后者可以暂存多批 I/O操作的较多数据。 应用程序进行I/O操作时,只是和SPOOLing程序交 换数据,可以称为"虚拟I/O"。这时虚拟I/O实际上是 从SPOOLing程序的缓冲池中读出数据或把数据送 入缓冲池,而不是跟实际的外设进行I/O操作。 Operating System Concepts 13.41 Silberschatz, Galvin and Gagne 2002 假脱机技术 优点: 高速虚拟I/O操作:应用程序的虚拟I/O比实际I/O速 度提高,缩短应用程序的执行时间。另一方面,程 序的虚拟I/O操作时间和实际I/O操作时间分离开来 。 实现对独享设备的共享:由SPOOLing程序提供虚 拟设备,可以对独享设备依次共享使用。 举例:打印机设备和可由打印机管理器管理的打印作 业队列。 如:Windows NT中,应用程序直接向针式打印机 输出需要15分钟,而向打印作业队列输出只需要1 分钟,此后用户可以关闭应用程序而转入其他工作 ,在以后适当的时候由打印机管理器完成15分钟的 打印输出而无需用户干预。 Operating System Concepts 13.42 Silberschatz, Galvin and Gagne 2002 13.4.5 Error Handling 错误处理 OS can recover from disk read, device unavailable, transient write failures 操作系统可以从有些磁盘读、设备无效等暂时的(transient)失败中 恢复过来(例如通过read retry, resend等)。 有些则是不可恢复的(permanent),如硬盘控制器坏。 Most return an error number or code when I/O request fails I/O系统调用时,如果I/O失败,大多数返回一个错误码 Unix 中,用errno来描述 或者用更为详细信息描述出错信息。例如 在SCSI中,用下列信息描述 各种状态: Sense key Additional sense code Additional sense-code qualifier System error logs hold problem reports 系统日志记录了出错报告 Operating System Concepts 13.43 Silberschatz, Galvin and Gagne 2002 13.4.6 Kernel Data Structures 核心数据结构 Kernel keeps state info for I/O components, including open file tables, network connections, character device state 核心保存着I/O组件的状态,包 括打开文件表,网络连接,字符设备状态 Many, many complex data structures to track buffers, memory allocation, “dirty” blocks 许多的数据结构是为了记录缓冲、内存分配和“脏”块 Some use object-oriented methods and message passing to implement I/O 有些OS用面向对象的方法和消息机制来实现I/O Operating System Concepts 13.44 Silberschatz, Galvin and Gagne 2002 Fig 13.9 UNIX I/O Kernel Structure Operating System Concepts 13.45 Silberschatz, Galvin and Gagne 2002 In summary, I/O subsystem supervises: The management of the name space for files and devices Access control to files and devices Operation control (for example, a modem can’t seek()) File system space allocation Device allocation Buffering, caching and spooling I/O scheduling Device status monitoring, error handling and failure recovery Device driver configuration and initialization Operating System Concepts 13.46 Silberschatz, Galvin and Gagne 2002 13.5 Transforming I/O to Hardware Operations 转换I/O请求为硬件操作 I/O Requests to Hardware Operations I/O请求到硬件操作 Consider reading a file from disk for a process: 考虑一个进程从磁盘中读取一个文件 Determine device holding file 确定保存文件的设备 Translate name to device representation 将设备名转换到具体的设备 Physically read data from disk into buffer 把数据从磁盘读到缓冲区中 Make data available to requesting process 通知进程数据现在是有效的 Return control to process 把控制权返回给进程 Operating System Concepts 13.47 Silberschatz, Galvin and Gagne 2002 Fig 13.10 Life Cycle of An I/O Request I/O请求的周期 Operating System Concepts 13.48 Silberschatz, Galvin and Gagne 2002 13.6 STREAMS 流 STREAM – a full-duplex communication channel between a user- level process and a device Stream (char, no record file) block, record file in file system A STREAM consists of: - STREAM head interfaces with the user process - driver end interfaces with the device - zero or more STREAM modules between them. Stream modules Support flow control Each module contains a read queue and a write queue Read(), getmsg() -- when a user process reads data from the streams Write(), putmsg() -- when a user process writes to a device Message passing is used to communicate between queues Benefits of using STREAMS: Provide a framework to a modular and incremental approach to writing device drivers and network protocols Operating System Concepts 13.49 Silberschatz, Galvin and Gagne 2002 Fig 13.11 The STREAMS Structure Operating System Concepts 13.50 Silberschatz, Galvin and Gagne 2002 13.7 Performance 性能 I/O is a major factor in system performance: I/O是系统性能的主要因素 Demands CPU to execute device driver, kernel I/O code 需要CPU执行设备驱动,核心I/O代码 Context switches due to interrupts 因为中断而导致的上下文转换 Data copying 数据拷贝 Network traffic especially stressful 网络状况尤其重要 Operating System Concepts 13.51 Silberschatz, Galvin and Gagne 2002 Fig 13.12 Intercomputer Communications Operating System Concepts 13.52 Silberschatz, Galvin and Gagne 2002 Other system use separate front-end processors for terminal I/O, to reduce the interrupt burden on the main CPU, for example: A terminal concentrator An I/O channel Operating System Concepts 13.53 Silberschatz, Galvin and Gagne 2002 Several principles to improve performance 改进性能几个原则 Reduce number of context switches 减少上下文转换数 Reduce data copying 减少数据的拷贝量 Reduce interrupts by using large transfers, smart controllers, polling 通过使用一次大量数据传输,智能控 制器和轮换来减少中断 Use DMA 使用DMA Balance CPU, memory, bus, and I/O performance for highest throughput 平衡CPU,内存,总线和I/O性能以 达到高吞吐率 Operating System Concepts 13.54 Silberschatz, Galvin and Gagne 2002 Fig 13.13 Device-Functionality Progression Operating System Concepts 13.55 Silberschatz, Galvin and Gagne 2002