550 likes | 676 Vues
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 请求为硬件操作
E N D
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 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
外设的特点: • 种类多 • 差异大(控制和速度) • 外设管理目的包括: • 外设资源的控制 • 外设资源的共享 • 提高外设资源的利用率。 Operating System Concepts
外部设备类型和特征 1. 按交互对象分类 • 人机交互设备:视频显示设备、键盘、鼠标、打印机 • 与计算机或其他电子设备交互的设备:磁盘、磁带、传感器、控制器 • 计算机间的通信设备:网卡、调制解调器 Operating System Concepts
2. 按交互方向分类 • 输入(可读):键盘、扫描仪 • 输出(可写):显示设备、打印机 • 输入/输出(可读写):磁盘、网卡 3. 按外设特性分类 • 使用特征:存储、输入/输出、终端 • 数据传输率:低速(如键盘)、中速(如打印机)、高速(如网卡、磁盘) • 信息组织特征:单个字符或数据块 • 字符设备(如打印机) • 块设备(如磁盘) Operating System Concepts
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
Fig 13.1 A Typical PC Bus Structure Operating System Concepts
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
Fig 13.2 Device I/O Port Locations on PCs (partial) Operating System Concepts
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
3. 直接存储访问方式(DMA, Direct Memory Access) • 由程序设置DMA控制器中的若干寄存器值(如内存始址,传送字节数),然后发起I/O操作。而后者完成内存与外设的成批数据交换,在操作完成时由DMA控制器向CPU发出中断。 • 优点:CPU只需干预I/O操作的开始和结束,而其中的一批数据读写无需CPU控制,适于高速设备。 Operating System Concepts
4. 通道控制方式(channel control) • 通道控制器(Channel Processor)有自己的专用存储器,可以执行由通道指令组成的通道程序,因此可以进行较为复杂的I/O控制,如网卡上信道访问控制。通道程序通常由操作系统所构造,放在内存里。 • 优点:执行一个通道程序可以完成几批I/O操作。 • 选择通道(selector channel):可以连接多个外设,而一次只能访问其中一个外设 • 多路通道(multiplexor channel):可以并发访问多个外设。分为字节多路(byte)和数组多路(block)通道。 Operating System Concepts
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.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
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
Fig 13.3 Interrupt-Driven I/O Cycle中断驱动的I/O循环 Operating System Concepts
Fig 13.4 Intel Pentium Processor Event-Vector Table Operating System Concepts
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
Fig 13.6 Six Step Process to Perform DMA Transfer通过六步来完成DMA传输 Operating System Concepts
直接存储访问方式(DMA, Direct Memory Access) • 由程序设置DMA控制器中的若干寄存器值(如内存始址,传送字节数),然后发起I/O操作。而后者完成内存与外设的成批数据交换,在操作完成时由DMA控制器向CPU发出中断。 • 优点:CPU只需干预I/O操作的开始和结束,而其中的一批数据读写无需CPU控制,适用于高速设备。 Operating System Concepts
13.2.4 通道控制方式(channel control) • 通道控制器(Channel Processor)有自己的专用存储器,可以执行由通道指令组成的通道程序,因此可以进行较为复杂的I/O控制,如网卡上信道访问控制。通道程序通常由操作系统所构造,放在内存里。 • 优点:执行一个通道程序可以完成几批I/O操作。 • 选择通道(selector channel):可以连接多个外设,而一次只能访问其中一个外设 • 多路通道(multiplexor channel):可以并发访问多个外设。分为字节多路(byte)和数组多路(block)通道。 Operating System Concepts
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
Fig 13.6 A Kernel I/O Structure Operating System Concepts
Fig 13.7 Characteristics of I/O Devices Operating System Concepts
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.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.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.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.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.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 per-device queue to improve the overall system efficiency。为改善系统性能,某些I/O需要按设备队列的进行排序 • Some OSs try fairness 某些操作系统尝试着公平 Operating System Concepts
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
Fig 13.8 Sun Enterprise 6000 Device-Transfer Rates Operating System Concepts
缓冲技术 缓冲技术可提高外设利用率,尽可能使外设处于忙状态;但有一个限制:进程的I/O请求不能超过外设的处理能力。 1. 引入缓冲技术的目的 • 匹配CPU或用户应用进程与外设的不同处理速度 • 减少对CPU的中断次数,提高CPU和I/O设备之间以及各个I/O设备之间的处理并行性。因此,缓冲区所在的位置:内存,控制器或外设。这些在不同位置的缓冲区组合在一起,构成多级缓冲机制。 Operating System Concepts
2. 单方向缓冲 • 单缓冲(single buffer):一个缓冲区,CPU和外设轮流使用,一方处理完之后接着等待对方处理。 • 双缓冲(double buffer):两个缓冲区,CPU和外设都可以连续处理而无需等待对方。要求CPU和外设的速度相近。 • 环形缓冲(circular buffer):多个缓冲区,CPU和外设的处理速度可以相差较大。 Operating System Concepts
3. 缓冲池(buffer pool) • 这是一种双方向缓冲技术;缓冲区整体利用率高。 • 缓冲区队列:三种:空闲缓冲区,输入缓冲区,输出缓冲区 • 操作:四种:设备输入,CPU读入,设备输出,CPU写出。上述操作访问各个缓冲区队列时,需要进行相应的互斥操作。 Operating System Concepts
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.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
假脱机技术 • 利用假脱机技术(SPOOLing, Simultaneous Peripheral Operation On Line, 也称为虚拟设备技术)可把独享设备转变成具有共享特征的虚拟设备,从而提高设备利用率。 • 引入:在多道批处理系统中,专门利用一道程序(SPOOLing程序)来完成对设备的I/O操作。无需使用外围I/O处理机。 Operating System Concepts
假脱机技术 • 假脱机的原理: • SPOOLing程序和外设进行数据交换,可以称为“实际I/O”。一方面,SPOOLing程序预先从外设输入数据并加以缓冲,在以后需要的时候输入到应用程序;另一方面,SPOOLing程序接受应用程序的输出数据并加以缓冲,在以后适当的时候输出到外设。 • 在SPOOLing程序中,需要管理两级缓冲区:内存缓冲区和快速外存上的缓冲池,后者可以暂存多批I/O操作的较多数据。 • 应用程序进行I/O操作时,只是和SPOOLing程序交换数据,可以称为"虚拟I/O"。这时虚拟I/O实际上是从SPOOLing程序的缓冲池中读出数据或把数据送入缓冲池,而不是跟实际的外设进行I/O操作。 Operating System Concepts
假脱机技术 • 优点: • 高速虚拟I/O操作:应用程序的虚拟I/O比实际I/O速度提高,缩短应用程序的执行时间。另一方面,程序的虚拟I/O操作时间和实际I/O操作时间分离开来。 • 实现对独享设备的共享:由SPOOLing程序提供虚拟设备,可以对独享设备依次共享使用。 • 举例:打印机设备和可由打印机管理器管理的打印作业队列。 • 如:Windows NT中,应用程序直接向针式打印机输出需要15分钟,而向打印作业队列输出只需要1分钟,此后用户可以关闭应用程序而转入其他工作,在以后适当的时候由打印机管理器完成15分钟的打印输出而无需用户干预。 Operating System Concepts
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.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
Fig 13.9 UNIX I/O Kernel Structure Operating System Concepts
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.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
Fig 13.10 Life Cycle of An I/O Request I/O请求的周期 Operating System Concepts
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
Fig 13.11 The STREAMS Structure Operating System Concepts