Sub topic 1
I/O Devices and Controllers
Prerequisites
Assume you know basic CPU and memory organization, system calls, user mode and kernel mode, and hardware interrupts. You should also know that a device can be much slower than the CPU.
I/O devices and controllers
An I/O device is hardware that exchanges data with a computer. Examples include keyboards, disks, network cards, printers, and displays. Devices differ in speed, data format, and control requirements, so the operating system does not usually communicate with each device directly.
A device controller is hardware that manages a particular device. It provides a standard interface to the CPU and hides device-specific details. A controller commonly contains:
- A data register for data being read or written.
- A status register containing flags such as ready, busy, or error.
- A control register containing commands and configuration values.
- Temporary buffers for holding data during a transfer.
A device driver is kernel software that knows how to use a controller. An application requests an operation through a system call. The kernel selects the driver, and the driver writes commands to the controller's registers. The controller then communicates with the physical device.
The controller may use memory-mapped I/O, where device registers appear at memory addresses, or port-mapped I/O, where the CPU uses special instructions. In both cases, access is restricted to the kernel.
Polling, interrupts and DMA
Polling means that the CPU repeatedly reads a controller's status register until the device is ready. It is simple and predictable, but it wastes CPU time while waiting. Polling can be reasonable for very short waits or simple embedded systems.
With interrupt-driven I/O, the CPU starts an operation and continues another task. When the controller needs attention, it sends an interrupt request. The processor saves enough state to run an interrupt handler. The handler checks the device status, records the result, and wakes a waiting process or schedules further work.
Direct memory access (DMA) allows a controller, called a DMA controller, to copy a block of data directly between a device and main memory. The CPU supplies the memory address, transfer length, direction, and device information. The CPU then performs other work. When the transfer finishes, the controller usually raises one interrupt.
| Method | CPU behavior while waiting | Best fit | Main cost |
|---|---|---|---|
| Polling | Repeatedly checks status | Short, predictable waits | Wasted CPU cycles |
| Interrupts | Runs other tasks | Infrequent events | Handler and context-switch overhead |
| DMA | Sets up a block transfer | Large data blocks | Setup and memory-coherence concerns |
A common sequence is: the driver starts the device, the controller transfers data, the controller interrupts the CPU, and the driver verifies completion. For DMA, the operating system must also handle cache consistency or use hardware support such as an IOMMU.
Exercise
A network controller receives a 64 KB packet buffer. Which technique is usually preferable: polling every byte, interrupt-driven transfer for every byte, or DMA with one completion interrupt? Why?
Show answer
DMA is preferable because it transfers the block without CPU copying. One completion interrupt avoids the large overhead of polling or handling an interrupt for every byte.