Sub topic 2
Polling, interrupts and DMA
This chapter explains the three ways an operating system moves data between a device and memory. It covers when each one is used and how to compare their CPU cost, which is a common GATE question. It assumes you know what a device controller and its registers are (from the previous chapter). It also assumes you know what an interrupt is, and you can do simple ratio arithmetic.
The core problem
A device is much slower than the CPU, and its timing is not under the CPU's control. A disk or network card finishes its work at an unpredictable moment. The OS therefore needs a way to learn that a device is ready and a way to move the data. The three techniques differ in who notices readiness and who moves the bytes.
| Technique | Who detects readiness | Who moves the data | CPU cost |
|---|---|---|---|
| Polling (programmed I/O) | CPU, by reading a status register in a loop | CPU | Highest, the CPU is tied up for the whole transfer |
| Interrupt-driven I/O | Device, by raising an interrupt | CPU, inside the interrupt handler | One interrupt per unit of data |
| DMA | Device and DMA controller | DMA controller | One setup and one interrupt per block |
Polling
With polling, the CPU repeatedly reads the device's status register until a "ready" or "done" bit is set. Then it reads or writes the data register. The handshake for one byte looks like this:
- The CPU reads the status register until the busy bit is clear.
- The CPU writes a command and sets the command-ready bit.
- The device sees the command and sets its busy bit.
- The device does the work and clears the busy bit.
- The CPU reads the result from the data register.
Busy-waiting wastes CPU cycles when the device is slow, because the CPU does nothing useful while it spins. Polling is the best choice in two cases:
- The device is fast and the wait is shorter than the cost of an interrupt (context save, handler entry, context restore).
- The system is a simple embedded one with nothing else to run.
High-speed network drivers sometimes switch to polling under heavy load. At very high packet rates, interrupts can consume the whole CPU, a problem called interrupt livelock.
Interrupt-driven I/O
Here the CPU starts the I/O and goes on running other work. When the device is ready, it raises a signal on the interrupt-request line. The CPU finishes the current instruction and saves its state. It then jumps to the interrupt service routine (ISR) chosen through the interrupt vector. The ISR moves the data and returns.
The cost is that every interrupt carries overhead: saving and restoring registers, and flushing the pipeline and possibly caches. If a device interrupts once per byte or word, that overhead is paid many times. At high data rates it can swamp the CPU. Interrupts are still the right choice for slow or irregular devices such as keyboards, mice and serial ports.
Interrupts have priorities. A higher-priority interrupt can preempt a lower-priority ISR, and maskable interrupts can be disabled during critical sections. Non-maskable interrupts (for example, hardware faults) cannot be disabled.
Direct memory access
For bulk transfers, such as disk or network blocks, moving each word through the CPU is wasteful. A DMA controller is a separate piece of hardware that transfers a whole block between the device and main memory without CPU involvement in each word.
The CPU programs the DMA controller with:
- the memory address (source or destination),
- the byte count,
- the direction (read or write),
- the device.
The controller then takes over the bus and moves the data. The CPU is interrupted once, when the whole block is done.
Bus sharing modes
The CPU and the DMA controller share the memory bus, so they compete for it. There are three common modes:
- Burst mode: the controller keeps the bus until the whole block is transferred. This is fast but blocks the CPU from memory for the duration.
- Cycle stealing: the controller takes the bus for one cycle per word and then gives it back. The CPU slows down a little but never stalls for long.
- Transparent mode: the controller uses the bus only when the CPU is not using it. This has no CPU slowdown but is the slowest for the transfer.
In cycle-stealing questions, the CPU is slowed only for the cycles the DMA controller takes, not for the whole transfer.
Edge cases
- DMA works with physical addresses. The OS must pin the buffer pages so they are not swapped out or moved during the transfer, or use an IOMMU to translate device addresses.
- If the CPU cache holds a copy of the buffer, it can become stale after a DMA write. The OS must invalidate or flush the cache, or the hardware must keep it coherent.
- DMA does not remove interrupts. It reduces them from one per word to one per block.
Comparing CPU cost numerically
The standard method is to compute how much CPU time is spent per second of transfer. For interrupts, the number of interrupts per second is the data rate divided by the bytes handled per interrupt. For DMA, the CPU cost per block is the setup time plus the completion interrupt time, divided by the time the block takes to transfer.
def cpu_fraction_polling():
# The CPU spins for the whole transfer.
return 1.0
def cpu_fraction_interrupt(rate_bytes_per_s, bytes_per_interrupt, isr_s):
interrupts_per_s = rate_bytes_per_s / bytes_per_interrupt
return interrupts_per_s * isr_s
def cpu_fraction_dma(rate_bytes_per_s, block_bytes, setup_s, completion_isr_s):
block_time_s = block_bytes / rate_bytes_per_s
return (setup_s + completion_isr_s) / block_time_s
rate = 1_000_000 # 1 MB/s device
print(round(cpu_fraction_polling() * 100, 2)) # 100.0
print(round(cpu_fraction_interrupt(rate, 4, 2e-6) * 100, 2)) # 50.0
print(round(cpu_fraction_dma(rate, 4096, 10e-6, 10e-6) * 100, 2)) # 0.49The device delivers 250,000 four-byte words per second. With a 2 µs ISR, interrupts consume half the CPU. DMA moves a 4096-byte block in 4.096 ms and costs the CPU only 20 µs of that time, which is about 0.49%.
Choosing a technique
- Use polling for very fast devices, very short waits, or simple systems with no other work.
- Use interrupts for slow or sporadic devices, where the CPU can do other work between events.
- Use DMA for high-volume block transfers such as disks and network cards.
Real systems combine them. A disk read uses DMA for the data and one interrupt for completion.
Exercise
A device transfers data at 2 MB/s (take 1 MB = 10^6 bytes). With interrupt-driven I/O, it interrupts once per 4-byte word and each ISR takes 1 µs. With DMA, it moves 8192-byte blocks, and the CPU spends 30 µs per block on setup plus the completion interrupt. What fraction of CPU time does each method use?
Show answer
Interrupt-driven: 2,000,000 / 4 = 500,000 interrupts per second, times 1 µs = 0.5 s per second, so 50% of the CPU. DMA: a block takes 8192 / 2,000,000 = 4.096 ms, so the CPU cost is 30 µs / 4096 µs ≈ 0.0073, or about 0.73%.
Exercise
DMA transfers a block with one interrupt at the end. Why is the CPU still needed, and why must the buffer be pinned in memory?
Show answer
The CPU must program the DMA controller (address, count, direction) and handle the completion interrupt. The buffer must be pinned because the DMA controller uses physical addresses. If the OS swapped out or moved the page mid-transfer, the controller would read or write the wrong memory.