Operating Systems Module 7: Storage and files Lesson 7.1 12 min

Talking to devices

Disks, keyboards and network cards are far slower than the CPU. See three ways to wait for them, polling, interrupts and DMA, and how much CPU time each one wastes.

Your program calls read() and asks for 64 KB from a disk. The CPU can run an enormous number of instructions in the time the disk takes to find the first byte. If the CPU simply stood and waited, nearly all of the machine would be wasted. How does the operating system talk to something that slow without wasting the CPU?

This lesson is about that question. The answer has three parts: how the CPU reaches a device at all, three ways of waiting for it, and the layers of kernel code that make read() end up at the right device.

How the CPU reaches a device

A device is not wired into your program. It sits behind a controller, a small chip with a few registers that the CPU can read and write, like memory cells with side effects. A typical controller has three kinds:

  • a status register: is the device busy? is the data ready? did something fail?
  • a command register: write a command here (“read block 7”) and the device starts working.
  • a data register or buffer: where the data comes out, or goes in.

The CPU reaches these registers in one of two ways. With memory-mapped I/O, the registers appear at certain physical addresses, and an ordinary load or store to that address talks to the device. With port-mapped I/O, special CPU instructions (in and out on x86) address a separate numbered space of ports. Both are only allowed in kernel mode (Lesson 1.3). A user program cannot poke at a controller. It asks the kernel, and the kernel’s code for that particular device does the poking.

That code is a driver: the kernel’s per-device code. It knows the device’s registers and its quirks, and it offers the rest of the kernel a simple, uniform face (“read this block”, “send this packet”). Because every disk driver offers the same face, the file system above it does not need to care which disk it is on.

Three ways to wait

Suppose the kernel has written a read command into a device. The data will arrive later, in pieces. How does the CPU find out?

To keep the sums easy, the simulator below uses an invented model, with time counted in ticks. It is not a real device, and the numbers are made up, but the shapes it produces are real.

  • Writing a command costs the CPU 1 tick (2 for DMA, because it also writes an address and a length).
  • The device needs 40 ticks to get ready, then produces one 8 KB chunk every 6 ticks. A 64 KB read is 8 chunks.
  • Copying a chunk from the device into RAM takes the CPU 1 tick.
  • Taking an interrupt costs the CPU 2 ticks (saving state, jumping to the handler, returning).

1. Polling

The simplest way: the CPU reads the status register again and again until it says “ready”. This is polling, or busy waiting. Watch what the CPU lane does.

