Skip to content

Volume ITechnology

2.7.1 — How the CPU Talks to Devices ​

A photo arrives at the upload service over the network. To the CPU, that photo is a long, slow event. A CPU core runs an instruction in a fraction of a nanosecond. A network packet takes microseconds to arrive, a disk read takes from tens of microseconds to milliseconds, and the whole upload takes seconds. If one CPU instruction took one second, an SSD read would take a few days, and a spinning disk's read would take about a year (1.6 has the full ladder).

So the real question of input and output is not "how do bytes move?". It is this: what does a very fast CPU do while a very slow device works, and how does it find out the device has finished? Here are the two possible answers, side by side.

ts
// polling-vs-waiting.ts — wait one second for a "device", two ways, and count the CPU used
function cpuMs(): number { const u = process.cpuUsage(); return (u.user + u.system) / 1000; }

let deviceDone = false;
setTimeout(() => { deviceDone = true; }, 1000);              // the "device" finishes after 1 s

const c0 = cpuMs();
const end = Date.now() + 1000;
let checks = 0;
while (Date.now() < end) { checks++; if (deviceDone) break; } // (1) polling: "done yet? done yet?"
console.log(`polling:  ${checks.toLocaleString()} checks, CPU used ${(cpuMs() - c0).toFixed(0)} ms`);

const c1 = cpuMs();
setTimeout(() => {                                            // (2) waiting: sleep until told
  console.log(`waiting:  1 wake-up, CPU used ${(cpuMs() - c1).toFixed(0)} ms`);
}, 1000);

// Prints:
// polling:  14,738,028 checks, CPU used 1031 ms
// waiting:  1 wake-up, CPU used 0 ms
  • (1) The first way asks "done yet?" in a tight loop. It asked almost fifteen million times, and burned a whole second of a CPU core doing nothing else. There is a second, sneakier problem. While this loop runs, Node cannot run anything else on this thread, including the timer that would set deviceDone. So the flag never flipped, and the loop only stopped because of its one-second limit. Polling on one thread can block the very notification it is waiting for.
  • (2) The second way asks to be woken when the time is up, and then does nothing. It used no measurable CPU at all. The thread slept, and the kernel woke it once.

The second way needs help from the hardware: a way for a device to tap the CPU on the shoulder. That tap is an interrupt. This page covers interrupts, how the kernel handles them, how data moves into memory without the CPU copying it, and the physical roads that connect devices to the CPU.

1. How the CPU gives a device a job ​

Every device, such as a disk controller or a network card, has a few device registers: small storage slots on the device itself. The CPU gives a device a job by writing to them: "read 8 blocks starting at block 91,442 into this memory address". It checks how a job is going by reading them: "status: busy".

On modern machines those registers are reached through ordinary memory addresses. The kernel arranges for certain physical addresses to belong not to RAM but to a device. Writing to such an address sends the value to the device instead of storing it in memory. This is called memory-mapped I/O. Only the kernel can reach those addresses, because they are mapped without the user permission bit (2.5.1). That is the hardware reason an ordinary program cannot talk to a device, and must make a system call instead (2.1.2).

The code in the kernel that knows a particular device's registers, and what to write into them, is that device's device driver (2.1.1). A driver for one network card knows nothing about another make of card. The rest of the kernel talks to every driver through the same small set of operations, which is how the VFS of 2.6.3 can treat every disk the same way.

2. Polling or interrupts ​

Once the job is given, the CPU must find out when it is done. There are two ways, the two halves of the opening demo.

  • Polling: the CPU reads the device's status register again and again in a loop, until it says "finished". It is simple, and it wastes the CPU completely during the wait. It is like standing at the microwave asking "ready?" every second.
  • Interrupts: the CPU gives the job and goes off to do other work, such as running another program. When the device finishes, it sends an electrical signal to the CPU. The CPU stops whatever it is doing, saves where it was, and jumps into the kernel to deal with it. It is like setting the microwave's timer and washing the dishes until it beeps.

Every general-purpose computer is interrupt-driven, because it lets the CPU do useful work during the long wait. This is the hardware underneath the blocked state of 2.2.1. A process that asked for data from the disk is set aside, using no CPU. When the disk's interrupt arrives, the kernel moves the process back to ready. It is also the same mechanism as the timer interrupt that makes preemptive scheduling possible (2.3.2). A timer is just a device whose only job is to interrupt the CPU on schedule.

