Source: Operating Systems: Three Easy Pieces (OSTEP) — Chapter 36, Arpaci-Dusseau & Arpaci-Dusseau
I/O Devices
Programs without input always produce the same result; programs without output have no observable purpose. Persistence starts here: how the OS integrates I/O devices efficiently.
Crux: mechanisms for talking to devices — and how to keep the CPU useful while they work.
System architecture
Classical hierarchy (physics + cost):
- Memory bus — CPU ↔ DRAM (short, fast, expensive)
- General I/O (e.g. PCI / PCIe) — graphics, NICs, NVMe
- Peripheral bus (SATA, USB, …) — disks, keyboards, mice
Faster buses must be shorter; slow devices tolerate longer, cheaper links with many ports. Modern chipsets (e.g. Intel Z270-style) keep graphics/memory close to the CPU, then an I/O hub for eSATA, USB, and PCIe.
Canonical device
Two parts: interface (registers/protocol the OS uses) and internals (microcontroller, memory, firmware — e.g. RAID).
Simplified registers: STATUS, COMMAND, DATA.
while (STATUS == BUSY) ; // poll
write data to DATA // programmed I/O (PIO)
write command to COMMAND // starts the device
while (STATUS == BUSY) ; // poll until done
Simple — but polling burns CPU on slow devices.
Rank the sequence
1 / 1Order the canonical programmed I/O protocol steps (top = first).
OrderDrag the handle to rearrange — top should be first.
- 1Poll STATUS until done
- 2Poll STATUS until not busy
- 3Write data to DATA
- 4Write command to COMMAND (starts the device)
Drag items into the correct order (or use ↑↓), then check.
Interrupts
Issue the request, sleep the waiter, run something else. Device raises an IRQ → ISR finishes the transfer and wakes the process. CPU and device overlap.
Fast devices: poll may win (IRQ + context switch cost). Hybrid: poll a bit, then interrupt. Under packet floods, IRQ storms can livelock the OS [MR96] — polling restores control. Coalescing batches IRQs (latency vs overhead trade-off) [A+11].
DMA
PIO makes the CPU copy every word to/from the device. A DMA engine is programmed with address, length, and target device; it moves data and interrupts when done so the CPU can run other work.
Talking to registers
| Method | Idea |
|---|---|
| I/O instructions | Privileged in/out (x86) to ports |
| Memory-mapped I/O | Loads/stores to special addresses routed to the device |
Only the OS should talk to devices directly — otherwise any process could own the disk.
Device drivers
Keep most of the OS device-neutral: file system → generic block layer → driver (SCSI, ATA, …). Apps can also use a raw block interface (fsck, defrag).
Downside: rich device errors may collapse to generic EIO [G08]. Drivers are a huge fraction of kernel code (~70% in Linux studies [C01]) and a common crash source [S03].
Case study: IDE (xv6)
Protocol sketch: wait READY/not BUSY → program sector count + LBA → issue READ/WRITE → transfer data on DRQ → handle IRQ → check ERROR.
xv6: ide_rw queues and sleeps; ide_start_request uses outb/outsl; ide_intr insls data on reads, wakeup, starts the next queued request [CK+08].
Rank the sequence
1 / 1Order the IDE protocol steps described for handling a request (top = first).
OrderDrag the handle to rearrange — top should be first.
- 1Wait until READY / not BUSY
- 2Program sector count and LBA
- 3Transfer data on DRQ
- 4Handle the IRQ
- 5Check the ERROR bit
- 6Issue the READ/WRITE command
Drag items into the correct order (or use ↑↓), then check.
Interactive
Try: bus hierarchy · poll protocol · interrupts · DMA · drivers/IDE.
Check your understanding
I/O devices
1 / 6Peripheral buses (USB, SATA) sit farther from the CPU mainly because…
Pick an answer to see if you are right.
Coding challenges
Homework lab: peek at a real driver
OSTEP Ch. 36 has no separate homework tarball — study a real IDE/AHCI path (xv6 ide.c or Linux block docs) and map it to the canonical protocol.
Lab: device interface
0/4 startedSummary
| Technique | Role |
|---|---|
| Polling | Simple status check — costly if device is slow |
| Interrupts | Overlap CPU with I/O; watch livelock / coalescing |
| DMA | Offload bulk data movement |
| MMIO / port I/O | Reach device registers |
| Drivers | Hide device quirks behind a generic block API |
Previous: Event-based Concurrency
Next: Hard Disk Drives — geometry, seek/rotate/transfer, scheduling.
Further Reading
- Remzi & Andrea Arpaci-Dusseau — Operating Systems: Three Easy Pieces Ch. 36
- OSTEP file-devices PDF
- xv6
ide.c[CK+08]; Mogul & Ramakrishnan livelock [MR96]; Chou et al. OS bugs [C01]; Smotherman interrupt history [S08]