Polling: read 64 KB by asking again and again

  1. The CPU writes the command The CPU writes a read command into the device's command register. That takes 1 tick. The device starts working at once. From here the CPU has two choices: keep asking the device whether it is ready, or go and do something else. Polling is the first choice.
    Time
    1 ticks
    CPU is
    command
    Chunks in RAM
    0 of 8
    CPU time on this read
    1 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  2. Poll, poll, poll The CPU reads the device's status register in a tight loop: ready yet? ready yet? Nothing else runs, so for the 46 ticks until the first chunk is ready the CPU does no useful work at all. It is busy, but only busy waiting.
    Time
    47 ticks
    CPU is
    polling
    Chunks in RAM
    0 of 8
    CPU time on this read
    47 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  3. Chunk 1 is ready: the CPU copies it The status register finally says ready. The CPU copies 8K from the device into RAM, one tick. Then it goes back to polling.
    Time
    48 ticks
    CPU is
    copying
    Chunks in RAM
    1 of 8
    CPU time on this read
    48 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  4. Chunk 2 is ready: the CPU copies it The CPU went back to polling and the next chunk became ready. The CPU copies 8K from the device into RAM, one tick. Then it goes back to polling.
    Time
    54 ticks
    CPU is
    copying
    Chunks in RAM
    2 of 8
    CPU time on this read
    54 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  5. Chunk 3 is ready: the CPU copies it The CPU went back to polling and the next chunk became ready. The CPU copies 8K from the device into RAM, one tick. Then it goes back to polling. The red hatched blocks are the CPU asking "ready yet?" and getting "no".
    Time
    60 ticks
    CPU is
    copying
    Chunks in RAM
    3 of 8
    CPU time on this read
    60 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  6. Chunk 4 is ready: the CPU copies it The CPU went back to polling and the next chunk became ready. The CPU copies 8K from the device into RAM, one tick. Then it goes back to polling.
    Time
    66 ticks
    CPU is
    copying
    Chunks in RAM
    4 of 8
    CPU time on this read
    66 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  7. Chunk 5 is ready: the CPU copies it The CPU went back to polling and the next chunk became ready. The CPU copies 8K from the device into RAM, one tick. Then it goes back to polling.
    Time
    72 ticks
    CPU is
    copying
    Chunks in RAM
    5 of 8
    CPU time on this read
    72 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  8. Chunk 6 is ready: the CPU copies it The CPU went back to polling and the next chunk became ready. The CPU copies 8K from the device into RAM, one tick. Then it goes back to polling.
    Time
    78 ticks
    CPU is
    copying
    Chunks in RAM
    6 of 8
    CPU time on this read
    78 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  9. Chunk 7 is ready: the CPU copies it The CPU went back to polling and the next chunk became ready. The CPU copies 8K from the device into RAM, one tick. Then it goes back to polling.
    Time
    84 ticks
    CPU is
    copying
    Chunks in RAM
    7 of 8
    CPU time on this read
    84 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  10. Chunk 8 is ready: the CPU copies it The CPU went back to polling and the next chunk became ready. The CPU copies 8K from the device into RAM, one tick. That was the last chunk.
    Time
    90 ticks
    CPU is
    copying
    Chunks in RAM
    8 of 8
    CPU time on this read
    90 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  11. read() returns All 64K are in RAM after 90 ticks, and the CPU spent every one of them on this read. Polling is simple and has no extra delay, but the CPU can do nothing else meanwhile.
    Time
    90 ticks
    CPU is
    polling
    Chunks in RAM
    8 of 8
    CPU time on this read
    90 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0

The whole read took 90 ticks, and the CPU was busy for all 90 of them. Busy, but not doing useful work: almost all of that time went into asking “ready yet?”. Polling has a good side, though. There is no extra delay: the CPU notices the data the moment it is ready, and there is nothing to set up.

2. Interrupts

The CPU does not have to ask. It can write the command and go and run another process, and the device can tap the CPU on the shoulder when it has something. That tap is an interrupt (Lesson 1.2). The process that asked for the data is marked waiting (Lesson 2.2), so the scheduler runs someone else, process B in the picture.

Press play and see how the CPU lane changes. The bottom pane shows polling for the same read.