3. What happens when an interrupt arrives ​

An interrupt is the third way into the kernel from the table in 2.1.2. Here is the sequence.

  1. The device raises an interrupt. Each device, or each queue on a device, has an interrupt number. On a PC the signal goes through an interrupt controller, which decides which CPU core should receive it.
  2. The CPU stops between two instructions. It saves the current instruction address and a few registers, switches to kernel mode, and looks up the interrupt number in a table the kernel set up at boot (2.1.3). That table gives the address of the kernel function for this interrupt, called the interrupt handler.
  3. The handler runs. It reads the device's status, acknowledges the interrupt so the device stops signalling, and records what happened.
  4. The CPU returns to exactly where it was, and the interrupted program continues as if nothing happened.

Top half and bottom half ​

There is a tension here. While an interrupt handler runs, it holds up whatever was interrupted, and on that core, often other interrupts too. So it must be very quick. But handling a finished disk read or a burst of network packets can be a lot of work: checking the data, passing it up through the file system or the network stack, waking the processes that were waiting.

Kernels solve this by splitting the work in two.

  • The top half is the interrupt handler itself. It does the bare minimum, immediately: acknowledge the device, grab the data or note where it is, and schedule the rest for later. It cannot sleep (2.4.2 explained why interrupt code may only use spinlocks).
  • The bottom half does the rest, a moment later, with interrupts enabled again. On Linux one kind of bottom half is called a softirq. When there is too much of this work to do immediately, a kernel thread called ksoftirqd takes it over, and you will see it in top on a busy network server (2.3.1 listed kernel threads).

On Linux, the file /proc/interrupts shows how many interrupts of each kind each core has handled. On a busy server you can watch the network card's numbers climb by tens of thousands a second.

When interrupts become too many ​

Interrupts beat polling when events are rare compared with the CPU's speed. But a fast network card can receive over a million packets a second. One interrupt per packet would leave the CPU doing nothing but entering and leaving interrupt handlers. This is called an interrupt storm.

So, under heavy load, network drivers switch to polling again. Linux's network stack does exactly this: after the first interrupt, it turns that card's interrupts off and polls the card in a loop, taking packets in batches, until the card has nothing left. Then it turns interrupts back on. Polling wins when there is always work waiting, and interrupts win when the waits are long. Good systems switch between the two.

4. DMA: do not make the CPU carry the bytes ​

There is a second waste to remove. A network card has received a 64 KB piece of a photo. Someone must copy those 64 KB from the card into RAM. If the CPU did it, reading each word from the device and writing it to memory, it would be tied up for the whole copy, doing nothing clever.

So machines include direct memory access, or DMA: the ability of a device to read and write RAM by itself, without the CPU. A modern network card or disk controller has this built in. The sequence becomes:

  1. The driver tells the device "put the incoming data at this physical memory address".
  2. The CPU goes back to running programs.
  3. The device copies the data into RAM on its own.
  4. Only when the whole transfer is finished does the device raise one interrupt to say "done".
CPUruns programsnetwork cardwith DMARAMbuffer for the photo1"put it here"2copies 64 KB itself3one interrupt: "done"
Figure 1 — Direct memory access. ① The driver tells the device where in RAM to put the data. ② The device copies it there on its own, while the CPU runs programs. ③ One interrupt reports that the whole transfer is finished.

The CPU is involved only at the start and the end. For the thousands of words in between, it is free. Interrupts and DMA together are why a machine can download a large file, play music and run a build all at once without the CPU drowning in copying.

Modern network cards go further. They keep a ring of buffers in RAM, set up in advance, and fill them one after another as packets arrive, raising an interrupt only now and then. The same ring idea reappears for programs in 2.7.3 as io_uring.

DMA has one security catch. A device that can write anywhere in RAM could, if it were malicious or buggy, overwrite the kernel. Modern machines have an IOMMU, a memory-management unit for devices, which gives each device its own address translation exactly as the MMU does for processes (2.5.1), so each device can only reach the memory the kernel allowed it.

5. The roads the bytes travel: buses, the chipset and PCIe ​

Interrupts and DMA describe who does the work. Underneath them is how a device and the CPU are physically connected. It explains why one SSD can be ten times faster than another that looks identical.

