Press a key while a program is busy crunching numbers. The letter still appears straight away. But the CPU was in the middle of someone else’s work, so how did your key press get noticed?
To understand what the operating system does, you need a rough picture of the machine it drives. Not the electronics: just three ideas. The CPU runs a simple loop. Memory is a long row of numbered boxes. And devices, which are slow, can tap the CPU on the shoulder.
The CPU runs one loop, forever
A CPU (processor) does one thing over and over:
- Fetch: read the next instruction from memory. A register called the program counter (PC) holds its address.
- Decode: work out what the instruction means.
- Execute: do it. Add two numbers, copy a value, or jump somewhere else.
Then it moves on to the next instruction and starts again. Real CPUs do this billions of times a second and overlap the steps cleverly, but the idea is the same.
Memory (RAM) is a long array of numbered cells. The number of a cell is its address. Programs and their data both live there: an instruction is just a number that the CPU knows how to decode.
Here is a toy CPU with two registers, A and B, and 8 memory cells. The program in cells 0 to 4 adds the numbers in cells 6 and 7 and stores the answer in cell 5. The first instruction is split into its fetch, decode and execute steps. After that, each step runs a whole instruction.
Something else happens part way through: you press a key.
A toy CPU, and a key press in the middle
-
Ready to run
The program sits in memory cells, like any other numbers. The PC (program counter) holds 0: the address of the first instruction.
- PC
- 0
- IR
- (empty)
- A
- 0
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 0
-
Fetch
The CPU reads cell 0 (the PC) into the IR, the instruction register, and moves the PC on to 1.
- PC
- 1
- IR
- LOAD A, 6
- A
- 0
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 0
-
Decode
The CPU works out what LOAD A, 6 means: which operation, and which registers or cells it uses.
- PC
- 1
- IR
- LOAD A, 6
- A
- 0
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 0
-
Execute
LOAD A, 6 copies the number in cell 6 (2) into register A.
- PC
- 1
- IR
- LOAD A, 6
- A
- 2
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 1
-
Run LOAD B, 7
Fetch from cell 1, decode, execute: LOAD B, 7 copies the number in cell 7 (3) into register B.
- PC
- 2
- IR
- LOAD B, 7
- A
- 2
- B
- 3
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 2
-
A keyboard interrupt
The keyboard raises the interrupt line. The CPU checks this line after every instruction, so the instruction it was running has already finished.
- PC
- 2
- IR
- LOAD B, 7
- A
- 2
- B
- 3
- Interrupt line
- HIGH (keyboard)
- Saved PC
- –
- Instructions run
- 2
-
Save the PC, jump to the handler
The CPU saves the PC (2) so it can come back later, and jumps to the keyboard handler: code that belongs to the operating system.
- PC
- in the handler
- IR
- (empty)
- A
- 2
- B
- 3
- Interrupt line
- HIGH (keyboard)
- Saved PC
- 2
- Instructions run
- 2
-
Handler: step 1
Read the key code from the keyboard controller.
- PC
- in the handler
- IR
- (empty)
- A
- 2
- B
- 3
- Interrupt line
- low
- Saved PC
- 2
- Instructions run
- 2
-
Handler: step 2
Put it in the kernel's input buffer, for whichever program is waiting for a key.
- PC
- in the handler
- IR
- (empty)
- A
- 2
- B
- 3
- Interrupt line
- low
- Saved PC
- 2
- Instructions run
- 2
-
Return from the interrupt
The handler ends with a return-from-interrupt instruction. The CPU puts the saved PC (2) back, and the program carries on as if nothing had happened.
- PC
- 2
- IR
- (empty)
- A
- 2
- B
- 3
- Interrupt line
- low
- Saved PC
- 2
- Instructions run
- 2
-
Run ADD A, B
Fetch from cell 2, decode, execute: ADD A, B adds B to A: A is now 5.
- PC
- 3
- IR
- ADD A, B
- A
- 5
- B
- 3
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 3
-
Run STORE A, 5
Fetch from cell 3, decode, execute: STORE A, 5 copies register A (5) into cell 5.
- PC
- 4
- IR
- STORE A, 5
- A
- 5
- B
- 3
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 4
-
Run HALT
Fetch from cell 4, decode, execute: HALT stops the CPU. The program is finished.
- PC
- 5
- IR
- HALT
- A
- 5
- B
- 3
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 5
The program still got the right answer, 5, and it never noticed the interruption. That is the whole trick:
- An interrupt is a signal from a device to the CPU: “I need attention”. The CPU checks for one after every instruction.
- When it sees one, it saves the PC, then jumps to an interrupt handler: a short piece of code that belongs to the operating system.
- The handler deals with the device, then returns. The CPU puts the saved PC back and the interrupted program carries on.
Who wrote the handler, and who told the CPU where it is? The operating system did, when the computer started (Lesson 1.4). Every interrupt hands control to the OS for a moment.
Devices are slow. Very slow.
Why interrupts, instead of the CPU checking the keyboard over and over? Because devices are slow compared with the CPU, and checking is wasted work. The program below takes some typical, rounded access times and scales them up, so that one CPU register access takes one second:
#include <stdio.h>
int main(void) {
const char *name[] = {"CPU register", "RAM", "SSD", "Hard disk"};
double ns[] = {0.3, 100, 100000, 10000000}; /* typical access times, in nanoseconds */
for (int i = 0; i < 4; i++) {
double s = ns[i] / ns[0]; /* scale: a register access becomes 1 second */
printf("%-12s %10.1f ns -> ", name[i], ns[i]);
if (s < 60) printf("%.0f second\n", s);
else if (s < 86400) printf("%.1f minutes\n", s / 60);
else if (s < 31536000) printf("%.1f days\n", s / 86400);
else printf("%.1f years\n", s / 31536000);
}
return 0;
}
Output (one run)
CPU register 0.3 ns -> 1 second RAM 100.0 ns -> 5.6 minutes SSD 100000.0 ns -> 3.9 days Hard disk 10000000.0 ns -> 1.1 years
If reading a register took a second, fetching from RAM would take 5.6 minutes, an SSD read 3.9 days and a hard disk read 1.1 years. The real numbers vary a lot between machines, but the gaps are always huge. A CPU that sat waiting for a disk would waste almost all of its time. So the OS asks the device to start, runs something else, and lets the device interrupt when it is done.
The C program above is the first of many in this course. You do not need to know C yet: printf prints, %.1f means “a number with one decimal place”, and for loops over the four rows. The build of this course compiled and ran it, and the output under it is real.
The timer: how the OS gets the CPU back
Here is a scarier problem. A program has a bug: an endless loop. The CPU is running that loop. The operating system is just code too, so while the loop runs, the OS is not running at all. How can it ever stop the loop?
Watch a two-instruction loop with nothing to stop it:
An endless loop, and no timer
-
Ready to run
The program sits in memory cells, like any other numbers. The PC (program counter) holds 0: the address of the first instruction.
- PC
- 0
- IR
- (empty)
- A
- 0
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 0
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 1.
- PC
- 1
- IR
- ADDI A, 1
- A
- 1
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 1
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 1
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 2
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 2.
- PC
- 1
- IR
- ADDI A, 1
- A
- 2
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 3
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 2
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 4
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 3.
- PC
- 1
- IR
- ADDI A, 1
- A
- 3
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 5
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 3
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 6
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 4.
- PC
- 1
- IR
- ADDI A, 1
- A
- 4
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 7
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 4
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 8
-
And so on, forever
The program never stops, and nothing ever takes the CPU away from it. The operating system has had no chance to run at all.
- PC
- 0
- IR
- JMP 0
- A
- 4
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 8
The loop could go on for ever, and nothing else would ever run. The fix is a piece of hardware: a timer that raises an interrupt at a fixed rate, whatever the running program is doing. Same loop, with a timer that fires after every 3 instructions:
The same loop, with a timer
-
Ready to run
The program sits in memory cells, like any other numbers. The PC (program counter) holds 0: the address of the first instruction.
- PC
- 0
- IR
- (empty)
- A
- 0
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 0
- Times the OS ran
- 0
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 1.
- PC
- 1
- IR
- ADDI A, 1
- A
- 1
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 1
- Times the OS ran
- 0
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 1
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 2
- Times the OS ran
- 0
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 2.
- PC
- 1
- IR
- ADDI A, 1
- A
- 2
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 3
- Times the OS ran
- 0
-
The timer goes off
The timer raises the interrupt line, as it does at a fixed rate. The CPU checks this line after every instruction, so the instruction it was running has already finished.
- PC
- 1
- IR
- ADDI A, 1
- A
- 2
- B
- 0
- Interrupt line
- HIGH (timer)
- Saved PC
- –
- Instructions run
- 3
- Times the OS ran
- 0
-
Save the PC, jump to the handler
The CPU saves the PC (1) so it can come back later, and jumps to the timer handler: code that belongs to the operating system.
- PC
- in the handler
- IR
- (empty)
- A
- 2
- B
- 0
- Interrupt line
- HIGH (timer)
- Saved PC
- 1
- Instructions run
- 3
- Times the OS ran
- 1
-
Handler: step 1
The kernel is running now. It can let the loop carry on, or stop it and run another program.
- PC
- in the handler
- IR
- (empty)
- A
- 2
- B
- 0
- Interrupt line
- low
- Saved PC
- 1
- Instructions run
- 3
- Times the OS ran
- 1
-
Return from the interrupt
The handler ends with a return-from-interrupt instruction. The CPU puts the saved PC (1) back, and the program carries on as if nothing had happened.
- PC
- 1
- IR
- (empty)
- A
- 2
- B
- 0
- Interrupt line
- low
- Saved PC
- 1
- Instructions run
- 3
- Times the OS ran
- 1
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 2
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 4
- Times the OS ran
- 1
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 3.
- PC
- 1
- IR
- ADDI A, 1
- A
- 3
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 5
- Times the OS ran
- 1
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 3
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 6
- Times the OS ran
- 1
-
The timer goes off
The timer raises the interrupt line, as it does at a fixed rate. The CPU checks this line after every instruction, so the instruction it was running has already finished.
- PC
- 0
- IR
- JMP 0
- A
- 3
- B
- 0
- Interrupt line
- HIGH (timer)
- Saved PC
- –
- Instructions run
- 6
- Times the OS ran
- 1
-
Save the PC, jump to the handler
The CPU saves the PC (0) so it can come back later, and jumps to the timer handler: code that belongs to the operating system.
- PC
- in the handler
- IR
- (empty)
- A
- 3
- B
- 0
- Interrupt line
- HIGH (timer)
- Saved PC
- 0
- Instructions run
- 6
- Times the OS ran
- 2
-
Handler: step 1
The kernel is running now. It can let the loop carry on, or stop it and run another program.
- PC
- in the handler
- IR
- (empty)
- A
- 3
- B
- 0
- Interrupt line
- low
- Saved PC
- 0
- Instructions run
- 6
- Times the OS ran
- 2
-
Return from the interrupt
The handler ends with a return-from-interrupt instruction. The CPU puts the saved PC (0) back, and the program carries on as if nothing had happened.
- PC
- 0
- IR
- (empty)
- A
- 3
- B
- 0
- Interrupt line
- low
- Saved PC
- 0
- Instructions run
- 6
- Times the OS ran
- 2
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 4.
- PC
- 1
- IR
- ADDI A, 1
- A
- 4
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 7
- Times the OS ran
- 2
-
Run JMP 0
Fetch from cell 1, decode, execute: JMP 0 sets the PC to 0, so the next instruction comes from cell 0.
- PC
- 0
- IR
- JMP 0
- A
- 4
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 8
- Times the OS ran
- 2
-
Run ADDI A, 1
Fetch from cell 0, decode, execute: ADDI A, 1 adds 1 to A: A is now 5.
- PC
- 1
- IR
- ADDI A, 1
- A
- 5
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 9
- Times the OS ran
- 2
-
The timer goes off
The timer raises the interrupt line, as it does at a fixed rate. The CPU checks this line after every instruction, so the instruction it was running has already finished.
- PC
- 1
- IR
- ADDI A, 1
- A
- 5
- B
- 0
- Interrupt line
- HIGH (timer)
- Saved PC
- –
- Instructions run
- 9
- Times the OS ran
- 2
-
Save the PC, jump to the handler
The CPU saves the PC (1) so it can come back later, and jumps to the timer handler: code that belongs to the operating system.
- PC
- in the handler
- IR
- (empty)
- A
- 5
- B
- 0
- Interrupt line
- HIGH (timer)
- Saved PC
- 1
- Instructions run
- 9
- Times the OS ran
- 3
-
Handler: step 1
The kernel is running now. It can let the loop carry on, or stop it and run another program.
- PC
- in the handler
- IR
- (empty)
- A
- 5
- B
- 0
- Interrupt line
- low
- Saved PC
- 1
- Instructions run
- 9
- Times the OS ran
- 3
-
Return from the interrupt
The handler ends with a return-from-interrupt instruction. The CPU puts the saved PC (1) back, and the program carries on as if nothing had happened.
- PC
- 1
- IR
- (empty)
- A
- 5
- B
- 0
- Interrupt line
- low
- Saved PC
- 1
- Instructions run
- 9
- Times the OS ran
- 3
-
And so on, forever
The program never stops, but the timer keeps handing control back to the operating system every 3 instructions.
- PC
- 1
- IR
- (empty)
- A
- 5
- B
- 0
- Interrupt line
- low
- Saved PC
- –
- Instructions run
- 9
- Times the OS ran
- 3
Now the operating system gets the CPU back three times in nine instructions, even though the loop never gives it up. Real timers fire hundreds or thousands of times a second. Each time, the kernel can decide to let the program carry on, or to pause it and run another. That decision is scheduling, and it is what Module 3 is about. Without a timer, there is no fair sharing of the CPU at all.
One more thing to notice: the CPU itself does not know about “programs” or “fairness”. It just runs instructions and jumps when the interrupt line goes up. Everything else is software, in the kernel.
Quick check
Quick check
-
The CPU checks for interrupts after every instruction. The handler belongs to the operating system.
-
Without a timer, a program that never gives up the CPU would keep it for ever.
-
Each step down is many times slower than the one before, which is why the CPU never sits waiting for a disk.
-
Saving and restoring the PC is what makes an interrupt invisible to the program.
Recap
- The CPU runs a fetch, decode, execute loop. The program counter says where the next instruction is.
- Memory is a numbered array. Programs and data both live there.
- Devices are very slow compared with the CPU, so they interrupt when they need attention. The CPU saves the PC, runs an OS handler, and returns.
- A hardware timer interrupts at a fixed rate, so the OS always gets the CPU back, even from an endless loop.
Next: if any program could run any instruction, it could switch the timer off. The CPU’s two modes stop that.
Join the discussion