I/O System
Overview
计算机的两个主要工作:
- I/O
- processing
与计算机相连的设备的控制是操作系统设计者关心的一个主要问题。
- I/O设备技术出现两个相矛盾的趋势:
- 硬件和软件接口日益增长的标准化。
- I/O设备日益增长的多样性。
- 操作系统内核设计成使用设备驱动程序模块的结构。
- 设备驱动程序为I/O子系统提供了统一接口。Device drivers present a uniform device-access interface to the I/O subsystem, much as system calls provide a standard interface between the application and the operating system.
I/O Hardware 硬件组成
I/O系统的组成:
- PC BUS I/O系统
- 主机I/O系统
- 以存储器为中心,CPU和各种都与存储器相连。
Common concepts 共同概念
- Port ,端口
- Bus (daisy chain or shared direct access),总线
- Controller (host adapter),控制器
I/O instructions control devices IO指令控制装置
Devices have addresses(寻址方式), used by
- Direct I/O instructions 直接执行IO
- Memory-mapped I/O 内存映射IO
I/O方式
轮询 Polling
确定设备状态
- 命令就绪 command-ready
- 忙碌 busy
- 错误 error
Busy-wait cycle to wait for I/O from device 忙等待周期
中断 Interrupts
Application I/O Interface
Block and Character Devices(块和字符设备)
Block devices(块设备) include disk drives
- Commands include read, write, seek
- Raw I/O or file-system access
- Memory-mapped file access possible
Character devices (字符设备) include keyboards, mice, serial ports
- Commands include get(), put()
- Libraries layered on top allow line editing
Network Devices(网络设备)
- Varying enough from block and character to have own interface
- Unix and Windows NT/9x/2000 include socket interface
- Separates network protocol from network operation
- Includes select() functionality
- Approaches vary widely (pipes, FIFOs, streams, queues,mailboxes)
Clocks and Timers(时钟和定时器)
- 提供以下三个基本函数
- 获取当前时间
- 获取已经逝去的时间
- 设置定时器以在T时触发操作X
- 测量逝去时间和触发器操作的硬件称为可编程间隔定时器(programmable interval timer)
- 可被设置为等待一定的时间,然后触发中断
- 也可设置成做一次或重复多次以产生定时中断
Blocking and Nonblocking I/O(阻塞和非阻塞I/O)
Blocking 阻塞 – process suspended until I/O completed ,进程挂起直到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)
- Implemented via multi-threading
- Returns quickly with count of bytes read or written.
OAsynchronous(异步)
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子系统信号处理
非阻塞与异步系统调用的差别是:
- 非阻塞read调用会马上返回,其所读取的数据可以等于或少于所要求的,或为零;
- 异步read调用所要求的传输应完整地执行,其具体执行可以是将来某个特定时间。
Kernel I/O Subsystem
I/O调度
Scheduling(I/O调度)调度一组I/O请求就是确定一个好的顺序来执行这些请求。
- 某些I/O需要按设备队列的顺序–先来先服务
- 某些操作系统尝试着公平–优先级高者优先
- 磁盘I/O调度
实现
- OS通过为每个设备维护一个请求队列来实现调度。
- 可以试图公平,也可以根据不同的优先级进行I/O调度。
- 其他方法:缓冲、高速缓冲、假脱机
Buffer 缓冲
缓冲 Buffering—-store data in memory while transferring between devices,用来保存在两设备之间或在设备和应用程序之间所传输数据的内存区域。。
缓冲作用:
- 解决设备速度不匹配
- 解决设备传输块的大小不匹配
- 为了维持拷贝语义“copy semantics”要求
缓冲区管理:为了解决CPU与I/O之间速度不匹配的矛盾,在它们之间配置了缓冲区。这样设备管理程序又要负责管理缓冲区的建立、分配和释放。
单缓冲、双缓冲、多缓冲、缓冲池
Caching 高速缓存
Caching (高速缓存)- fast memory holding copy of data
- 缓冲与高速缓存的差别是缓冲只是保留数据仅有的一个现存拷贝,而高速缓存只是提供了一个驻留在其他地方的数据的一个高速拷贝。
- 高速缓存和缓冲是两个不同的功能,但有时一块内存区域也可以同时用于两个目的。
- 当内核接收到I/O请求时,内核首先检查高速缓存以确定相应文件的内容是否在内存中。如果是,物理磁盘I/O就可以避免或延迟。
SPOOLing 假脱机技术
SPOOLing(Simultaneous Peripheral Operation On Line)称为假脱机技术。:用来保存设备输出的缓冲,这些设备如打印机不能接收交叉的数据流。
操作系统通过截取对打印机的输出来解决这一问题。应用程序的输出先是假脱机到一个独立的磁盘文件上。当应用程序完成打印时,假脱机系统将相应的待送打印机的假脱机文件进行排队。
Printing:打印机虽然是独享设备,通过SPOOLing技术,可以将它改造为一台可供多个用户共享的设备。
Device reservation & Error Handling 设备预定和错误处理
Device reservation(设备预定) – provides exclusive access to a device提供对设备的独占访问
- System calls for allocation and deallocation分配和再分配的系统调用
- Watch out for deadlock 有可能产生死锁
Error Handling(错误处理)
- OS can recover from disk read, device unavailable, transient write failures。操作系统可以恢复磁盘读,设备无效,暂时的失败
- Most return an error number or code when I/O request fails 当I/O失败时,大多数返回一个错误码
- System error logs hold problem reports系统日志记录了出错报告
I/O Protection
User process may accidentally or purposefully attempt to disrupt normal operation via illegal I/O instructions 用户进程可能会意外地或故意地试图通过非法的I/O指令中断正常操作
- All I/O instructions defined to be privileged 将I/O指令定为特权指令
- I/O must be performed via system calls 必须通过系统调用执行I/O
- Memory-mapped and I/O port memory locations must be protected too 内存映射和I/O端口内存位置也必须受到保护
Transforming I/O Requests to Hardware Operations
Consider reading a file from disk for a process:
- 确定保存文件的设备
- 转换名字到设备的表示法
- 把数据从磁盘读到缓冲区中
- 通知请求进程数据现在是有效的
- 把控制权返回给进程
STREAMS
STREAM – a full-duplex communication channel between a user-level process and a device in Unix System V and beyond Unix系统V及更高版本中用户级进程与设备之间的全双工通信通道
A STREAM consists of:
- STREAM head interfaces with the user process
- driver end interfaces with the device
- zero or more STREAM modules between them.
Each module contains a read queue and a write queue
Message passing is used to communicate between queues
Performance
I/O a major factor in system performance: I/O系统性能的主要因素
- Demands CPU to execute device driver, kernel I/O code
- Context switches due to interrupts 中断导致的上下文切换
- Data copying
- Network traffic especially stressful 网络流量特别紧张
0 条评论