A bus is a set of shared wires that several parts use to talk. For decades, PC motherboards had two helper chips. The northbridge sat next to the CPU and handled the fast traffic: memory and the graphics card. The southbridge hung off the northbridge and handled the slow traffic: disks, USB, audio and the network. Every memory access went out of the CPU, through the northbridge and back, and that extra hop became a bottleneck as CPUs got faster.

So the northbridge moved inside the CPU. AMD put the memory controller on the processor chip in 2003, and Intel followed in 2008. Today the CPU talks to memory and to its fastest devices directly. One remaining helper chip, which Intel calls the platform controller hub and AMD simply calls the chipset, handles the slower devices. Phones and Apple's M-series Macs go further still: a system-on-chip puts the CPU, the graphics processor, the memory controller and most of the input and output on one piece of silicon.

The connection to fast devices is PCIe, PCI Express. Older buses were shared: every device on them took turns on the same wires, like cars on a single-lane road. PCIe instead gives each device its own point-to-point link, made of one or more lanes. A lane is two pairs of wires, one for each direction, so data flows both ways at once. A link can bundle lanes. An SSD typically gets 4, written x4. A graphics card gets 16, x16.

Each PCIe generation roughly doubles the speed of a lane.

PCIe generationOne laneAn x4 SSD
3.0about 1 GB/sabout 4 GB/s
4.0about 2 GB/sabout 8 GB/s
5.0about 4 GB/sabout 16 GB/s

This is where NVMe comes from. Older SSDs spoke a protocol called SATA, designed in the early 2000s for spinning disks, and reached the CPU through the chipset. SATA tops out at about 0.6 GB/s. NVMe SSDs speak a protocol designed for flash memory, directly over PCIe lanes. NVMe also allows many separate queues of requests, up to 65,535, so different CPU cores can each have their own queue without sharing a lock (2.4.2). SATA had one queue of 32. That is why the same-looking little stick can be ten times faster.

It is also why DMA matters so much. At 8 GB/s, a CPU copying the bytes itself would do nothing else. In a cloud data centre, the same PCIe links carry network cards that hand a slice of themselves directly to each virtual machine, which 14.1.2 covers.

What the interviewer will push on ​

"What is the difference between polling and interrupt-driven I/O?" They are checking whether you know the trade-off, not just the definitions. Polling wastes the CPU while waiting; interrupts let it work, and the device signals when it is done. The tell of depth is the reverse case: at very high event rates, such as a busy network card, interrupts themselves become the overhead, and Linux switches to polling in batches.

"What happens when an interrupt arrives?" The CPU finishes the current instruction, saves its place, switches to kernel mode and jumps to the handler listed in the interrupt table. The handler acknowledges the device and schedules the rest. Strong answers mention the top half and bottom half split, and why the top half must be quick and cannot sleep.

"What is DMA, and why does it matter?" The device copies data to or from RAM itself; the CPU only sets up the transfer and handles one completion interrupt. Without it, the CPU would spend its time copying. Bonus: the IOMMU limits where a device may write.

"Why is an NVMe SSD so much faster than a SATA SSD?" NVMe uses PCIe lanes directly instead of a protocol designed for spinning disks, and it has many deep queues instead of one queue of 32. Quote rough figures: SATA about 0.6 GB/s, PCIe 4.0 x4 about 8 GB/s.

"Why can't a user program talk to a device directly?" Device registers are reached through memory addresses the kernel maps without the user permission bit, so user-mode code faults if it tries. It must make a system call.

Something to volunteer that nobody asks for: the same ring of buffers that a network card fills by DMA is the idea behind io_uring, which gives programs shared rings with the kernel. Seeing that one design used at two levels shows you understand why it is fast: both sides work through memory, and nobody waits for anybody.

Recall

  • The CPU is millions of times faster than devices, so the question is what it does during the wait. It gives jobs through device registers, reached by memory-mapped I/O that only the kernel's device drivers can touch.
  • Polling wastes the CPU (a whole core in the demo); interrupts let the device signal completion, and every general-purpose system is interrupt-driven. At very high rates, network drivers switch back to batched polling.
  • On an interrupt, the CPU saves its place, enters kernel mode and runs the interrupt handler from the kernel's table. The quick top half acknowledges the device; the bottom half (softirqs, ksoftirqd) does the rest later.
  • Direct memory access lets the device copy data to RAM itself, with one completion interrupt; the IOMMU limits where devices may write.
  • Fast devices connect over PCIe lanes, each generation doubling lane speed. NVMe SSDs speak a flash protocol directly over PCIe with many queues, reaching about 8 GB/s on PCIe 4.0 x4, against about 0.6 GB/s for SATA.

