Professional Documents
Culture Documents
Input/Output
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Overview
● OS controls I/O devices
○ Issue commands,
○ handles interrupts,
○ handles errors
Block devices
● stores information in fixed-size blocks, sectors or clusters
● Each one with its own address
● Each read or write blocks independently of one another
● Hard disks, CD-ROMs, USB sticks
Character devices
● accepts a stream of characters, without regard to any block structure.
● It is not addressable and does not have any seek operation.
● Printers, mice, network interfaces
● Direct access to the hardware device.
I/O Devices
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Device Controllers
● I/O unit consist of 2 components
○ mechanical(device)
○ electronic (device controller/adapter)
Isolated I/O:
2 separate address space used.
● Memory location
● I/O devices.
Memory Mapped I/O
● Controller has registers which OS can write and
read
● Write-gives command to device
● Read-learn device status……
● Devices have data buffer which OS can
read/write (e.g. video RAM, used to display pixels
on a screen)
● How does CPU communicate with control
registers and device data buffers?
Memory-Mapped I/O
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory mapped advantages
o Don’t need special instructions to
read/write control registers=> can write a
device driver in C
o Don’t need special protection to keep users
from doing I/O directly. Just don’t put I/O
memory in any user space
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory mapped advantages
o An instruction can reference control
registers and memory
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory mapped disadvantage
o Can cache memory words, which means
that old memory value (e.g. for port 4)
could remain in cache
o => have to be able to disable caching when
it is worthwhile
o Add extra complexity to devices
o I/O devices and memory have to respond
to memory references
o Works with single bus because both
memory and I/O look at address on bus
and decide who it is for
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory mapped disadvantage
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory-Mapped I/O
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DMA
o CPU COULD request data one byte at a
time from I/O controller
o Big waste of time, use DMA
o DMA controller on mother-board; normally
one controller for multiple devices
o CPU reads/writes to registers in controller
o Memory address register
o Byte count register
o Control registers-I/O port, direction of transfer, transfer units
(byte/word), number of bytes to transfer in a burst
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
When DMA is not used
o Controller reads a block into its memory
o Computes checksum
o Interrupts OS
o Sends byte at a time to memory
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
How does DMA work?
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DMA controller modes
o Cycle stealing mode-transfer goes on word
at a time, competing with CPU for bus
cycles. Idea is that CPU loses the
occasional cycle to the DMA controller
o Burst mode-DMA controller grabs bus and
sends a block
o Fly by mode-DMA controller tells device
controller to send word to it instead of
memory. Can be used to transfer data
between devices.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Questions
o Why buffer data in controllers?
o Can do check-sum
o Bus may be busy-need to store data someplace
o Is DMA really worth it? Not if
o CPU is much faster then DMA controller and can do the job
faster
o don’t have too much data to transfer
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
PC interrupt structure
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interrupt processing
o Controller puts number on address line
telling CPU which device wants attention
and interrupts CPU
o Table (interrupt vector) points to interrupt
service routine
o Number on address line acts as index into
interrupt vector
o Interrupt vector contains PC which points to
start of service routine
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interrupt processing
o Interrupt service routine acks interrupt
o Saves information about interrupted program
o Where to save information
o User process stack, kernel stack are both possibilities
o Both have problems
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The precise (ideal) interrupt
Leaves the machine in a well-defined state.
Four Properties
1. PC (Program Counter) is saved in a known
place.
2. All instructions before the one pointed to by
the PC have fully executed.
3. No instruction beyond the one pointed to by
the PC has been executed.
4. Execution state of the instruction pointed to
by the PC is known.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Precise and Imprecise Interrupts (2)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
How to process an imprecise
interrupt
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
I/O Software-Goals
o Device independence-don’t have to specify the
device when accessing the device
o Uniform naming-name should not depend on
device type.
o Error handling-do it as close to the device as
possible (e.g. controller should be the first to fix
error, followed by the driver)
o Synchronous and asynchronous-OS needs to
make I/O operations blocking (e.g. program
blocks until data arrives on a read) because it is
easy to write blocking ops
I/O Software-Goals
o Buffering- e.g. when a packet arrives
o Shared devices (disks) and dedicated
devices (tapes) must be handled
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
There are fundamentally 3 different ways that I/O
can perform.
● Programmed I/O
● Interrupt-driven I/O
● I/O using DMA
Programmed I/O
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Good vs Bad
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interrupt Driven I/O
o Idea: block process which requests I/O,
schedule another process
o Return to calling process when I/O is done
o Printer generates interrupt when a character is
printed
o Keeps printing until the end of the string
o Re-instantiate calling process
Disadvantage:
● Interrupt occur on every character.
● Interrupt take time which waste certain amount
of CPU time.
DMA
o Use DMA controller to send characters to
printer instead of using the CPU
o CPU is only interrupted when the buffer is
printed instead of when each character is
printed
o DMA is worth it if (1) DMA controller can drive
the device as fast as the CPU could drive it (2)
there is enough data to make it worthwhile
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
I/O Software Layers
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interrupt processing details
1. Save registers that have not already saved by
interrupt hardware.
2. Set up a context for the interrupt service
procedure. Setting up TLB, MMU and page
table.
3. Set up a stack for the interrupt service
procedure.
4. Acknowledge the interrupt controller. If there is
no centralized interrupt controller, re-enable
interrupts.
5. Copy the registers from where they were
saved to the process table.
Interrupt processing details
6 Run the interrupt service procedure.
7 Choose which process to run next. High
priority process chosen to run now.
8 Set up the MMU context for the process to run
next. Some TLB setup also be needed.
9 Load the new process’ registers, including its
PSW.
10 Start running the new process.
Device Drivers
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Device Drivers
I/O device attached to a computer needs some device-specific
code for controlling it. This code called device driver.
o Driver contains code specific to the device
o Supplied by manufacturer
o Installed in the kernel
o User space might be better place
o Why? Bad driver can mess up kernel
o Allow outside installation.
o Classify drivers most common categories like, block
devices and character devices.
o Defining standard interface that support all block drivers
and a second standard interface that support all character
drivers.
Device Driver
Has several function:
o Accept abstract read and write request from device
independent software.
o If needed, then initialize the device
o Manage power requirement and log event.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Buffering
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Buffering
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Error handling
● Many errors are device specific and must be handled by
appropriate driver.
● Framework for error handling is device independent.
● Programming Errors:
○ Process ask for something impossible.
○ Writing to a input device.
○ Providing a invalid buffer address.
○ Solution: Report back an error code to the caller.
● Actual I/O error:
○ Write a disk block that has been damaged.
○ Read from a device that has been switched off.
There is some possible solutions.
Allocating and Releasing Dedicated Devices
● Examine requests for device usage and accept or reject them.
● Perform opens on special files for devices directly.
● It the device is not available, the open fails.
● Closing such a dedicated device and release it.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Example Flow through layers
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Disks
• Magnetic (hard)
– Reads and writes are equally fast=> good for storing file
systems
– Disk arrays are used for reliable storage (RAID)
• Optical disks (CD-ROM, CD-Recordables, DVD) used
for program distribution
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Disks-more stuff
o Magnetic disk are organized into cylinder, tracks,
sectors etc.
o Some disks have microcontrollers which do bad block
re-mapping, track caching
o Some are capable of doing more then one seek at a
time, i.e. they can read on one disk while writing on
another
o Real disk geometry is different from geometry used by
driver => controller has to re-map request for (cylinder,
head,sector) onto actual disk
o Disks are divided into zones, with fewer tracks on the
inside, gradually progressing to more on the outside
o Logical block addressing.
Disk Zones
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD
• CDs are made from an original master disc.
• The master is "burned" with a infrared laser beam that
etches bumps (called pits) into its surface.
• A bump represents the number zero, so every time the
laser burns a bump into the disc, a zero is stored there.
• The lack of a bump (which is a flat, unburned area on the
disc, called a land) represents the number one.
The laser can store all the information sampled from the
original track of music by burning some areas (to represent
zeros) and leaving other areas unburned (to represent ones).
CD
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD
Once the master disc has been made, it is used to stamp out
millions of plastic duplicates.
Once each disc is pressed, it's coated with a thin aluminum layer,
covered with protective polycarbonate and lacquer, and the label
is printed on top.
CD-ROM
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD-ROMs Sector Layout
Sector
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD-Recordable
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD-Recordable
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DVD (Digital Versatile Disk)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DVD
DVD Formats
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DVD
DVD
DVD
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Redundant Array of Inexpensive
Disks (RAID)
o Parallel I/O to improve performance and reliability
o vs SLED, Single Large Expensive Disk
o Bunch of disks which appear like a single disk to the
OS
o SCSI disks often used-cheap, 7 disks per controller
o SCSI is set of standards to connect CPU to
peripherals
o Different architectures-level 0 through level 7
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
RAID Levels 0,1,2
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
RAID
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Cylinder Skew
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
High level format
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
High level format for each partition
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Power goes on
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Disk Arm Scheduling Algorithms
Read/write time factors
1. Seek time (the time to move the arm to the
proper cylinder).
2. Rotational delay (the time for the proper sector
to rotate under the head).
3. Actual data transfer time.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Disk Arm Scheduling Algorithms
o Driver keeps list of requests (cylinder number,
time of request) in a table.
o Contain pending request for each cylinder.
o Try to optimize the seek time
o FCFS is easy to implement, but optimizes
nothing
oTanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Disk Arm Scheduling Algorithms
o First Come First Serve.
o Shortest Seek Time First.
o Elevator Algorithm.
o C-Scan Scheduling Algorithm.
o C-Look Scheduling Algorithm.
SSF (Shortest Seek Time First)
Cylinders are serviced is 12, 16, 34, 36, 9 and 1 which yields arm
motions of 1, 4, 18, 2 27 and 8. Uses 60 cylinders, a bit better
Example Problem
Total Move:
Step 1: 53-23 = 30
Step 2: 89-23 = 66
Step 3: 132-89 = 43
Step 4: 174-132= 42
Step 5: 174-42=132
Step 6: 187-42=145
Total move = ?
C-Scan Scheduling Algorithm
Total move = ?
C-Look Scheduling Algorithm
Total move = ?
Disk Controller Cache
o Disk controllers have their own cache
o Cache is separate from the OS cache
o OS caches blocks independently of where they
are located on the disk
o Controller caches blocks which were easy to
read but which were not necessarily requested
oTanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Bad Sectors-the controller approach
o Manufacturing defect-that which was written
does not correspond to that which is read
(back)
o Controller or OS deals with bad sectors
o If controller deals with them the factory
provides a list of bad blocks and controller
remaps good spares in place of bad blocks
o Substitution can be done when the disk is in
use-controller “notices” that block is bad and
substitutes
oTanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Error Handling