Interrupts: B runs while the device works

  1. The CPU writes the command, the process blocks The CPU writes the read command (1 tick). The process cannot continue until the data arrives, so the kernel marks it waiting (Lesson 2.2) and the scheduler runs another process, B. The CPU will not look at the device again until the device interrupts it.
    Time
    1 ticks
    CPU is
    command
    Chunks in RAM
    0 of 8
    CPU time on this read
    1 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  2. B runs while the device works For the 46 ticks until the first chunk is ready, the CPU runs B. This time is free: the read is using none of it.
    Time
    47 ticks
    CPU is
    other work
    Chunks in RAM
    0 of 8
    CPU time on this read
    1 ticks
    CPU free for other work
    46 ticks
    Interrupts
    0
  3. Interrupt 1: chunk 1 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    50 ticks
    CPU is
    interrupt
    Chunks in RAM
    1 of 8
    CPU time on this read
    4 ticks
    CPU free for other work
    46 ticks
    Interrupts
    1
  4. Interrupt 2: chunk 2 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    56 ticks
    CPU is
    interrupt
    Chunks in RAM
    2 of 8
    CPU time on this read
    7 ticks
    CPU free for other work
    49 ticks
    Interrupts
    2
  5. Interrupt 3: chunk 3 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    62 ticks
    CPU is
    interrupt
    Chunks in RAM
    3 of 8
    CPU time on this read
    10 ticks
    CPU free for other work
    52 ticks
    Interrupts
    3
  6. Interrupt 4: chunk 4 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    68 ticks
    CPU is
    interrupt
    Chunks in RAM
    4 of 8
    CPU time on this read
    13 ticks
    CPU free for other work
    55 ticks
    Interrupts
    4
  7. Interrupt 5: chunk 5 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    74 ticks
    CPU is
    interrupt
    Chunks in RAM
    5 of 8
    CPU time on this read
    16 ticks
    CPU free for other work
    58 ticks
    Interrupts
    5
  8. Interrupt 6: chunk 6 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    80 ticks
    CPU is
    interrupt
    Chunks in RAM
    6 of 8
    CPU time on this read
    19 ticks
    CPU free for other work
    61 ticks
    Interrupts
    6
  9. Interrupt 7: chunk 7 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    86 ticks
    CPU is
    interrupt
    Chunks in RAM
    7 of 8
    CPU time on this read
    22 ticks
    CPU free for other work
    64 ticks
    Interrupts
    7
  10. Interrupt 8: chunk 8 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. That was the last chunk.
    Time
    92 ticks
    CPU is
    interrupt
    Chunks in RAM
    8 of 8
    CPU time on this read
    25 ticks
    CPU free for other work
    67 ticks
    Interrupts
    8
  11. The process is ready again The last chunk is in RAM at tick 92. The driver wakes the waiting process (waiting to ready, Lesson 2.2) and read() can return. There were 8 interrupts, one per chunk.
    Time
    92 ticks
    CPU is
    other work
    Chunks in RAM
    8 of 8
    CPU time on this read
    25 ticks
    CPU free for other work
    67 ticks
    Interrupts
    8

With interrupts, the CPU spent only 25 ticks on this read, and 67 ticks were free for process B. The read itself finished in 92 ticks: slightly later than polling (90), because every interrupt costs a few ticks of its own. That is the trade. Interrupts give up a little speed on this one read to give the CPU back to everything else.

But there are 8 interrupts for 8 chunks. Each one costs the same 2-tick overhead plus the copy, however small the chunk. For a slow device that is a good deal. For a very fast one it can become a problem, as you will see shortly.

3. DMA

Look again at what the CPU still does in the interrupt version: it copies every chunk from the device into RAM, one tick each, and it takes an interrupt for each. Copying bytes is dull work that the CPU does not need to do.

DMA (direct memory access) hands that job to a DMA controller. The CPU tells it where in RAM the data should go and how much, and starts the device. From then on, the device writes straight into RAM over the memory bus. The CPU is interrupted once, at the end.

DMA: one setup, one interrupt

  1. The CPU programs the DMA controller Besides the command, the CPU writes where in RAM the data should go and how much (2 ticks in all). After that the device has everything it needs to move the data itself, a job done by a **DMA controller** (direct memory access). The process blocks and B runs.
    Time
    2 ticks
    CPU is
    setup
    Chunks in RAM
    0 of 8
    CPU time on this read
    2 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  2. B runs while the device prepares The device is getting its first chunk ready. The CPU is running B.
    Time
    42 ticks
    CPU is
    other work
    Chunks in RAM
    0 of 8
    CPU time on this read
    2 ticks
    CPU free for other work
    40 ticks
    Interrupts
    0
  3. Chunks flow into RAM by themselves The device writes each finished chunk straight into RAM over the memory bus. The CPU does not copy anything and is not interrupted: 4 of 8 chunks are in RAM already, and B has never stopped.
    Time
    66 ticks
    CPU is
    other work
    Chunks in RAM
    4 of 8
    CPU time on this read
    2 ticks
    CPU free for other work
    64 ticks
    Interrupts
    0
  4. The last chunk lands in RAM All 8 chunks are in RAM. The CPU has not been involved since it set up the transfer.
    Time
    90 ticks
    CPU is
    other work
    Chunks in RAM
    8 of 8
    CPU time on this read
    2 ticks
    CPU free for other work
    88 ticks
    Interrupts
    0
  5. One interrupt: the transfer is complete Now the DMA controller raises a single interrupt. The handler (2 ticks) wakes the waiting process, and read() returns at tick 92. Compare the CPU time spent on the read with polling: DMA needs the CPU only to start and to finish.
    Time
    92 ticks
    CPU is
    interrupt
    Chunks in RAM
    8 of 8
    CPU time on this read
    4 ticks
    CPU free for other work
    88 ticks
    Interrupts
    1