Self-test: Why did the polling loop in the demo never see the flag change? What does an interrupt handler do, and why must it be quick? Why does a fast network card sometimes stop using interrupts? What does the CPU do during a DMA transfer? Why can two SSDs of the same size and shape differ tenfold in speed?

Quiz Bank ​

FoundationalWhat is the difference between polling and interrupt-driven I/O?

With polling, the CPU repeatedly reads a device's status register in a loop until the device reports that it has finished, wasting cycles during the whole wait. With interrupt-driven I/O, the CPU gives the device its job and moves on to other work; when the device finishes, it raises an interrupt, which makes the CPU stop, run the kernel's interrupt handler for that device, and then continue. Interrupts are far better when waits are long, which is why every general-purpose system uses them, and they are the hardware basis of the blocked process state.

FoundationalWhat is DMA and why does it matter?

Direct memory access lets a device read and write RAM by itself, without the CPU copying the data. The driver tells the device where in memory to put or take the data, the device performs the whole transfer, and it raises a single interrupt when it is finished. Without DMA, the CPU would have to move every word of every transfer itself, which at modern speeds of gigabytes per second would leave it no time for anything else. An IOMMU restricts which memory each device may reach.

AppliedWhy are interrupt handlers split into a top half and a bottom half?

While an interrupt handler runs, it holds up the code it interrupted, and often other interrupts on that core, and it is not allowed to sleep. So it must finish very quickly. But handling a completed transfer can involve a lot of work, such as processing packets through the network stack and waking waiting processes.

The top half is the handler itself: it does only the urgent minimum, such as acknowledging the device and noting what arrived, and schedules the rest. The bottom half does that rest shortly afterwards, with interrupts enabled. On Linux, bottom halves include softirqs, and the ksoftirqd kernel thread processes them when there is too much to do immediately.

InterviewInterrupts are better than polling, so why does Linux poll network cards under heavy load?

Interrupts win when events are rare compared with the CPU's speed, because the CPU does useful work between them. A fast network card can receive over a million packets a second. One interrupt per packet means the CPU spends most of its time entering and leaving interrupt handlers, an interrupt storm, and can stop making progress.

So after the first interrupt, Linux disables that card's interrupts and polls it, processing packets in batches until the card's queue is empty, then re-enables interrupts. Polling is efficient when there is always work waiting, because no check is wasted. The general rule: interrupts for long or unpredictable waits, polling for constant work.

InterviewWhy is an NVMe SSD faster than a SATA SSD with the same flash chips?

The difference is the connection and the protocol. SATA was designed for spinning disks, connects through the chipset, tops out at about 0.6 GB/s, and has a single queue of up to 32 outstanding requests. NVMe connects directly to the CPU over PCIe lanes, typically four, at about 2 GB/s per lane on PCIe 4.0, giving about 8 GB/s, and supports thousands of queues with many requests each, so each CPU core can have its own queue without sharing a lock. The flash can be identical; NVMe simply lets the drive's parallelism reach the CPU.

StaffA high-traffic proxy shows one CPU core at 100 percent in kernel time, mostly in network softirq processing, while the other cores are idle. Explain and suggest fixes.

All the network card's receive work is landing on one core. The card raises interrupts on one queue, or all queues are steered to the same core, so that core runs every top half and every softirq bottom half for the incoming traffic. It saturates in kernel time, while the cores that run the proxy's user-space threads sit idle waiting for data it has not finished processing.

Fixes:

  1. Enable multiple receive queues on the card and spread their interrupts across cores. This feature is usually called receive-side scaling.
  2. Adjust which core each interrupt is sent to.
  3. Where the card has only one queue, have the kernel spread the packet processing to other cores in software. Check that the load-balancing of flows across queues actually distributes the traffic, since a few very busy connections can still land on one queue.

Scenario Drill ​

DrillA colleague writes a tiny upload helper that, after asking the disk to save a photo, loops checking whether the save has finished, to return the result as fast as possible. On a busy server, the whole machine slows down. Using this page, explain why and what to do instead.

