CIE AS & A Level Computer Science, section 4.1 (Central Processing Unit (CPU) architecture)
What an interrupt is
An interrupt is a signal to the processor that something needs immediate attention. The processor stops the current task, deals with the event, and then carries on.
Possible causes
- Hardware: a key pressed or mouse clicked, a printer out of paper, a power failure, a device finishing a task.
- Software: an error such as division by zero or an invalid instruction, or a program asking the OS for a service.
- Timer: a clock signal when a process has used its time slice.
- I/O: a device signals that data has been transferred.
Applications
Interrupts let the processor respond to events without constantly checking devices (polling). They support multitasking and handling of input and output.
Detecting an interrupt
The processor checks for interrupts at the end of each fetch-execute cycle (after the execute stage). If one is found, and it has a high enough priority, it is handled.
How an interrupt is handled
- The processor finishes the current instruction.
- The contents of the registers (including the PC) are saved, usually on a stack, so the task can resume later.
- The Interrupt Service Routine (ISR) for that type of interrupt is loaded (the PC is set to its start).
- The ISR runs.
- The saved register values are restored and the interrupted task continues.
If several interrupts occur, priorities decide which is dealt with first. A higher-priority interrupt can interrupt a lower-priority ISR.
Interrupt versus polling
| Interrupt | Polling | |
|---|---|---|
| Processor | works until signalled | repeatedly checks the device |
| Efficiency | efficient | wastes processor time |
Common mistake: saying an interrupt "stops the program for good". The program resumes after the ISR because its registers are saved and restored. Also, interrupts are detected at the end of the cycle, not in the middle of an instruction.
Exam tip: describe the sequence: interrupt detected at the end of the F-E cycle, registers saved, ISR run, registers restored, original task resumes. Give an example cause.
Check yourself
Give two possible causes of an interrupt.
Any two: key pressed, printer out of paper, division by zero, timer signal, I/O finished.
Why are the register contents saved when an interrupt occurs?
So that the interrupted program can resume from exactly where it stopped.
When does the processor check for interrupts?
At the end of each fetch-execute cycle.