Skip to content
CurriculumLesson 24 ยท I/O
Intermediate~16 min

Polling, Interrupts & DMA

A device may be slower than the CPU by orders of magnitude. The question is who waits: the CPU, an interrupt handler, or a DMA controller.

In plain English

Polling is asking are we there yet. An interrupt is the driver telling you. DMA is a helper carrying the bags while you do other work.

By the end you will be able to

  • Compare polling, interrupt-driven I/O, and DMA.
  • Trace a DMA transfer from setup to completion.
  • Explain when each technique is appropriate.
  • Know the CPU cost of each approach.
01

Polling

With polling, the CPU repeatedly reads a status register until the device is ready, then moves the data. It is simple and predictable, but it wastes CPU cycles while waiting. Polling can be reasonable for very fast devices or when the CPU has nothing else to do.

lesson-24.osOS
while ((inb(PORT_STATUS) & READY) == 0) {
    /* spin */
}
data = inb(PORT_DATA);
02

Interrupt-driven I/O

With interrupts, the CPU starts the device and continues other work. When the device is ready, it raises an interrupt. The handler reads or writes the data. This frees the CPU during the wait, but every transfer has interrupt overhead. High-rate devices can generate too many interrupts, which is where DMA helps.

03

DMA

A DMA controller is programmed with a source, destination, and length. It moves data directly between the device and memory while the CPU does other work. When the transfer finishes, the controller raises an interrupt. The CPU is involved at setup and completion, not for every byte. This is how disks and network cards achieve high throughput without saturating the CPU.

lesson-24.osOS
dma.source = device_buffer;
dma.destination = memory_buffer;
dma.length = 4096;
dma.start();

/* CPU does other work */

on DMA_complete_interrupt():
    process(memory_buffer);

Key takeaways

  • Polling wastes CPU cycles while waiting.
  • Interrupt-driven I/O frees the CPU during the wait.
  • DMA moves bulk data without per-byte CPU work.
  • DMA uses a completion interrupt.
  • The right choice depends on device speed and transfer size.

Common pitfalls

  • Assuming DMA removes all CPU involvement; setup and completion still cost cycles.
  • Using polling for a slow device while other work is available.
  • Forgetting that interrupt handlers should be short.

Kernel notes

  • Compare the three techniques on CPU utilization.
  • List the DMA setup fields.
  • Know when polling is actually acceptable.

Quick check

0 / 2 answered

1. Which technique moves data directly between a device and memory without per-byte CPU copying?

2. What is the main downside of polling?