The helper is polling. Every waiting request keeps a CPU core busy asking "done yet?" in a loop, millions of times a second, so CPU time that other requests and programs need is spent asking a question whose answer is "not yet". With many uploads waiting at once, several cores are burned this way. If the helper runs on a single-threaded event loop like Node's, it is even worse: the loop blocks the very event that would report completion, as the opening demo showed.

Instead, let the kernel and the device do the waiting. Issue the save and let the thread sleep, or in Node use the asynchronous file API, so the event loop is free and the completion arrives as an event once the device's interrupt has been handled. The request is answered just as soon, because the wake-up happens the moment the device finishes, and no CPU is spent in between.

Next: the kernel can now be told when a device has finished. But how should a program wait? Should its thread simply sleep in read until data arrives, or should it ask "is there anything yet?" and carry on? Page 2.7.2 shows that this one choice decides how a whole server is built, and why the answer that seems simplest stops working at about ten thousand connections.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Volume I — Technology
Part 0 — Retention & Notation
Part 1 — Foundations of Computation
Part 2 — Operating Systems
2.1 The Kernel
2.2 Processes
2.3 Threads & Scheduling
2.4 Concurrency Under the Hood
2.5 Memory Management
2.6 File Systems
2.7 Input/Output
2.8 Linux for Engineers
2.9 Windows & macOS in Practice
Part 3 — Languages, Compilers & Runtimes
3.6 JavaScript & V8
3.7 TypeScript
3.8 Node.js Internals
3.13 Python & CPython
3.14 FastAPI & Production Python
Part 4 — Data Structures & Algorithms
4.0 Classic Problems — Before NeetCode
4.4 Arrays & Hashing — Problems
4.5 Two Pointers
4.6 Sliding Window
4.8 Stack — Problems
4.9 Linked List — Problems
4.11 Binary Search — Problems
4.13 Trees
4.14 Tree — Problems
4.15 Trie — Problems
4.17 Heap — Problems
4.18 Backtracking — Problems
4.19 Graphs
4.20 Graph — Problems
4.21 Advanced Graph — Problems
4.23 1-D DP — Problems
4.24 2-D DP — Problems
4.26 Greedy — Problems
4.27 Interval — Problems
4.28 Math & Geometry — Problems
4.30 Bit Manipulation — Problems
Part 5 — Networking & the Web
5.3 IP
5.4 Transport
5.6 HTTP
Part 6 — Frontend Engineering
6.1 Browser Internals
6.2 HTML & CSS Mastery
6.3 JavaScript in the Browser
6.4 React, from First Component to Full Shop
6.5 Frameworks & Rendering
6.8 UI Engineering & Patterns
Part 7 — Databases & Storage
7.2 SQL Mastery
7.3 Storage Engine Internals
7.4 Transactions
7.5 NoSQL Taxonomy
7.8 Analytics & Big Data
Part 8 — Security, Identity & Cryptography
8.2 Cryptography
8.3 Protocols of Trust
8.4 Identity: AuthN & AuthZ
8.5 Application Security
8.6 Infrastructure Security
Part 9 — Software Design (LLD)
9.2 OOP Foundations
9.3 Design Principles
9.4 Design Patterns
9.5 Concurrency
9.6 API Design
9.7 LLD Interview Machines
9.9 Node/Express Production Playbook
Part 10 — Distributed Systems & HLD
10.7 Consistency & Consensus
10.8 Messaging & Microservices
10.14 Caching
Building blocks from the design seat
Part 11 — System Design Case Studies
Part 12 — AI & ML Engineering
12.1 What AI Engineering Is
12.2 The Math You Need
12.3 Classical Machine Learning
12.4 Data for ML
12.5 Deep Learning
12.6 Embeddings & Vector Search
12.7 Transformers & LLMs
12.8 Building with LLMs
12.9 Retrieval-Augmented Generation
12.10 Agents
12.11 Adapting Models
12.12 Production AI
12.13 Multimodal AI
12.14 AI Security & Governance
12.15 Case Studies & Capstone
Part 13 — Cloud, DevOps & Infrastructure
13.11 Git, from First Commit to Team Workflow
Part 14 — Containers: Docker & Kubernetes
14.1 Foundations
14.2 Docker
14.3 Kubernetes
14.4 Kubernetes on Azure
Part 15 — The Craft & Career
Nothing picked yet