You are on page 1of 111

Chapter 5

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

● Provide easy to use interface to devices


○ Hopefully device independent

● First look at hardware, than software


○ Emphasize software
○ Software structured in layers
○ Look at disks
I/O Devices
Two types of I/O devices:
● Block devices
● Character devices

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

Some typical device


network, and bus data
rates.

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)

● Controller is a chip with a connector which plugs


into cables to device

● Interface between the controller and the device


may be standard interface.

● Often interface between the controller and the


device may be low level.
Device Controllers
● Disk
○ Disk might have 10,000 sectors of 512 bytes per track
○ Serial bit stream comes off drive
○ Has preamble, 4096 bits/sector, error correcting code
○ Preamble has sector number, cylinder number, sector size….

● Controller assembles block from bit stream, does error


correction, puts into buffer in controller.

● After checksum verified and the block has been


declared to error free, it can be copied to main
memory.
Memory Mapped I/O
2 techniques for address I/O devices by the CPU.
● Memory Mapped I/O
● Isolated I/O

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

(a) Separate I/O ports and memory space.


(b) Memory-mapped I/O. (c) Memory mapped data buffers
and separate ports (Pentium)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
How CPU addresses registers and
buffers
o First scheme
o Puts read on control line
o Put address on address line
o Puts I/O space or memory space on signal line to differentiate
o Read from memory or I/O space
o Memory mapped approach
o Put address on address line and let memory and I/O devices
compare address with the ranges they serve

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

Loop test port_4 //check if port 4 is zero


beq ready //it is is zero, go to ready
branch loop //otherwise, continue testing
Ready
If instruction just references registers, then
need more instructions to do the test-read it
in.

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

o Harder with multiple buses because I/O


devices can’t see their addresses go by
any more

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Memory-Mapped I/O

(a) A single-bus architecture.


(b) A dual-bus memory architecture.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Solutions
o Solution is for CPU to try memory first. If it
does not get a response then it tries I/O
device
o Other fixes-snooping device, filter
addresses
o Main point-have to complicate hardware to
make this work

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?

Operation of a DMA transfer.

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

The connections between the devices and the interrupt controller


use interrupt lines on the bus rather than dedicated wires.

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)

(a) A precise interrupt. (b) An imprecise interrupt.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
How to process an imprecise
interrupt

o With great difficulty


o Either Need complicated hardware logic to
re-start after interrupt (Pentium)
o Or have complicated processing in the OS

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

Steps in printing a string.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Good vs Bad

o The good: simple idea, OK if the CPU is not


bothered too much
o The bad: CPU is bothered too much

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

Layers of the I/O software system.


Interrupt Handlers
● The idea: driver starting the I/O blocks until I/O
finishes and interrupt happens.
● Handler processes interrupt
● Wakes up driver when processing is finished
● Drivers are kernel processes with their very own
○ Stacks
○ PCs
○ states

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.

Have some similar structure:


o Driver start out by checking the input parameters to see
if they are valid or not.
o If not an error is returned.
o If valid, translation is needed.
Device Driver
o The driver may check if the device is currently in use. If it is
the request is queued.
o Once the device is on, the actual control is begin.
o Controlling means issuing a sequence of command.
o After knowing issuing command, driver start writing them
into controller’s device register.
o Next to see if the controller prepare to accept the
commands.
o After issuing command 2 situation will apply,
o Device driver wait for controller for doing some work for it by blocking
itself until interrupt comes to unblock it.
o Operation finishes without delay.
Device-Independent I/O Software
Why the OS needs a standard interface

● Driver functions differ for different drivers


● Kernel functions which each driver needs are different for
different drivers
● Too much work to have new interface for each new device
type
Interface:Driver functions
o In each class of devices OS defines a set of functions
that the driver must supply. e.g. read, write, turn on,
turn off, formatting...
o Driver has a table of pointers for those functions
o OS just needs table address to call the functions
o The table of function pointers defines the interfaces
between the driver and the rest of the operating
system.
o All devices of a given class must obey it.
Interface:Names and protection
o OS maps symbolic device names onto the right driver
o Unix: /dev/disk0 maps to an i-node which contains the
major and minor device numbers for disk0
o Major device number locates the appropriate driver, minor
device number passes parameters to the driver to specify the
unit to be read or written.
o All devices have major and minor numbers.
o Protection: In Unix and Windows devices appear as
named objects => can use file protection system

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Buffering

(a) Unbuffered input. (b) Buffering in user space.


(c) Buffering in the kernel followed by copying to user space.
(d) Double buffering in the kernel.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Buffering

