An interrupt is a signal to the processor that something needs attention. The processor pauses its current program, runs a short routine to deal with the event, then resumes exactly where it left off. Questions give you a scenario such as a key press, a printer fault or a power failure, and ask you to explain what happens.
This lesson follows comparing translators and uses the processor ideas from hardware and processing.
What happens, step by step?
- A source sends a signal: hardware (keyboard, printer, timer) or software (a program that needs a service).
- The processor checks for interrupts after finishing the current instruction in each fetch-decode-execute cycle.
- If an interrupt is waiting and its priority is high enough, the processor saves its state: the program counter and the register contents.
- It runs the interrupt service routine (ISR) that matches the signal.
- It restores the saved state and continues the original program from the next instruction.
If two interrupts arrive together, the one with the higher priority is serviced first. A power failure signal outranks a key press.
The cycle with an interrupt check can be written in pseudocode:
REPEAT
FETCH instruction at program counter
EXECUTE instruction
IF interrupt signal received AND its priority is high enough THEN
SAVE program counter and registers
RUN interrupt service routine
RESTORE program counter and registers
ENDIF
UNTIL program ends
Worked example: a key press during a calculation
A program has four instructions:
I1: A ← 2
I2: B ← A * 3
I3: C ← B + 1
I4: D ← C * 2
The user presses a key while I2 is executing. Trace the processor.
| Step | Action | A | B | C | D |
|---|---|---|---|---|---|
| 1 | Execute I1 | 2 | |||
| 2 | Execute I2: B = 2 × 3 | 2 | 6 | ||
| 3 | Interrupt check: key press waiting, so save state (next instruction is I3, A = 2, B = 6) | 2 | 6 | ||
| 4 | Run ISR: store the key in the keyboard buffer | 2 | 6 | ||
| 5 | Restore state, resume at I3 | 2 | 6 | ||
| 6 | Execute I3: C = 6 + 1 | 2 | 6 | 7 | |
| 7 | Execute I4: D = 7 × 2 | 2 | 6 | 7 | 14 |
The final answer is D = 14, which is the same as with no interrupt.
This is the point of the save and restore steps: the interrupted program is not harmed. Check: 2 × 3 = 6, 6 + 1 = 7, 7 × 2 = 14. You can step through a similar loop in the restricted pseudocode trace trainer.
The mistake to watch for
Mistaken answer: “The processor stops running the program until the user stops pressing keys.”
This treats the interrupt as a permanent stop, and it leaves out the saved state. Another version says the processor keeps asking the keyboard whether a key has been pressed. That is polling, and the point of an interrupt is that the device signals the processor instead.
A full answer names the signal, says the current instruction finishes, says the state is saved, names the routine and says the program resumes.
Check yourself
1. A printer runs out of paper while a document is printing. Explain how an interrupt is used.
Show answer
The printer sends an interrupt signal to the processor. The processor finishes its current instruction and saves its state. It runs a routine that tells the user the printer needs paper. Then it restores its state and continues the other program.
2. A key press and a power-failure warning arrive at the same time. Which is handled first, and why?
Show answer
The power-failure warning, because it has a higher priority. The processor may need to save work before power is lost. The key press can wait.
3. Give one thing the processor must save before running an interrupt service routine, and say why.
Show answer
The program counter (or the register contents). Without it, the processor would not know which instruction to run next, or what values the interrupted program was using, when it comes back.
Where this leads next
Now move from reacting to events to the maintenance tools on a computer, in identify a utility function. Later, the software and systems practice set mixes interrupts with the other lessons.
Writing a clean sequence of steps is a skill our teachers can coach directly in online one-to-one Computer Science tuition.