
41fc56832461c1ffdce3fae3d30c5093.ppt
- Количество слайдов: 58
Chapter 5 Input/Output
Introduction • I/O: main function of OS – Drive devices, catch interrupts, handle errors – Provide a device independent interface between devices and rest of the computer systems • To understand I/O – Principles of I/O hardware – Principles of I/O software – Layer structure of I/O software – Some specific I/O devices as examples 2
Outline • • Principles of I/O hardware Principles of I/O software layers Disks 3
Block and Character Devices • Block devices: store info in fixed-size blocks – Examples: disks – Each block has its own address – Each block can be read/written independently • Character devices: no block structure, deliver/accept a stream of characters – Examples: printers, network, mice – Not addressable, no seek operation • Devices not in the classification: clock 4
Huge Range in Speeds • Many orders of magnitude in data rates – Keyboard: 10 bytes/sec – Mouse: 100 bytes/sec – Laser printer: 100 kb/sec – IDE disk: 5 mb/sec – PCI bus: 528 mb/sec • Challenge: how to design a general structure to control various I/O devices? 5
Structure of I/O Units • A mechanical component: the device itself • An electronic component: device controller, adaptor – Printed circuit card inserted into expansion slot – Connector: a cable to device – Interface between controller and device • ANSI, IEEE, ISO, e. g. IDE and SCSI interface • Very low level – Convert a serial bit stream into a block of bytes and perform necessary error correction 6
Mechanical / Electronic Components Mechanical components Monitor CPU Memory Keyboard Video controller Keyboard controller Floppy disk driver Hard disk driver Floppy disk controller Hard disk controller Electronic components Bus 7
Memory in I/O Controllers • Some registers for communication with CPU – CPU writes commands into registers – CPU reads states of devices from registers • Data buffer for transferring data • How CPU communicates with control registers/device data buffers? 8
I/O Port • Each control register is assigned an I/O port number (8 -/16 -bit integer) • Instruction IN and OUT – IN REG, PORT – OUT PORT, REG Memory 0 x. FFFF… • Used in early computers, mainframes • Separated address spaces I/O ports 0 9
Memory-Mapped I/O • Map all control registers into memory space – Usually at the top of the address space • Hybrid scheme Memory 0 x. FFFF… I/O ports 0 0 10
Pros & Cons of Memory-Mapped I/O • Advantages – Hide details of I/O for programming – No special protection mechanism is needed – Access control registers directly, save time • Disadvantages – Caching a control register is disastrous – Memory modules and I/O devices must examine all memory references 11
Direct Memory Access (DMA) • Disk reads without DMA – Controller reads the block from drive – Interrupt, OS read controller’s buffer memory • DMA approach Drive 1. CPU DMA programs the controller DMA and controller Disk controller Buffer Main memory Address CPU Count Control 4. Ack Interrupt when done 3. Data transferred 2. DMA requires transfer to memory Bus 12
DMA Controller Memory Transfer • Use bus to transfer • Cycle stealing – Send a word at at time – Don’t block CPU for bus control • Burst mode – Acquire bus, transfer in blocks – More efficient – Block CPU from using bus 13
Interrupts Revisited 3. CPU acks interrupt CPU 2. Controller issues interrupt Interrupt controller 1. Device is finished Disk Keyboard Clock Printer Bus 14
Interrupt Processing • I/O devices raise interrupt by asserting a signal on a bus line assigned • Multiple interrupts the one with high priority goes first • Interrupt controller interrupts CPU – Put device # on address lines • Device # check interrupt vector table for interrupt handler (a program) – Enable interrupts shortly after the handler starts 15
Outline • • Principles of I/O hardware Principles of I/O software layers Disks 16
Goals of The I/O Software • • Device independence Uniform naming Error handling Synchronous (blocking) vs. asynchronous (interrupt-driven) transfers – Synchronous transfers: easy to program – Asynchronous transfers: good for CPU • Buffering • Sharable vs. dedicated devices 17
How to Perform I/O? • Programmed I/O – Have the CPU do all the work • Interrupt-driven I/O • I/O using DMA User space Kernel space String to be printed: ABC User space Kernel space ABC A 18
Polling/Busy Waiting • Simple • Waste a lot of CPU time copy_from_user(buffer, p, count); for (i=0; i
Interrupt-Driven I/O Print system call copy_from_user(buffer, p, count); enable_interrupts(); while (*printer_status_reg!=READY); *printer_data_register=p[0]; scheduler(); Interrupt service procedure Interrupt occurs on every character! if (count==0){ unblock_user(); } else { *printer_data_register=p[I]; count--; i++; } acknowledge_interrupt(); return_from_interrupt(); 20
I/O Using DMA • Too many interrupts in interrupt-driven I/O • DMA reduces # of interrupts from 1/char to 1/buffer printed Print system call copy_from_user(buffer, p, count); set_up_DMA_controller(); scheduler(); Interrupt service procedure acknowledge_interrupt(); unblock_user(); return_from_interrupt(); 21
Outline • • Principles of I/O hardware Principles of I/O software layers Disks 22
An Overview User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware 23
Interrupt Handlers • Hide IO interrupts deep in OS – Driver starts I/O and blocks – Interrupt wakes up driver – See details in text book • Process switching and interrupt • Interrupt processing takes considerable number of CPU instructions User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware 24
Device Drivers • Device-specific code for controlling I/O devices – Written by manufacture, delivered along with device – One driver for one (class) device(s) • Position: below the rest of OS • Interfaces for rest of OS – Block device and character device have different interfaces User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware 25
Logical Position of Device Drivers User space User program Rest of the OS Kernel space Printer driver Hardware Printer controller Devices printer 26
How to Install a Driver? • UNIX systems – Often run by computer centers, devices rarely change – Drivers and OS are in a single binary program • Windows – Devices often change, users don’t know how to compile OS – Dynamically load drivers into OS during execution 27
Functions of Device Drivers • • Accept abstract read/write requests Initialize device, if necessary Manage power requirements and log events Etc. 28
Structure of Device Drivers • Check input parameters – If invalid, return error • Translate form abstract to concrete terms – For disk driver, convert a linear block number into head, track, sector, and cylinder numbers • Check if the device is currently in use – If so, queue the request – If not, prepare device for the request • Issue a sequence of command to execute the request – May block and wait for interrupt • Return data and status information 29
Device-Independent I/O Software • Why dev-independent I/O software? – Perform I/O functions common to all devices – Provide a uniform interface to user-level software User-level I/O software Device-independent I/O software Device drivers • Functions in deviceindependent software – Uniform interfacing for dev. drivers – Buffering – Error reporting – Allocating and releasing dedicated devices – Providing a deviceindependent block size Interrupt handlers Hardware 30
Uniform Interfacing for Device Drivers • Diverse device drivers modify OS for new device, tedious work! • Mapping symbolic device names onto proper driver • Protection of devices from illegal access 31
Buffering for Input • Unbuffered input – User process is started up for every character – Many short runs in a process inefficient! • Buffering in user space – More efficient than unbuffered input – Can the buffer be paged out? • Yes how to handle the next character? • No the pool of available pages shrink User space Kernel space unbuffered Buffering in user space 32
Buffering in Kernel • Buffering in kernel – Buffer is full copy the buffer to user space – More efficient – What happens for characters during page swapping? • Double buffering in kernel – Buffers are used in turn User space Kernel space Buffering in kernel Double buffering 33
Buffering for Output • Buffering in user space is not good – Block user process, or – Unblocked call, don’t know when the buffer can be reused • Buffering in kernel – User can reuse the buffer in user space immediately User space Kernel space Buffering in user space Buffering in kernel 34
If Data Buffered Too Many Times • Many sequential buffering slow down transmission User process User space Kernel space 1 5 2 4 Network controller 3 Network 35
Classes of I/O Errors • Programming errors: ask for mission impossible – E. g. writing a keyboard, reading a printer – Invalid parameters, like buffer address – Report an error code to caller • Actual I/O error – E. g. write a damaged disk block – Up to the driver and software, e. g. , A dialog box • Serious error: display message, system terminates – E. g. root directory is destroyed 36
Allocating/Releasing Dedicated Devices • Blocking calls – Block the process if the device is unavailable • Non-blocking calls – Operation “open” – If the device is unavailable, return “fail” 37
User-Space I/O Software • Libraries of I/O software linked with user programs – Make system calls, e. g. write(fd, buffer, nbytes); – Format input/output, e. g. printf/scanf • Spooling – User-level, controlled by a daemon – Eliminated unnecessarily waiting/deadlock User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware 38
Summary I/O request I/O reply User-level I/O software Make I/O call; format I/O; spooling Device-independent OS software Naming, protection, blocking, buffering, allocation Device drivers Setup device registers; check status Interrupt handlers Wake up driver when I/O completed Hardware Perform I/O operation 39
Outline • • Principles of I/O hardware Principles of I/O software layers Disks 40
Kinds of Disks • Magnetic disks – Hard disks and floppy disks – Reads/writes are equally fast – Ideal secondary memory – Highly reliable storage • Optical disks – CD-ROM, CD-Recordable, DVD 41
Structure of Magnetic Disks • Cylinders tracks sectors • IDE disks – The drive contains a microcontroller – Real controller issues higher-level commands • Overlapped seeks – Seeks on two or more drives simultaneously • Virtual geometry Sector – x cylinders, y heads, z sectors/track • Logical block addressing – Continuous numbering sectors Track 42
RAID • Redundant array of independent disks • Use six specific disk organization to improve disk performance and reliability • A disk box connected to computer • RAID controller • Appear like a single large disk • Data distributed over drives – Allow parallel operations – Several different schemes: level 0 - 5 43
RAID Level 0 • Virtual single disk is divided into n strips – Each strip has k sectors – Total disk space: n * k sectors • Striping: allocate consecutive strips over drives in round-robin fashion – Read 4 consecutive strips parallel I/O Sector 0 -(k-1) Sector k-(2 k-1) Sector 2 k-(3 k-1) 3 k-(4 k-1) Strip 0 Strip 1 Strip 2 Strip 3 Strip 4 Strip 5 Strip 6 Strip 7 44
Pros and Cons of RAID Level 0 • Good for large requests – Parallel I/O – The bigger the better • Straightforward implementation • Not good for one sector at a time – No parallelism no performance gain • Poorer reliability than SLED – 1/n mean time to failure for n disks • No redundancy – not a true RAID 45
RAID Level 1 • Duplicate all disks – Primary disks and backup disks – Write every strip is written twice – Read either copy can be used Primary disks backup disks Strip 0 Strip 1 Strip 2 Strip 3 Strip 4 Strip 5 Strip 6 Strip 7 46
Pros and Cons of RAID Level 1 • Write performance is poorer than SLED • Read performance is up to twice as good as SLED • Excellent fault tolerance – A drive fails use its copy – Recovery: install a new drive, copy the whole backup drive 47
RAID Level 3 • Work on a word/byte basis • Split each byte into a pair of 4 -bit nibbles • Add a Hamming code to each one to form a 7 -bit word • 7 drives are synchronized in terms of arm position and rotational position – Write the 7 -bit Hamming coded word over 7 drives, 1 bit/drive Word 1 Word 2 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 48
Pros and Cons of RAID Level 2 • Advantages – Immense throughput • Quadruple I/O capability if 7 drives are used • 32 times speedup if 39 drives are used – One I/O request at a time – Highly fault tolerant • Using Hamming code to correct faults on the fly • Disadvantages – Require all drives rotationally synchronized – Use substantial number of drives – Do Hamming checksum every bit time 49
RAID Level 3 • • Simplified version of RAID level 2 A single parity bit for each data word Drives must be exactly synchronized Fault correction – Parity bit + crashed drive id • High data rate • One I/O request at a time Bit 1 Bit 2 Bit 3 Bit 4 Parity 50
RAID Level 4 • Use strips • Strip-for-strip parity onto an extra drive – Each strip is k bytes long – Exclusive OR all strips a parity strip k bytes long Strip 0 Strip 1 Strip 2 Strip 3 P 0 -3 Strip 4 Strip 5 Strip 6 Strip 7 P 4 -7 51
Pros and Cons of RAID Level 4 • No synchronized drives • Protect against the loss of one drive • Poor performance for small updates – One sector is changed read all the drives to recalculate and rewrite the parity – Read the old user data and old parity data to recompute the new parity – Small update two reads and two writes 52
RAID Level 5 • Workload bottleneck: parity drive • Distributing the parity bits uniformly over all drives in round robin fashion • Reconstructing a crashed drive is complex Strip 0 Strip 1 Strip 2 Strip 3 P 0 -3 Strip 4 Strip 5 Strip 6 P 4 -7 Strip 7 P 8 -11 P 12 -15 P 16 -19 53
Warm-up • Structure of I/O units – Mechanical/electronic components, memory, I/O ports • How to perform I/O? – Programmed I/O, interrupt-driven I/O, DMA • Layers of I/O software • Disks – RAID disk User-level I/O software Device-independent I/O software Device drivers Interrupt handlers Hardware 54
Cost of Read / Write A Disk Block • Seek time – The time to move the arm to the proper cylinder – Dominate the other two times for most disks • Rotational delay – The time for the proper sector to rotate under the head 2 • Actual data transfer time 1 55
Optimize Seek Time • Fist-come, first-served: little can be done • Shortest seek first – Handle the closest request next – Requests far from the middle get poor service 1 5 0 7 2 3 6 4 Total: 47 moves Total: 63 moves 56
Elevator Algorithm • Upper bound: twice of the number of cylinders 1 5 0 7 2 3 6 4 57
Summary • I/O module is an important component in OS • Important features of I/O hardware – Device controllers, memory mapped I/O, DMA, and interrupts • I/O software – Programmed I/O, interrupts, DMA • Four layers of I/O software • Disk – RAID and disk arm scheduling 58