Chapter 7 IO
Chapter 7 IO
CPU instructions to
access I/O devices
Direct Memory Access
• Feature of computer systems that allows certain
hardware subsystems to access main memory
(RAM), independent of the central processing
unit (CPU).
• Without DMA, when the CPU is using
programmed input/output, it is typically fully
occupied for the entire duration of the read or
write operation, and is thus unavailable to
perform other work.
Direct Memory Access
• With DMA, the CPU first initiates the transfer,
then it does other operations while the transfer is
in progress, and it finally receives an interrupt
from the DMA controller when the operation is
done.
• This feature is useful when the CPU needs to
perform useful work while waiting for a relatively
slow I/O data transfer.
• Many hardware systems such as disk drive
controllers, graphics cards, network cards and
sound cards use DMA.
Disk read-write without a DMA
• The disk controller reads the block
Drive
from the drive serially, bit by bit,
until the entire block is in the Disk Main
controller’s buffer. CPU Controller Buffer Memory
• Next, it computes the checksum
to verify that no read errors have
occurred.
Bus
Disk read-write without a DMA
• Then the controller causes an
Drive
interrupt, so that OS can read the
block from controller’s buffer (a Disk Main
byte or a word at a time) by CPU Controller Buffer Memory
executing a loop.
• After reading every single part of
the block from controller device
register, the operating system will
store them into the main memory.
Bus
Disk read-write using DMA
Drive
1. CPU
programs DMA Disk Main
CPU DMA Controller Controller Memory
Buffer
controller
Address
Count
Control
Bus
Step 1: First the CPU programs the DMA controller by setting its registers so it
knows what to transfer where.
It also issues a command to the disk controller telling it to read data from the disk
into its internal buffer and verify the checksum.
When valid data are in the disk controller’s buffer, DMA can begin.
Disk read-write using DMA
Drive
1. CPU
programs DMA Disk Main
CPU DMA Controller Controller Memory
Buffer
controller
Address 2. DMA
Count requests
transfer to
Control
memory
Bus
Step 2: The DMA controller initiates the transfer by issuing a read request over the
bus to the disk controller.
This read request looks like any other read request, and the disk controller does
not know (or care) whether it came from the CPU or from a DMA controller.
Disk read-write using DMA
Drive
1. CPU
programs DMA Disk Main
CPU DMA Controller Controller Memory
Buffer
controller
Address 2. DMA
Count requests
transfer to 3. Data
Control
memory transferred
Bus
Typically, the memory address to write to is on the bus’ address lines, so when the
disk controller fetches the next word from its internal buffer, it knows where to
write it.
Step 3: The write to memory is another standard bus cycle.
Disk read-write using DMA
Drive
1. CPU
programs DMA Disk Main
CPU DMA Controller Controller Memory
Buffer
controller 4. ACK
Address 2. DMA
Count requests
transfer to 3. Data
Control
memory transferred
Bus
Step 4: When the write is complete, the disk controller sends an acknowledgement
signal to the DMA controller, also over the bus.
The DMA controller then increments the memory address to use and decrements
the byte count.
If the byte count is still greater than 0, steps 2 to 4 are repeated until it reaches 0.
Disk read-write using DMA
Drive
1. CPU
programs DMA Disk Main
CPU DMA Controller Controller Memory
Buffer
controller 4. ACK
Address 2. DMA
Count requests
transfer to 3. Data
Control
memory transferred
Bus
At that time, the DMA controller interrupts the CPU to let it know that the transfer
is now complete.
When the OS starts up, it does not have to copy the disk block to memory; it is
already there.
Modes of bus operation
• The buses can be operated in two modes
1. Word-at-a-time mode: Here the DMA requests for
the transfer of one word and gets it.
• If CPU wants the bus at same time then it has to wait.
• This mechanism is known as Cycle.
2. Block mode: Here the DMA controller tells the device
to acquire the bus, issues a series of transfer and
then releases the bus.
• This form of the operation is called Burst mode.
• It is more efficient then cycle stealing.
Goals of I/O Software
1. Device independence
– It should be possible to write programs that can
access any I/O devices without having to specify
device in advance.
– For example, a program that reads a file as input
should be able to read a file on a floppy disk, on a
hard disk, or on a CD-ROM, without having to modify
the program for each different device.
2. Uniform naming
– Name of file or device should be some specific string
or number. It must not depend upon device in any
way.
Goals of I/O Software
3. Error handling
– Error should be handled as close to hardware as
possible.
– If any controller generates error then it tries to solve
that error itself. If controller can’t solve that error
then device driver should handle that error, perhaps
by reading all blocks again.
– Many times when error occurs, error is solved in
lower layer. If lower layer is not able to handle error
then problem should be told to upper layer.
– In many cases error recovery can be done at a lower
layer without the upper layers even knowing about
error.
Goals of I/O Software
4. Synchronous vs. asynchronous transfers
– Most of devices are asynchronous device. CPU starts transfer
and goes off to do something else until interrupt occurs.
– I/O Software needs to support both the types of devices.
5. Buffering
– Data comes in main memory cannot be stored directly.
– For example data packets come from the network cannot be
directly stored in physical memory.
– Packets have to be put into output buffer for examining them.
– Some devices have several real-time constraints, so data must
be put into output buffer in advance to decouple the rate at
which buffer is filled and the rate at which it is emptied, in
order to avoid buffer under runs.
Device driver
• I/O devices which are plugged with computer
have some specific code for controlling them.
This code is called the device driver.
• Each device driver normally handles one device
type, or at most one class of closely related
devices.
• Generally device driver is delivered along with the
device by device manufacturer.
• Device drivers are normally positioned below the
rest of Operating System.
Logical positioning of device drivers
User User
Space Programs
Rest of
Operating System
Kernel
Space
Printer CD-Rom
Driver Driver
Printer CD-Rom
Hardware
Controller Controller
Device
Functions of device drivers
1. Device driver accept abstract read and write
requests from device independent software.
2. Device driver must initialize the device if
needed.
3. It also controls power requirement and log
event.
4. 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.
5. Pluggable device can be added or removed while
Device Independent I/O Software
• Exact boundary between the drivers and the
device independent I/O software is system
dependent.
• Function of device independent I/O Software
1. Uniform interfacing for device drivers.
2. Buffering.
3. Error Reporting.
4. Allocating and releasing dedicated devices.
5. Providing a device-independent block size.
Uniform interfacing for device drivers
• A major issue of an OS is how to make all I/O
devices and drivers look more or less the same.
• One aspect of this issue is the interface between
the device drivers and the rest of the OS.
Uniform interfacing for device drivers
• Figure shows situation
in which each device
driver has a different
interface to OS, it
means that interfacing
each new driver
requires a lot of new
programming effort.
Uniform interfacing for device drivers
• Figure shows a
different design in
which all drivers have
the same interface.
• Now it becomes much
easier to plug in a new
driver.
Buffering
• Buffering is also issue, both for block and
character devices.
• In case of a process, which reads the data from
the modem, without buffering the user process
has to be started up for every incoming
character.
• We have four different options:
1. Unbuffered input
2. Buffering in user space
3. Buffering in kernel followed by user space
4. Double buffering in kernel space
Unbuffered input
• No buffer available in user
User User
space or kernel space. Space Programs
Modem
Buffering in user space
• Buffer in users pace: here
User
user process provides an n- Space
character buffer in user
space and does a read of n- Kernel
Space
characters.
Modem
Buffering in kernel space followed by user space
Modem
Double buffering in kernel space
9 50
1
0 50
1
9
• Disk movement will be 11, 12, 16, 34, 36, 50, 0, 1,9.
• Total cylinder movement: (12-11) + (16-12) + (34-16) +(36-34)
+(50-36) + (50-0) + (1-0)+ (9-1) = 98
Examples for Disk Arm Scheduling Algorithm