The same 64 KB arrived in 92 ticks, the same as with interrupts, but the CPU spent only 4 ticks on the read: 2 to set it up and 2 for the one interrupt at the end. The other 88 ticks went to process B. DMA does not make the device faster. It frees the CPU from the work of moving the data, which is why every modern disk, network card and graphics card uses it.

When polling wins

Interrupts are not always better. An interrupt has a fixed cost, so if the device produces data very quickly, the interrupts can pile up. Here the device makes a chunk every tick, but each interrupt takes 3 ticks to handle (2 for the interrupt, 1 for the copy).

A fast device: the interrupts cannot keep up

  1. The CPU writes the command, the process blocks The CPU writes the read command (1 tick). The process cannot continue until the data arrives, so the kernel marks it waiting (Lesson 2.2) and the scheduler runs another process, B. The CPU will not look at the device again until the device interrupts it.
    Time
    1 ticks
    CPU is
    command
    Chunks in RAM
    0 of 8
    CPU time on this read
    1 ticks
    CPU free for other work
    0 ticks
    Interrupts
    0
  2. B runs while the device works For the 41 ticks until the first chunk is ready, the CPU runs B. This time is free: the read is using none of it.
    Time
    42 ticks
    CPU is
    other work
    Chunks in RAM
    0 of 8
    CPU time on this read
    1 ticks
    CPU free for other work
    41 ticks
    Interrupts
    0
  3. Interrupt 1: chunk 1 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    45 ticks
    CPU is
    interrupt
    Chunks in RAM
    1 of 8
    CPU time on this read
    4 ticks
    CPU free for other work
    41 ticks
    Interrupts
    1
  4. Interrupt 2: chunk 2 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    48 ticks
    CPU is
    interrupt
    Chunks in RAM
    2 of 8
    CPU time on this read
    7 ticks
    CPU free for other work
    41 ticks
    Interrupts
    2
  5. Interrupt 3: chunk 3 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    51 ticks
    CPU is
    interrupt
    Chunks in RAM
    3 of 8
    CPU time on this read
    10 ticks
    CPU free for other work
    41 ticks
    Interrupts
    3
  6. Interrupt 4: chunk 4 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    54 ticks
    CPU is
    interrupt
    Chunks in RAM
    4 of 8
    CPU time on this read
    13 ticks
    CPU free for other work
    41 ticks
    Interrupts
    4
  7. Interrupt 5: chunk 5 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    57 ticks
    CPU is
    interrupt
    Chunks in RAM
    5 of 8
    CPU time on this read
    16 ticks
    CPU free for other work
    41 ticks
    Interrupts
    5
  8. Interrupt 6: chunk 6 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    60 ticks
    CPU is
    interrupt
    Chunks in RAM
    6 of 8
    CPU time on this read
    19 ticks
    CPU free for other work
    41 ticks
    Interrupts
    6
  9. Interrupt 7: chunk 7 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. Then B carries on until the next one.
    Time
    63 ticks
    CPU is
    interrupt
    Chunks in RAM
    7 of 8
    CPU time on this read
    22 ticks
    CPU free for other work
    41 ticks
    Interrupts
    7
  10. Interrupt 8: chunk 8 is ready The device raises its interrupt line. The CPU saves B's state, jumps to the driver's interrupt handler, which copies 8K into RAM, and returns to B. The interrupt costs 2 ticks and the copy 1. That was the last chunk.
    Time
    66 ticks
    CPU is
    interrupt
    Chunks in RAM
    8 of 8
    CPU time on this read
    25 ticks
    CPU free for other work
    41 ticks
    Interrupts
    8
  11. The process is ready again The last chunk is in RAM at tick 66. The driver wakes the waiting process (waiting to ready, Lesson 2.2) and read() can return. There were 8 interrupts, one per chunk.
    Time
    66 ticks
    CPU is
    other work
    Chunks in RAM
    8 of 8
    CPU time on this read
    25 ticks
    CPU free for other work
    41 ticks
    Interrupts
    8

