Skip to main content

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):

  1. Memory bus — CPU ↔ DRAM (short, fast, expensive)
  2. General I/O (e.g. PCI / PCIe) — graphics, NICs, NVMe
  3. 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.

Quiz

Rank the sequence

1 / 1

Order the canonical programmed I/O protocol steps (top = first).

OrderDrag the handle to rearrange — top should be first.

  1. 1Poll STATUS until done
  2. 2Poll STATUS until not busy
  3. 3Write data to DATA
  4. 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.

Interrupts are not always better

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​

MethodIdea
I/O instructionsPrivileged in/out (x86) to ports
Memory-mapped I/OLoads/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].

Quiz

Rank the sequence

1 / 1

Order the IDE protocol steps described for handling a request (top = first).

OrderDrag the handle to rearrange — top should be first.

  1. 1Wait until READY / not BUSY
  2. 2Program sector count and LBA
  3. 3Transfer data on DRQ
  4. 4Handle the IRQ
  5. 5Check the ERROR bit
  6. 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​

Quiz

I/O devices

1 / 6

Peripheral 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

Lab: device interface

0/4 started

Loading lab…

Summary​

TechniqueRole
PollingSimple status check — costly if device is slow
InterruptsOverlap CPU with I/O; watch livelock / coalescing
DMAOffload bulk data movement
MMIO / port I/OReach device registers
DriversHide device quirks behind a generic block API

Previous: Event-based Concurrency

Next: Hard Disk Drives — geometry, seek/rotate/transfer, scheduling.

Further Reading​