Skip to content
Tutorials Center
My learning

Topics

Lesson 51 of 66 ยท I/O and Disk Scheduling

I/O Hardware, Interrupts and DMA

Learn how an operating system talks to devices: ports and controllers, polling, interrupts, DMA, device drivers and the kernel I/O subsystem, with CPU-cost simulations in Python.

A computer that could only calculate would be useless. It must read keys, draw pixels, send packets and store files, and the machines that do this (keyboards, disks, network cards, displays) are wildly different from each other and thousands of times slower than the CPU. The operating system's I/O subsystem hides those differences behind a few simple interfaces and keeps the CPU busy with useful work while devices do their slow jobs.

In this lesson you meet the hardware vocabulary, the three ways a CPU can talk to a device (polling, interrupts and DMA) and the layered software that sits above them.

Devices, ports and controllers

A device connects to the machine through a port (a connection point such as USB), or a bus (shared wires such as PCIe). Each device has a controller, a small chip or circuit that operates the device and speaks the bus protocol. The controller exposes a handful of registers:

  • a status register (is it busy? is data ready? did an error occur?),
  • a control (command) register the CPU writes to start an action,
  • data-in and data-out registers or buffers that carry the bytes.

The CPU reaches those registers in one of two ways:

  • Port-mapped I/O: special instructions (in, out on x86) address a separate I/O port space.
  • Memory-mapped I/O: the registers appear at ordinary physical addresses, so normal loads and stores operate the device. This is how most modern devices, including graphics cards and NVMe disks, are driven.

Devices fall into broad classes: block devices (disks, SSDs: fixed-size blocks, random access), character devices (keyboards, serial ports: streams of bytes) and network devices (sockets).

Method 1: polling (programmed I/O)

The simplest approach: the CPU writes a command, then loops reading the status register until the device says "done" (this is also called busy-waiting). Each byte may need another round of the loop.

  • Pro: simple, and low latency when the device is nearly always ready.
  • Con: if the device is slow, the CPU burns billions of cycles doing nothing useful.

Method 2: interrupts

Instead of waiting, the CPU starts the operation and goes on running other processes. When the device finishes, its controller raises an interrupt request (IRQ) on a wire. The CPU stops what it is doing, saves its state, looks up the right interrupt handler in the interrupt vector table, runs it, and resumes the interrupted work.

Key details:

  • Interrupts are maskable (the CPU can postpone them in critical code) or non-maskable (hardware failure, always delivered).
  • They carry priorities, so a timer or disk interrupt can preempt a low-priority one.
  • Handlers are split in two: a short top half that acknowledges the device and does the minimum, and a bottom half (softirq, tasklet, deferred work) that finishes processing later with interrupts enabled.
  • Traps (software interrupts) use the same mechanism: a system call is a deliberate trap into the kernel.

An interrupt costs something: saving and restoring state and polluting caches takes microseconds. A network card receiving millions of packets per second can spend all its time in interrupts, which is why high-speed drivers use interrupt coalescing or switch to polling under load (Linux NAPI).

Method 3: Direct Memory Access (DMA)

For bulk transfers, even one interrupt per byte is far too expensive. A DMA controller moves a whole block between the device and main memory on its own, without involving the CPU for every byte.

  1. The driver tells the DMA controller the memory address, the byte count and the direction.
  2. The controller steals bus cycles to copy the data ("cycle stealing") or takes over the bus for a burst.
  3. When the whole block has been moved, the controller raises one interrupt.

The CPU's part shrinks from thousands of instructions to a couple of setup writes and one interrupt handler. A modern IOMMU also restricts which memory a device may reach by DMA, so a faulty or malicious device cannot overwrite the kernel.

sequenceDiagram
    participant CPU
    participant Driver
    participant DMA as DMA controller
    participant Disk
    CPU->>Driver: read(file, 4 KB)
    Driver->>DMA: address, count, direction
    DMA->>Disk: request block
    Note over CPU: runs other processes
    Disk-->>DMA: data
    DMA-->>CPU: copies into memory, then ONE interrupt
    CPU->>Driver: interrupt handler wakes the process

The CPU is free while the DMA controller and the disk work.

Counting the cost

Let us compare the three methods for reading a 4,096-byte block when the CPU runs 1 instruction per nanosecond.

Python CPU time: polling vs interrupts vs DMA

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Polling occupies the CPU for the entire wait. Per-byte interrupts are even worse because each interrupt is expensive. DMA uses a tiny fraction.

Simulating interrupt-driven I/O

This program runs a tiny scheduler where a process asks for I/O. With blocking and interrupts, another process fills the gap; with polling, the CPU stalls.

Python Overlapping computation with device time

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With interrupts the system finishes sooner because B and C run while A's device works.

Device drivers and the kernel I/O subsystem

A device driver is kernel code that knows the details of one kind of controller. It presents the kernel with a uniform interface (open, read, write, ioctl) so the layers above do not care whether the bytes come from an NVMe disk or a USB stick. Drivers are the biggest source of kernel bugs, which is why some systems run them in user space (see microkernels).

Above the drivers the kernel provides common services:

  • Scheduling of I/O requests (for disks, see disk scheduling),
  • Buffering, caching and spooling (next lesson),
  • Error handling (retry, report EIO),
  • Protection: user programs can only reach devices through system calls,
  • Naming (/dev/sda) and device reservation.

Software layers, from the top:

  1. User processes (read(fd, buf, n))
  2. Device-independent kernel I/O software (naming, buffering, protection)
  3. Device drivers
  4. Interrupt handlers
  5. Hardware controllers and devices

Blocking, non-blocking and asynchronous I/O

  • Blocking: the call does not return until the data is ready; the process sleeps. Easiest to program.
  • Non-blocking: the call returns at once with "nothing yet" and the program tries again or uses select/poll/epoll to wait on many descriptors.
  • Asynchronous: the program starts the operation and is notified later when it is complete (Linux io_uring, Windows overlapped I/O).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main drawback of polling?

    Polling keeps the CPU in a loop checking the status register. For slow devices that is time it could spend on other processes.
  2. 2. What does a DMA controller change compared with interrupt-per-byte I/O?

    The CPU only sets up the transfer and handles one completion interrupt instead of one per byte.
  3. 3. Why are interrupt handlers split into a top half and a bottom half?

    The top half acknowledges the device quickly. The slower processing runs later so other interrupts are not blocked.
  4. 4. Which is true of memory-mapped I/O?

    Memory-mapped I/O places controller registers in the address space, so regular memory instructions operate the device.
  5. 5. Which kind of I/O returns immediately and signals completion later?

    With asynchronous I/O the program continues and is notified when the operation finishes.

Summary

  • Devices connect through ports or buses and are operated through controller registers, reached by port-mapped or memory-mapped I/O.
  • Polling is simple but wastes CPU; interrupts let the CPU do other work until the device signals completion; DMA moves whole blocks and raises one interrupt.
  • Interrupt handlers have short top halves and deferred bottom halves.
  • Drivers hide device differences; the kernel adds scheduling, buffering, protection and error handling.
  • I/O can be blocking, non-blocking or asynchronous.

Next: Buffering, Caching and Spooling