58
Chapter 5 Input/Output

Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

Embed Size (px)

Citation preview

Page 1: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

Chapter 5Input/Output

Page 2: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

2

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

Page 3: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

3

Outline

• Principles of I/O hardware

• Principles of I/O software

• I/O software layers

• Disks

Page 4: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

4

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

Page 5: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

5

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?

Page 6: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

6

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

Page 7: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

7

Mechanical / Electronic Components

Bus

CPU MemoryVideo

controllerKeyboard controller

Floppy disk

controller

Hard disk controller

Monitor Keyboard

Floppy disk

driver

Hard disk

driver

Mechanical components

Electronic components

Page 8: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

8

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?

Page 9: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

9

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

0xFFFF…

0

Memory

I/O ports

Page 10: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

10

Memory-Mapped I/O

• Map all control registers into memory space– Usually at the top of the address space

• Hybrid scheme

0xFFFF…

0

Memory

0xFFFF…

0

Memory

I/O ports

Page 11: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

11

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

Page 12: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

12

Direct Memory Access (DMA)• Disk reads without DMA

– Controller reads the block from drive– Interrupt, OS read controller’s buffer memory

• DMA approach

CPU

DMA controller

Disk controller

Main memory

Bus

Address

Count

Control

Drive

1. CPU programs the DMA and controller Buffer

2. DMA requires transfer to memory

3. Data transferred

4. Ack

Interrupt when done

Page 13: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

13

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

Page 14: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

14

Interrupts Revisited

CPU

Bus

Interrupt controller

Disk

1. Device is finished

Keyboard

ClockPrinter2. Controller

issues interrupt

3. CPU acks interrupt

Page 15: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

15

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

Page 16: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

16

Outline

• Principles of I/O hardware

• Principles of I/O software

• I/O software layers

• Disks

Page 17: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

17

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

Page 18: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

18

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

String to be printed: ABC

Kernel space

User space

Kernel space ABC A

Page 19: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

19

Polling/Busy Waiting

• Simple

• Waste a lot of CPU time

copy_from_user(buffer, p, count);

for (i=0; i<count; i++){

while (*printer_status_reg!=READY);

*printer_data_register=p[I];

}

return_to_user();

// p is the kernel buffer

// loop on every character

// loop until ready

// output one character

Page 20: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

20

Interrupt-Driven I/OPrint system callcopy_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!

Page 21: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

21

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();

Page 22: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

22

Outline

• Principles of I/O hardware

• Principles of I/O software

• I/O software layers

• Disks

Page 23: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

23

An Overview

User-level I/O software

Device-independent I/O software

Device drivers

Interrupt handlers

Hardware

Page 24: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

24

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 instructionsUser-level I/O software

Device-independent I/O software

Device drivers

Interrupt handlers

Hardware

Page 25: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

25

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

Page 26: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

26

Logical Position of Device Drivers

User program

Rest of the OS

Printer driver

Printer controller

printer

User space

Kernel space

Hardware

Devices

Page 27: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

27

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

Page 28: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

28

Functions of Device Drivers

• Accept abstract read/write requests

• Initialize device, if necessary

• Manage power requirements and log events

• Etc.

Page 29: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

29

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

Page 30: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

30

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

• Functions in device-independent software– Uniform interfacing for

dev. drivers– Buffering– Error reporting– Allocating and releasing

dedicated devices– Providing a device-

independent block size

User-level I/O software

Device-independent I/O software

Device drivers

Interrupt handlers

Hardware

Page 31: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

31

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

Page 32: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

32

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

User space

Kernel space

Buffering in user space

Page 33: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

33

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

User space

Kernel space

Double buffering

Page 34: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

34

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

User space

Kernel space

Buffering in kernel

Page 35: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

35

If Data Buffered Too Many Times

• Many sequential buffering slow down transmission

User process

1

2Network controller

Network

4

5

3

User space

Kernel space

Page 36: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

36

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

Page 37: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

37

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”

Page 38: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

38

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

Page 39: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

39

Summary

User-level I/O software Make I/O call; format I/O; spooling

Device-independent OS software

Naming, protection, blocking, buffering, allocation

Device driversSetup device registers; check

status

Interrupt handlersWake up driver when I/O

completed

Hardware Perform I/O operation

I/O request I/O reply

Page 40: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

40

Outline

• Principles of I/O hardware

• Principles of I/O software

• I/O software layers

• Disks

Page 41: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

41

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

Page 42: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

42

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 Track

Sector

Page 43: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

43

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

Page 44: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

44

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

Strip 4

Strip 0

Strip 5

Strip 1

Strip 6

Strip 2

Strip 7

Strip 3

Sector0-(k-1)

Sectork-(2k-1)

Sector2k-(3k-1)

Sector3k-(4k-1)

Page 45: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

45

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

Page 46: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

46

RAID Level 1

• Duplicate all disks– Primary disks and backup disks– Write every strip is written twice– Read either copy can be used

Strip 4

Strip 0

Strip 5

Strip 1

Strip 6

Strip 2

Strip 7

Strip 3

Strip 4

Strip 0

Strip 5

Strip 1

Strip 6

Strip 2

Strip 7

Strip 3

Primary disks backup disks

Page 47: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

47

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

Page 48: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

48

Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7

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

Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7Word 1Word 2

Page 49: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

49

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

Page 50: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

50

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

Bit 1 Bit 2 Bit 3 Bit 4 Parity

Page 51: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

51

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 4

Strip 0

Strip 5

Strip 1

Strip 6

Strip 2

Strip 7

Strip 3

P4-7

P0-3

Page 52: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

52

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

Page 53: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

53

P8-11

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 4

Strip 0

Strip 5

Strip 1

Strip 6

Strip 2

P4-7

Strip 3

Strip 7

P0-3

P12-15

P16-19

Page 54: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

54

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

Page 55: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

55

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

• Actual data transfer time1

2

Page 56: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

56

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: 63 movesTotal: 47 moves

Page 57: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

57

Elevator Algorithm

• Upper bound: twice of the number of cylinders

1 5 0 7 2 3 6 4

Page 58: Chapter 5 Input/Output. 2 Introduction I/O: main function of OS –Drive devices, catch interrupts, handle errors –Provide a device independent interface

58

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