What a CPU does, step by step

Video 017 of 140 in Virtualization Foundations, in the unit “CPUs: sockets, cores, threads and caches”. 3:30, with English and Hindi audio.

Every program you run is, at the bottom, a long list of tiny instructions. A chip called the CPU carries them out. To understand how a server spends its time, it helps to follow one instruction through the chip. This article takes a simple example from an online parcel tracker and walks through the three steps the CPU repeats for every instruction it runs.

What you will learn

Why one instruction is the place to start

Picture a customer of Harbor Point Logistics clicking Track my order on the company's web portal. A server receives that click. A program on that server runs thousands of instructions to find the parcel and build the page. The CPU runs every one of them.

The question is simple: what does the CPU do with one single instruction? The answer is the same for every instruction, whatever the program is for.

The three steps the CPU repeats

A register is the CPU's own scratch paper. It is far smaller than memory, but the CPU reads and writes it almost instantly.

How the CPU knows where to read next

The CPU must know where to read next. It keeps a number called the program counter, which holds the memory address of the next instruction. After each instruction, the counter moves on to the next place. Most of the time, a program runs in a straight line.

Sometimes an instruction says jump, for example to repeat a loop or to skip a block of code. Then the counter is set to a new place instead of moving on.

How to see how busy the CPU is on a live machine

A loop without an exit makes the CPU fetch, decode and execute the same instructions forever. A busy CPU is not always broken, though. It may be doing real work, and that is normal under load. The top tool shows how much of the processor each program uses.

Show the busiest programs once, in batch mode
top -b -n 1 | head -12

Note: Each row is a running program. The %CPU column shows how busy each program keeps the CPU.

Terms

CPU
The central processing unit, the chip that carries out the instructions of a program.
Program counter
A number inside the CPU that holds the memory address of the next instruction.
Register
A tiny storage slot inside the CPU that holds the number it is working on right now.
Instruction
One small step in a program, such as adding two numbers or moving data, that the CPU carries out.

Check yourself

What are the three steps the CPU repeats for each instruction?

Fetch the instruction from memory, decode what it asks for, and execute the work, such as an add or a move.

What does the program counter hold?

The memory address of the next instruction. After each instruction it moves on, unless a jump sets it to a new place.

Does a busy CPU always mean something is broken?

No. A busy CPU may be doing real work under load. A loop with no exit is a problem, because the work never finishes.

Previous: A cloud machine instead of a laptop. Next: Sockets, cores and threads: what a CPU count means.