OS
Prepared By : Sheetal J. Nagar
Topics covered
Principles of I/O Hardware:
I/O devices,
Device controllers ,
Direct memory access
Principles of I/O Software:
Goals of Interrupt handlers ,
Device drivers ,
Device independent I/O software ,
Secondary-Storage Structure: Disk structure
Disk scheduling algorithm
RAID
Introduction
One of the main functions of OS is to control all
the IO devices of the computer system.
OS hides internal complexity working of IO
devices.
1) I/O Devices
2) Device Controllers
3) Direct Memory Access
1) I/O devices
“The devices which are used for input and output
purpose are known as I/O devices”
I/O devices can be roughly divided into two
categories:
1) block devices
2) character devices.
I/O devices
Block devices Character devices
“Stores information in fixed-size “A character device delivers or
blocks”. accepts a stream of characters,
Each block has its own address. without regard to any block
Block size ranges from 512 to 65,536 structure”.
bytes. It is not addressable and does not
All transfers are in units of one or have any seek operation.
more consecutive blocks. Example :Printers, network interfaces
Each block can be accessed
independent of all the others.
Ex. Hard disks, Blu-ray discs, and
USB sticks
Block Device
A block device is one that stores information in fixed-size
blocks, each one with its own address.
Common block sizes range from 512 to 65,536 bytes.
All transfers are in units of one or more entire (consecutive)
blocks.
Each block can be accessed independent of all the others.
Ex. Hard disks, Blu-ray discs, and USB sticks
Character Device
“A character device delivers or accepts a stream of
characters, without regard to any block structure”.
It is not addressable and does not have any seek
operation.
Example :Printers, network interfaces
2) Device controllers
I/O unit consist of a 2 components:
1) Mechanical component : Device itself
2) Electronic component : known as device controller
“Electronic component of I/O devices is called the Device
Controller.”
The controller card usually has a connector on it, into which a
cable leading to the device itself can be plugged.
Components of
I/O devices
Mechanical Electronic
component component
Device itself Known as
Device Controller
Controller's tasks :
1) Convert serial bit stream to block of bytes
2) perform error correction as necessary
3) Acts as an interface between device and OS
4) Each controller has few registers which are used for
communicating with the CPU.
5) By writing into those registers, the OS can give commands
to the device to do something, like turn on or turn off device,
deliver data, accept data etc.
3) Direct Memory Access
The CPU can request data from an I/O controller one byte at
a time, but doing so wastes the CPU’s time, so a different
scheme, called DMA (Direct Memory Access) is often
used.
We assume that the CPU accesses all devices and memory
via a single system bus that connects the CPU, the memory,
and the I/O devices.
3) Direct Memory Access
To explain how DMA works, let us first look at how disk reads occur when
DMA is not used.
1) CPU requests the device controller to read one byte from the disk and
transfer it into the RAM.
2) After receiving the request from the CPU, controller reads one byte
from the disk and writes it in RAM.
3) After completing this for just one block, controller sends notification to
the CPU that it has completed the transfer of one byte.
4) After receiving notification from the driver, CPU repeats steps 1) , 2)
and 3) and completes the transfer of n number of blocks.
Disadvantages:
Here, CPU utilization is low, because after transfer of each
block is completed, CPU has to send request again and
again.
This concept is known as “Interrupt Driven I/O”.
3) Direct Memory Access
DMA
To overcome the problem occurring in the interrupt driven I/O,
DMA can be used.
DMA utilizes CPU more than interrupt driven I/O.
In DMA CPU transfers the responsibility to DMA controller.
When DMA is used, the procedure is different.
When DMA is used things are as shown below.
1) CPU initiates the transfer and requests DMA controller to begin the
transfer by sending starting address and total no. of blocks to read
(count).
2) DMA controller requests Disk Controller to transfer the data from the
disk to main memory.
3) After completing the transfer the Disk Controller gives acknowledge to
DMA controller.
4) DMA controller will decrement the count by one.
5) If(count > 0) { Step 2) , 3) and 4) are repeated. }
else { DMA controller will acknowledge the CPU that reading
task is completed. }
Figure : Operation of a DMA transfer
1. CPU
programs
the DMA
controller
4. Ack
Interrupt 2. DMA requests 3. Data
when done transfer to memory transferred
count = count - 1
If(count > 0)
{ Step 2) , 3) and 4) are repeated. }
else
{ acknowledge the CPU that task is completed. }
Advantages:
CPU doesn’t receive acknowledgement after each
block transfer like the case of interrupt driven I/O.
So, CPU can be utilized in better way than interrupt
driven I/O.
1) Device independence
2) Uniform Naming
3) Error handling
4) Synchronous vs. Asynchronous transfers
5) Buffering
1) Device independence
A key concept in the design of I/O software is known as device
independence.
It means we should be able to write i/o programs without having
to specify the device in advance.
For example, a program that reads a file as input should be able
to read a file on a hard disk, a DVD, or on a USB stick without
having to be modified for each different device.
It is up to the operating system to take care of the problems
caused by the fact that these devices really are different and
require very different command sequences to read or write.
2) Uniform Naming
Closely related to device independence is the goal of
uniform naming.
The name of a file or a device should simply be a
string (or an integer) and not depend on the device.
All files and devices are addressed the same way: by
a path name.
3) Error handling
In general, “errors should be handled as close to the hardware
as possible”.
If the controller discovers a read error, it should try to correct the
error itself if it can.
If it cannot, then the device driver should handle it.
Many errors are temporary, such as read errors caused by dust on
the read head, and will frequently go away if the operation is
repeated.
In many cases, error recovery can be done transparently at a low
level without the upper levels even knowing about the error.
4) Synchronous vs. Asynchronous transfers
Synchronous means blocking transfer
Asynchronous means interrupt-driven transfers.
Most physical I/O is asynchronous—the CPU starts the
transfer and goes off to do something else until the interrupt
arrives.
User programs are much easier to write if the I/O operations
are blocking—after a read system call the program is
automatically suspended (blocked) until the data are available
in the buffer.
5) Buffering
Often data that come from a device cannot be stored
directly in their final destination.
For example, when a packet comes in from the network,
the operating system does not know where to put it until it
has stored the packet somewhere and examined it.
Buffering involves copying and often has a major impact
on I/O performance.
I/O Software Layers
1) Interrupt Handlers
Interrupt : “A signal that gets the attention of the CPU and
is usually generated when I/O is required”.
For example:
Hardware interrupts are generated when a key is pressed
or when the mouse is moved.
Software interrupts are generated by a program requiring
disk input or output.
An internal timer may continually interrupt the computer
several times per second to keep the time of day current
or for timesharing purposes.
1) Interrupt Handlers
When an interrupt occurs, control is transferred to the
operating system, which determines the action to be
taken.
Interrupts play a major role to communicate between
Device and CPU.
So we need to have a software layer which can handle
the interrupts related to I/O device.
5) Interrupt Handlers
Steps:
CPU saves the state of currently executing process.
Stops current process.
Runs the interrupt-service procedure. It will extract
information from the interrupting device controller’s
registers. thus, it serves the new process by loading new
data structure (Process table) of new process.
After serving the interrupt, again old process is started
from the point it left.
2) Device Drivers
“Each I/O device attached to a computer needs some
device-specific code for controlling it, This code is
called as device driver”.
It is generally written by the device’s manufacturer and
delivered along with the device.
Device
Drivers
Printer Camcorder CD-ROM
Driver Driver Driver
Printer Controller Camcorder Controller CD-ROM Controller
Functions of device drivers
1) Device driver accept read and write requests from device
2) Device driver must initialize the device if needed.
3) It also checks statues of devices. If it is currently in use then
queue the request for latter processing. If device is in idle
state then request can be handled now.
4) Device driver writes the command sequence to the control
register.
5) It provides error handling facility related to a particular I/O
device.
3) Device-Independent I/O Software
As we know OS interfaces with different device drivers.
Each device has separate interface with OS and driver
function of different device differ from driver to driver (and
device to device).
It means interfacing with each new driver requires lot of
new programming effort.
So we need a layer which can handle this issue and
which is device independent.
3) Device-Independent I/O Software
1) Uniform interfacing for device drivers
▪ It provides uniform interface between OS and
different device drivers.
▪ If this layer is not included, the programming
efforts are large to write different interface code
for different drivers.
2) Buffering
▪ I/O performance increases due to buffering capabilities.
Unbuffered input Buffering in
Buffering in Double buffering
the kernel
user space in the kernel
followed by
copying to
user space.
3) Error reporting
▪ Here, general errors are handled like :
▪ Writing to an input device (keyboard, mouse, scanner)
▪ Reading from an output device (Printer, Plotter)
▪ Specifying invalid device to use (Ex. trying to access Disk3,
while system has only 2 Disks).
▪ Another class of the errors:
• Trying to read/write a disk block that has been damaged.
• Trying to read from a camcorder that has been switched off.
• In this case it is up to the device driver to decide what to do.
If the driver doesn’t know what to do, it may pass the problem
to the device independent I/O software.
4) Allocating and releasing Devices
▪ Some devices such as CD-ROM recorders can be used only
by a single process at any given moment.
▪ A mechanism for requesting and releasing dedicated devices
is required.
5) Providing a device-independent block size
▪ Different disks may have different sector sizes.
▪ It is up to the device independent I/O software to hide
▪ this fact and provide a uniform block size to higher layers.
Disk Structure
Each disk platter has a flat circular shape, like a CD. The two surfaces of
a platter are covered with a magnetic material.
The heads are attached to a disk arm that moves all the heads as a unit.
The surface of a platter is logically divided into circular tracks, which are
subdivided into sectors.
The set of tracks that are at one arm position makes up a cylinder.
There may be thousands of cylinders in a disk drive, and each track may
contain hundreds of sectors.
Modern disk drives are addressed as large one-dimensional arrays of
logical blocks, where the logical block is the smallest unit of transfer.
The size of a logical block is usually 512 bytes, although some disks can
have a different logical block size, such as 1024 bytes.
1) FCFS
2) SSF or SSTF
3) SCAN (Elevator)
4) C-SCAN
5) LOOK
6) C-LOOK
Disk Arm Scheduling Algorithms
Let’s consider how long it takes to read or write a disk block.
The time required is determined by three factors:
1) Seek time – “the time to move the arm to the proper cylinder”
2) Rotational delay – “Time for the proper sector to appear under the
reading head”
3) Actual data transfer time.
For most disks, the seek time dominates the other two times,
so reducing the mean seek time can improve system
performance substantially.
1) FCFS (First-Come, First-Served)
“The request that comes first is served first”.
If the disk driver accepts requests one at a time and carries
them out in that order, that is, FCFS.
FCFS Example:
Current Cylinder Position: 11
Cylinder Request: 1, 36, 16, 34, 9, 12
Total Number of Cylinders: 40
Find Total Arm Movement.
If 1 seek takes 6 ms then find total seek time for FCFS algo.
0 1 9 11 12 16 34 36 39
2) SSF (Shortest Seek First)
or
SSTF (Shortest Seek Time First)
The SSTF algorithm selects the request with the
minimum seek time from the current head position.
It always handles the closest request next, to minimize
seek time.
SSTF Example:
Current Cylinder Position: 11
Cylinder Request: 1, 36, 16, 34, 9, 12
Total Number of Cylinders: 40
Find Total Arm Movement.
If 1 seek takes 6 ms then find total seek time for SSTF algo.
0 1 9 11 12 16 34 36 39
3) SCAN or Elevator
This algorithm works on the basic concept of elevator.
“The arm starts moving in one direction (upward or downward)
and serves all the requests coming in its path.
After reaching to one end of disk, it reverses the direction and
again serves all the remaining requests coming in its path”.
SCAN Example:
Current Cylinder Position: 11
Cylinder Request: 1, 36, 16, 34, 9, 12
Total Number of Cylinders: 40
Find Total Arm Movement.
If 1 seek takes 6 ms then find total seek time for SCAN algo.
0 1 9 11 12 16 34 36 39
4) C-SCAN
It works in the same way as SCAN with little changes as follows:
“After reaching to One Disk End, it directly jumps to the Other
End, without serving any request along that path ”.
After reaching to that end again starts serving all the requests
along that path.
C-SCAN Example:
Current Cylinder Position: 11
Cylinder Request: 1, 36, 16, 34, 9, 12
Total Number of Cylinders: 40
Find Total Arm Movement.
If 1 seek takes 6 ms then find total seek time for C-SCAN
algo.
0 1 9 11 12 16 34 36 39
5) LOOK
It is an enhanced version of SCAN algorithm.
“The arm starts moving in one direction (upward or
downward) and serves all the requests coming in its path.
After reaching to last request in one direction of disk, it
reverses the direction and again serves all the remaining
requests coming in its path”.
LOOK Example:
Current Cylinder Position: 11
Cylinder Request: 1, 36, 16, 34, 9, 12
Total Number of Cylinders: 40
Find Total Arm Movement.
If 1 seek takes 6 ms then find total seek time for LOOK algo.
0 1 9 11 12 16 34 36 39
6) C-LOOK (Circular - LOOK)
It works in the same way as SCAN with little changes as
follows:
“After reaching to the last request in one direction, it directly
jumps to the last request in the opposite direction, without
serving any request along that path ”.
C-LOOK Example:
Current Cylinder Position: 11
Cylinder Request: 1, 36, 16, 34, 9, 12
Total Number of Cylinders: 40
Find Total Arm Movement.
If 1 seek takes 6 ms then find total seek time for C-LOOK
algo.
0 1 9 11 12 16 34 36 39
RAID
RAID is “Redundant Array of Independent Disks”.
Parallel processing is being used to speed up CPU
performance.
Nowadays, most manufacturers refer to the seven standard
configurations as RAID level 0 through RAID level 6 to
achieve parallel processing.
Here actually there is no hierarchy but the term ‘‘level’’ simply
indicates seven different organizations possible.
RAID Level 0
RAID Level 0
“single disk is divided up into strips of k sectors each, with
sectors 0 to k - 1 being strip 0,
sectors k to 2k - 1 strip 1, and so on.
Distributing data over multiple drives like this is called striping”.
Performance is excellent and the implementation is
straightforward.
Disadvantage of RAID level 0 is that the reliability is potentially
worse than having a SLED (Single Large Expensive Disk).
RAID Level 1
RAID Level 1
It duplicates all the disks.
so there are four primary disks and four backup disks.
On a write, every strip is written twice.
On a read, either copy can be used.
Consequently, write performance is no better than for a single
drive, but read performance can be up to twice as good.
Fault tolerance is excellent: if a drive crashes, the copy is simply
used instead.
Recovery consists of simply installing a new drive and copying the
entire backup drive to it.
RAID Level 2
RAID Level 2
RAID level 2 works on a word basis, possibly even a byte
basis.
Imagine splitting each byte into a pair of 4-bit nibbles, then
adding a Hamming code to each one to form a 7-bit word (of
which bits 1, 2, and 4 were parity bits).
Hamming code is used for error correction and detection.
All seven drives need to be synchronized in terms of arm
position and rotational position.
RAID Level 3
Parity
RAID Level 3
RAID level 3 is a simplified version of RAID level 2.
Here a single parity bit is computed for each data word and written
to a parity drive.
As in RAID level 2, the drives must be exactly synchronized, since
individual data words are spread over multiple drives.
In the case of a drive crashing, it provides full 1-bit error
correction since the position of the bad bit is known.
RAID Level 4
P(0-3)
P(4-7)
P(8-11)
RAID Level 4
It like RAID level 0, with a strip-for-strip parity written onto an extra drive.
All the strips are EXCLUSIVE ORed together, resulting in a parity strip k
bytes long.
XOR ( 1111 , 1001 , 1010 , 0000 ) = 1100
If drive 2 crashes which contains data 1001, then it can be computed by
performing XOR operation from remaining 3 disks and parity disk.
For Recovery perform following:
XOR ( 1111 , 1010 , 0000 , 1100 ) = 1001
Disadvantage : If one sector is changed, it is necessary to read all the
drives in order to recalculate the parity, which must then be rewritten.
Thus, parity drive may become a bottleneck.
RAID Level 5
P(0-3)
P(4-7)
P(8-11)
P(12-15)
P(16-19)
RAID Level 5
The bottleneck problem of level 4 is eliminated in RAID level 5
by distributing the parity bits uniformly over all the drives,
round-robin fashion.
However, in the event of a drive crash, reconstructing the
contents of the failed drive is a complex process.
RAID Level 6
P(0-2) P’(0-2)
P(3-5) P’(3-5)
P(6-8) P’(6-8)
P(9-11) P’(9-11)
RAID Level 5
Raid level 6 is similar to RAID level 5, except that an additional
parity block is used.
In other words, the data is striped across the disks with two
parity blocks instead of one.
As a result, writes are bit more expensive because of the parity
calculations, but reading operation offers more reliability.
Reference Books:
1. “Operating System Concepts” by Silberschatz,
Peter B. Galvin and Greg Gagne
2. “Modern Operating Systems” by Andrew S
Tanenbaum