I/O Management
Amir H. Payberah
payberah@[Link]
2022
Overview
I I/O management is a major component of OS design and operation.
I Ports, busses, device controllers connect to various devices.
I Device drivers encapsulate device details.
• Present uniform device-access interface to I/O subsystem.
1 / 36
I/O Hardware
2 / 36
I/O Hardware
I Variety of I/O devices:
• Storage, e.g., disks, tapes
• Transmission, e.g., network connections, bluetooth
• Human-interface, e.g., screen, keyboard, mouse, audio in and out
I We only need to understand how the devices are attached and how the
software can control the hardware.
3 / 36
Common Concepts in I/O Hardware
I Port: connection point for device.
I Bus: set of wires and protocols that specify the messages that can be
sent on the wires.
I Controller: integrated or separate circuit board that operate a port, a
bus, or a device.
4 / 36
Port
I Device I/O ports addresses on PCs.
5 / 36
Bus
I PCI bus: connects the processor-memory subsystem to fast devices.
I Expansion bus: connects relatively slow devices.
I Serial-attached SCSI (SAS)
6 / 36
Host-Device Interaction
7 / 36
Host-Device Interaction
I Polling
I Interrupt
I Direct memory access (DMA)
8 / 36
Polling (1/2)
I A handshake between the host and a controller.
I Assume 2 bits for coordination: busy and command-ready bits.
I For each byte of I/O:
1. Host reads the busy bit from the status register until 0.
2. Host sets the write bit and if write copies data into the data-out
register.
3. Host sets the command-ready bit.
4. Controller sets the busy bit, executes transfer.
5. Controller clears the busy bit, error bit, and command-ready bit when
transfer done.
9 / 36
Polling (2/2)
I Step 1 is busy-wait cycle (polling) to wait for I/O from device.
I Reasonable if device is fast.
I But inefficient if device slow.
10 / 36
Interrupts (1/3)
I Polling can happen in 3 instruction cycles.
• (1) read status, (2) extract status bit, and (3) branch if not zero.
• Inefficient, but more efficient way?
I CPU interrupt-request line is triggered by I/O device.
• Checked by processor after each instruction.
• Saves state and jumps to interrupt-handler routine at a fixed address in
memory.
11 / 36
Interrupts (2/3)
12 / 36
Interrupts (3/3)
I The interrupt mechanism accepts an address: a number that selects a
specific interrupt-handling routine.
13 / 36
Direct Memory Access (DMA)
I Bypasses CPU to transfer data
directly between I/O device and
memory.
14 / 36
Application I/O Interface
15 / 36
Application I/O Interface
I I/O system calls encapsulate device behaviors in generic classes.
I Device-driver layer hides differences among I/O controllers from kernel.
I Each OS has its own I/O subsystem structures and device driver frame-
works.
16 / 36
A Kernel I/O Structure
17 / 36
Characteristics of I/O Devices (1/2)
I Devices vary in many dimensions
• Data-transfer mode: character or block
• Access method: sequential or random-access
• Transfer schedule: synchronous or asynchronous (or both)
• Sharing: sharable or dedicated
• Device speed: speed of operation
• I/O direction: read-write, read only, or write only
18 / 36
Characteristics of I/O Devices (2/2)
19 / 36
Character Devices
I Character devices include keyboards, mouse, serial ports.
I A character device transfers bytes one by one.
I Commands include get() and put().
20 / 36
Block Devices
I Block devices include disk drives.
I Commands include read() and write() and seek() for random-access
devices.
21 / 36
Network Devices
I Varying enough from block and character to have own interface.
I Linux, Unix, Windows and many others include socket interface.
• Separates network protocol from network operation.
22 / 36
Clocks and Timers
I Provide current time, elapsed time, and timer (trigger operation X at
time T)
I Programmable interval timer, the hardware used for timings, and periodic
interrupts.
I Normal resolution about 1/60 second.
I Some systems provide higher-resolution timers.
23 / 36
Blocking, Nonblocking and Asynchronous I/O
I Blocking: process suspended until I/O completed
• Insufficient for some needs
I Nonblocking: I/O call returns as much as available
• User interface, data copy (buffered I/O)
• Implemented via multi-threading
• select() to find if data ready then read() or write() to transfers.
I Asynchronous: process runs while I/O executes
• I/O subsystem signals process when I/O completed.
24 / 36
Synchronous vs. Asynchronous I/O Methods
Synchronous Asynchronous
25 / 36
Kernel I/O Subsystem
26 / 36
Kernel I/O Subsystem
I Kernels provide many services related to I/O:
• Scheduling
• Buffering
• Caching
• Spooling
• Device reservation
• Error handling
27 / 36
Scheduling (1/2)
I Determine a good order in which to execute I/O requests.
I Some I/O request ordering via per-device queue.
I Some OSs try fairness.
28 / 36
Scheduling (2/2)
I In asynchronous I/O the kernel must be able to keep track of many I/O
requests at the same time.
• The OS attaches the wait queue to a device-status table.
• The table contains an entry for each I/O device.
• If the device is busy with a request, the type of request and other
parameters will be stored in the table entry for that device.
29 / 36
Buffering and Caching
I Buffering: stores data in memory while transferring between devices.
• To cope with device speed mismatch.
• To cope with device transfer size mismatch, e.g., fragmentation and
reassembly of messages.
• To maintain copy semantics.
I Caching: faster device holding copy of data.
• Always just a copy
• Key to performance
30 / 36
Spooling and Device Reservation
I Spooling: a buffer that holds output for a device.
• If device can serve only one request at a time, i.e., printing
I Device reservation: provides exclusive access to a device.
• System calls for allocation and de-allocation
• Watch out for deadlock
31 / 36
Error Handling
I OS can recover from disk read, device unavailable, and transient write
failures.
• Retry a read or write.
• Track error frequencies, stop using device with increasing frequency of
retry-able errors.
I Most return an error number when I/O request fails.
I System error logs hold problem reports.
32 / 36
I/O Protection
I A user process may accidentally or purposefully attempt to disrupt normal
operation via illegal I/O instructions.
I I/O must be performed via system calls.
33 / 36
Summary
34 / 36
Summary
I I/O hardware: port, bus, controller
I Host-device interaction: polling, interrupt, DMA
I Devices: char, block, network
I Kernel I/O: schedulling, buffering, caching, spooling, device reservation,
error handling
35 / 36
Questions?
Acknowledgements
Some slides were derived from Avi Silberschatz slides.
36 / 36