An interrupt is a signal delivered to the CPU telling it to pause the current task and deal with something more urgent.
The CPU spends almost all its time in the fetch-decode-execute cycle, working through whatever program is currently running. An interrupt breaks the loop so a more urgent task can be handled at once.
How interrupts are generated
Interrupts come from two sources:
- Hardware interrupts are raised by physical components when they need attention.
- The keyboard has been pressed.
- The mouse has been moved or clicked.
- The printer has run out of paper.
- A USB device has been plugged in.
- The power button has been pressed.
- Software interrupts are raised by running programs or by the OS itself.
- A program tries to divide by zero (an error).
- A program asks the OS to perform a service (e.g. open a file).
- Two programs collide on the same memory address.
- A program has stopped responding.
How the CPU handles an interrupt
The standard step-by-step sequence for handling an interrupt:
- The current fetch-decode-execute cycle finishes so the CPU is at a clean stopping point.
- The CPU saves the contents of its registers (PC, ACC and others) to a special area in RAM called the stack, so the original task can be resumed later.
- The CPU looks up the interrupt in the Interrupt Service Routine (ISR) table. The ISR is a small program that knows how to handle this particular kind of interrupt.
- The CPU runs the ISR; for example, it reads the keystroke from the keyboard buffer and passes it to the active program.
- When the ISR finishes, the CPU restores the saved register values from the stack.
- The CPU resumes the original task exactly where it left off.
Interrupt priorities
Not every interrupt is equally urgent. The OS assigns priorities:
- A power-failure interrupt is critical and must be handled immediately.
- A keyboard interrupt is high-priority (the user expects an instant response).
- A printer "out of paper" interrupt is less urgent than a CPU temperature warning.
If a higher-priority interrupt arrives while a lower-priority one is being handled, the CPU can save its state again and switch to the more urgent one (nested interrupts).
Why interrupts matter
Interrupts let the CPU respond instantly to outside events without having to constantly poll every device. They are what make multitasking feel responsive: every keystroke, mouse movement, network packet and disk-completion message reaches the CPU through an interrupt.