While your program sits waiting for a file, is the CPU idle too? Picture an office with a single clerk: the moment one visitor leaves to fetch a document, the clerk calls the next one in.
Before the details, the big picture: what actually happens when a computer runs a program, and what each part does. One sentence matters most here — the CPU is always running somebody's code. Step through the diagram below.
The CPU computes and executes instructions; memory holds the code and data of running programs; disks, networks and keyboards are devices. The CPU knows nothing on its own — it must fetch instructions from memory.
A program file on disk is dead data. Running it means the OS loads it into memory, prepares its memory space, and points the CPU at the first instruction. From then on the CPU executes it line by line.
If programs could drive devices or read each other's memory directly, chaos. So only the OS may do those things and programs ask for them. The OS also decides who gets the CPU and for how long.
"My program stopped waiting for the disk" and "the CPU is idle" are entirely different statements. The moment your program stalls, the OS puts another program on the CPU. The CPU truly rests only when nothing is runnable.
Keep your eye on the label inside the CPU box: Program A, then the kernel on a system call, then Program B while A waits, then the kernel again on an interrupt, and idle only when nothing can run. That label is never empty.
A computer has three main parts: the CPU that computes, memory holding the running programs, and devices such as disks. Look inside memory — several programs are loaded at once. But there is only one CPU, so only one of them executes at this instant.
Try it in the simulator
Terms in this chapter
The CPU is always running someone's code — your program stalling is not the CPU idling.
The remaining problem
So how exactly does the CPU run one line of code? Next we go down to individual instructions →