Polling finishes first (50 ticks against 66), because it never pays the interrupt overhead. Interrupts still leave 41 ticks free, but the data takes longer to arrive, and under heavy load the CPU can end up doing nothing but taking interrupts. Real systems use both ideas: a Linux network driver, for example, takes an interrupt for the first packet of a burst, then switches to polling while packets keep coming, and goes back to interrupts when things go quiet. Very fast storage can be polled for the same reason.

So the rule of thumb is this: polling is better when the wait is shorter than the cost of an interrupt, or when the device is busy all the time anyway. Interrupts are better when the device is slow and the CPU has other work. DMA is better whenever there is a lot of data to move.

From read() down to the device

All of this happens at the bottom of a stack of kernel code. When a program calls read(), the request passes through several layers. Step through them, and watch where the process stops and the CPU moves on.

A read() going down the I/O stack

  1. read() enters the kernel Your program calls read() on a file descriptor. This is a system call (Lesson 1.3), so the CPU switches to kernel mode.
    Layer
    Your program
    Call
    read(fd, buf, 4096)
  2. The virtual file system finds the file The VFS is the kernel's common face for every kind of file system. It finds the open file behind the descriptor and passes the request on to whichever file system holds it.
    Layer
    System call and VFS
    CPU mode
    kernel
  3. The file system finds the blocks The file system turns a position in the file into block numbers on the device (Lesson 7.4). If the data is already in the page cache, it is copied to your buffer and the story ends here.
    Layer
    File system
    Question
    which disk blocks?
  4. The block layer queues the request The request joins a queue for the device. The I/O scheduler may reorder or merge requests here (Lesson 7.2).
    Layer
    Block layer and I/O scheduler
    Queue
    request waiting
  5. The driver programs the controller The driver writes the command, and a DMA address, into the controller's registers. Then the process is marked waiting and the scheduler runs something else.
    Layer
    Device driver
    Process
    waiting
  6. The device does the work The controller moves the data into RAM by DMA. Nobody is polling: the CPU is running other processes.
    Layer
    Disk controller and disk
    CPU
    running another process
  7. The device interrupts The controller raises an interrupt. The driver's handler runs briefly, finds out which request finished and wakes the waiting process.
    Layer
    Device driver
    Process
    ready
  8. read() returns The process runs again with the data in its buffer, as if the call had simply taken a long time.
    Layer
    Your program
    Returned
    4096 bytes

Each layer only talks to its neighbours. That is the same separation you saw in Lesson 1.1, and it is why a program does not need to change when you swap a disk for an SSD or a USB stick: only the driver at the bottom is different.

In the real world

Linux lets you look at all of this. These commands show the interrupt counts per device, the devices on the PCI bus with the driver the kernel picked for each, and the block devices the kernel sees:

Terminal
$ cat /proc/interrupts
$ lspci -k
$ lsblk

In /proc/interrupts, each row is an interrupt line, with one column of counts per CPU core and the name of the device that owns it. Run it twice a few seconds apart while you copy a big file or load a web page, and the counts for the disk or network card move. The -k flag of lspci adds a “Kernel driver in use” line under each device, which is the driver this lesson was about. Windows and macOS have the same structure with different names: Windows drivers plug into a stack of driver layers, and macOS has its own driver framework. :::

Quick check

Quick check

  1. A driver is…
  2. In the simulator, why did polling use all of the CPU's time during the read?
  3. What does DMA free the CPU from?
  4. When can polling beat interrupts?

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion