580 likes | 598 Views
This chapter introduces the main function of the operating system, which is to provide a device independent interface between devices and the rest of the computer system. It covers the principles of I/O hardware and software, the layer structure of I/O software, and specific I/O devices as examples. The chapter also discusses the structure of I/O units, the role of mechanical and electronic components, memory in I/O controllers, I/O ports, memory-mapped I/O, and direct memory access (DMA). It concludes with an overview of interrupts, I/O software goals, and different methods of performing I/O.
E N D
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
Outline • Principles of I/O hardware • Principles of I/O software • I/O software layers • Disks
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
Huge Range in Speeds • Many orders of magnitude in data rates • Keyboard: 10 bytes/sec • Mouse: 100 bytes/sec • Laser printer: 100kb/sec • IDE disk: 5mb/sec • PCI bus: 528mb/sec • Challenge: how to design a general structure to control various I/O devices?
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
Mechanical / Electronic Components Mechanical components Floppy disk driver Hard disk driver Monitor Keyboard Floppy disk controller Video controller Keyboard controller Hard disk controller CPU Memory Electronic components Bus
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?
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 • Used in early computers, mainframes • Separated address spaces Memory I/O ports
Memory-Mapped I/O • Map all control registers into memory space • Usually at the top of the address space • Hybrid scheme Memory Memory I/O ports
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
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 programs the DMA and controller Disk controller DMA controller Main memory Buffer Address CPU Count 4. Ack Control Interrupt when done 3. Data transferred 2. DMA requires transfer to memory Bus
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
Interrupts Revisited 1. Device is finished Interrupt controller 3. CPU acks interrupt Disk Keyboard CPU Clock 2. Controller issues interrupt Printer Bus
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
Outline • Principles of I/O hardware • Principles of I/O software • I/O software layers • Disks
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
How to Perform I/O? • Programmed I/O • Have the CPU do all the work • Interrupt-driven I/O • I/O using DMA A
Polling/Busy Waiting • Simple • Waste a lot of CPU time
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 if (count==0){ unblock_user(); } else { *printer_data_register=p[I]; count--; i++; } acknowledge_interrupt(); return_from_interrupt(); Interrupt occurs on every character!
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();
Outline • Principles of I/O hardware • Principles of I/O software • I/O software layers • Disks
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
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
Logical Position of Device Drivers User program User space Rest of the OS Kernel space Printer driver Hardware Printer controller printer Devices
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
Functions of Device Drivers • Accept abstract read/write requests • Initialize device, if necessary • Manage power requirements and log events • Etc.
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
Why dev-independent I/O software? Perform I/O functions common to all devices Provide a uniform interface to user-level software Functions in device-independent software Uniform interfacing for dev. drivers Buffering Error reporting Allocating and releasing dedicated devices Providing a device-independent block size Device-Independent I/O Software
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
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 unbuffered Buffering in user space
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 Buffering in kernel Double buffering
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 Buffering in user space Buffering in kernel
If Data Buffered Too Many Times • Many sequential buffering slow down transmission User process User space 5 1 Kernel space 2 4 Network controller 3 Network
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
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”
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
Summary I/O request I/O reply
Outline • Principles of I/O hardware • Principles of I/O software • I/O software layers • Disks
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
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 • x cylinders, y heads, z sectors/track • Logical block addressing • Continuous numbering sectors Sector Track
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
Strip 0 Strip 1 Strip 2 Strip 3 Strip 4 Strip 5 Strip 6 Strip 7 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-(2k-1) Sector 2k-(3k-1) Sector 3k-(4k-1)
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
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 0 Strip 1 Strip 2 Strip 3 Strip 4 Strip 5 Strip 6 Strip 7 Strip 4 Strip 5 Strip 6 Strip 7
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
Bit 4 Bit 5 Bit 6 Bit 7 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 Bit 1 Bit 2 Bit 3 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
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
Bit 4 Parity Bit 1 Bit 2 Bit 3 Bit 4 Parity Bit 1 Bit 2 Bit 3 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