Networking may involve many copies of a packet.

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.

● An alternative approach is to block the caller without failing.


● Blocked process are put on a queue.
● As soon as device becomes available and first process on the
queue is allowed to acquire it.
Device-Independent Block Size
● Device independent software provide a uniform block size to
higher layer.
● Higher layer only deal with abstract devices that all use the
same logical block size, independent of the physical sector
size.
User Space I/O Software

o Library routines are involved with I/O-printf,scanf,write


for example. These routines makes system calls
o Spooling systems-keep track of device requests
made by users.
o Create a special process(daemon) and a special
directory(spooling directory)
o Think printing.
o User generates file, puts it in a spooling directory.
o Daemon process monitors the directory, printing the user file
o File transfers also use a spooling directory

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Example Flow through layers

o User wants to read a block, asks OS


o Device independent software looks for block in cache
o If not there, invokes device driver to request block from
disk
o Transfer finishes, interrupt is generated
o User process is awakened and goes back to work

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

(a) Physical geometry of a disk with two zones.


(b) A possible virtual geometry for this disk.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD

o Optical disks have higher density than magnetic disks


o Used for distributing commercial software + reference
works (books)
o Cheap because of high production volume of music
CDs
o First used for playing music digitally

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD

o Laser burns holes on a master (coated glass) disk


o Mold is made with bumps where holes were
o Resin poured into –has same pattern of holes as
glass disk
o Aluminum put on top of resin
o Pits (depressions) and lands (unburned area) are
arranged in spirals
o Laser is used to read the pits and lands and convert
them into bits (0 and 1)

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

Recording structure of a compact disc or CD-ROM.

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

o CD’s can be used to store data as well as audio


o Needed to improve the error-correcting ability of the
CD
o Encode each byte (8 bits) in a 14 bit symbol with 6 bits
of ECC
o 42 symbols form a frame
o Group 98 frames into a CD-ROM sector
o Extra error-correcting code is attached to sector

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

o Written once, read arbitrarily many times.


o Cheaper manufacturing process led to cheaper CD-
ROM (CD-R)
o Used as backup to disk drives
o Small companies can use to make masters which they
give to high volume plants to reproduce

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD-Recordable

• Initially, dye layer is transparent and lets the laser light


pass through and reflect off the gold layer which used
instead of aluminium layer.
• To write, laser is turned up to high power.
• When the beam hits a spot of dye, it heats up, breaking
chemical bond.
• This change creates a dark spot.
• Photodetector detect the difference between dark spot
and intact area.
CD-Recordables

Cross section of a CD-R disk and laser. A silver


CD-ROM has similar structure, except without dye layer and
with pitted aluminum layer instead of gold layer.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
CD-Rewritable
● Instead of dye, uses an alloy of silver, indium, antimony and
tellurium for the recording layer.
● This alloy has two stable states: crystalline and amorphous,
with different reflectivities.
● Use lasers with 3 different powers:
● At high power- laser melt the alloy, converting it to from high
reflectivity crystalline state to the low reflectivity amorphous to
represent a pit.
● At medium power- alloy melts and reforms in its natural
crystalline state to become a land again.
● At low power- state of the material is sensed but no phase
transition occurs.
DVD (Digital Versatile Disk)

DVD use same design as CD with a few


improvements
1. Smaller pits
(0.4 microns versus 0.8 microns for CDs).
2. A tighter spiral
(0.74 microns between tracks versus 1.6
microns for CDs).
3. A red laser
(at 0.65 microns versus 0.78 microns for CDs).

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DVD (Digital Versatile Disk)

• This led to much bigger capacity ~ 5 Gbyte


(seven fold increase in capacity)
• Can put a standard movie on the DVD (133
minutes)
• Hollywood wants more movies on the same
disk, so have 4 formats

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DVD
DVD Formats

1. Single-sided, single-layer (4.7 GB).


2. Single-sided, dual-layer (8.5 GB).
3. Double-sided, single-layer (9.4 GB).
4. Double-sided, dual-layer (17 GB).

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
DVD
DVD
DVD

A double-sided, dual-layer DVD disk.

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

Backup and parity drives are shown shaded.


Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Raid Levels 4 and 5

o Raid level 3 works like level 2, except all parity bits go


on a single drive
o Raid 4,5 work with strips. Parity bits for strips go on
separate drive (level 4) or several drives (level 5)

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
RAID

Backup and parity drives are shown shaded.


Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Hard Disk Formatting

• Low level format-software lays down tracks and


sectors on empty disk (picture next slide)
• Partitioning- Making storage device visible to
OS.
• High level format is done next
Sector Format

512 byte sectors standard


Preamble:
● Star with certain bit pattern helps to recognize the start of the
sector.
● contains address of sector, cylinder number.
ECC(Error Correction Code):
Contain redundant information for recovery from errors.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Cylinder Skew

Offset sector from


one track to
next one in
order to get
consecutive
sectors
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Interleaved
Reading continuously requires a large buffer in the controller.

Next sector will be fly by the head??


Solution: Numbering the sectors in an interleaved fashion.
● Single interleaving.
● Double interleaving.

To avoid the need for interleaving, the controller should be able to


buffer the entire track.
Interleaved sectors

Copying to a buffer takes time; could wait a disk rotation before


head reads next sector. So interleave sectors to avoid this
(b,c)

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
High level format

• Partitions-for more then one OS on same


disk
• Pentium-sector 0 has master boot record
with partition table and code for boot block
• Pentium has 4 partitions-can have both
Windows and Unix
• In order to be able to boot, one sector has
to be marked as active

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
High level format for each partition

• master boot record in sector 0


• boot block program
• free storage admin (bitmap or free list)
• root directory
• empty file system
• indicates which file system is in the
partition (in the partition table)

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Power goes on

• BIOS reads in master boot record and jumps to


it
• Boot program checks which partition is active
• Reads in boot sector from active partition
• Boot sector loads bigger boot program which
looks for the OS kernel in the file system
• OS kernel is loaded and executed

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)

While head is on cylinder 11, requests for 1,36,16,34,9,12 come in


FCFS would result in 10, 35, 20, 18, 25 and 3 in total= 111.
The sequence for SSF is 12, 9, 16, 1, 34 would require
1,3,7,15,33,2 movements for a total of 61 cylinders
Elevator algorithm
o It is a greedy algorithm-the head could get
stuck in one part of the disk if the usage was
heavy
o Elevator-keep going in one direction until there
are no requests in that direction, then reverse
direction
o Real elevators sometimes use this algorithm
o Variation on a theme-first go one way, then go
the other
The Elevator

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

Suppose a disk having 300 cylinders numbered from 0 to 299.


The disk head is currently at cylinder 53. Queue is 23, 89, 132,
174, 42, 187.

Calculate total cylinder move:


● First Come First Serve.
● Shortest Seek Time First.
● Elevator Algorithm(Go up first).
● C-Scan Scheduling Algorithm.
● C-Look Scheduling Algorithm.
First Come First Serve

0 23 42 53 89 132 174 187 190


First Come First Serve

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 = 30+66+43+42+132+145 = 458


Shortest Seek Time First

0 23 42 53 89 132 174 187 190

Total move = 194


Elevator Algorithm

0 23 42 53 89 132 174 187 190

Total move = ?
C-Scan Scheduling Algorithm

0 23 42 53 89 132 174 187 190

Total move = ?
C-Look Scheduling Algorithm

0 23 42 53 89 132 174 187 190

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

(a) A disk track with a bad sector.


(b) Substituting a spare for the bad sector.
(c) Shifting all the sectors to bypass the bad one.
Bad Sectors-the OS approach

o Gets messy if the OS has to do it


o OS needs lots of information-which blocks are
bad or has to test blocks itself
o Backups problem
o Seek Error
Stable Storage
o RAIDS can protect against sectors
going bad
o Can’t protect against write operations
spitting out garbage or crashes
during writes
o Stable storage: either correct data is
laid down or old data remains in
place
o Necessary for some apps-data can’t
be lost or go bad
Assumptions
o Can detect a bad write on
subsequent reads via ECC
o Probability of having bad data in
sector on two different disks is
negligible
o If CPU fails, it stops along with any
write in progress at the time. Bad
data can be detected later via ECC
during read op
The idea and the operations
o Use 2 identical disks-do the same
thing to both disks
o Use 3 operations
o Stable write-first write, then read back
and compare. If they are the same
write to second disk. If write fails, try
up to n times to get it to succeed.
After n failures keep using spare
sectors until it succeeds. Then go to
disk 2.
The idea and the OPS
o Stable read-read from disk 1 n times
until get a good ECC, otherwise read
from disk 2 (assumption that
probability of both sectors being bad
is negligible)
o Crash recovery-read both copies of
blocks and compare them. If one
block has an ECC error, overwrite it
with the good block. If both pass the
ECC test, then pick either
CPU Crashes

(a)Crash happens before write (b) crash happens during write to 1


(c)crash happens after 1 but before 2 (d) during 2, after 1 (e) both
are the same
End of Slide

You might also like