Interrupt Handling Process
What you'll learn: How a computer system responds to an interrupt from the moment it's detected until normal execution resumes.
The Interrupt Journey
An interrupt signals the CPU to pause what it's doing and handle something urgent. But how does the system actually process this signal? The interrupt handling process follows a precise sequence of steps to ensure nothing gets lost and execution can resume cleanly.
The Five-Step Sequence
1. Interrupt Detection
The CPU checks for pending interrupts at the end of each instruction cycle. When one is detected, the CPU finishes the current instruction before responding.
2. Context Saving
Before jumping to handle the interrupt, the CPU must save its current state—the context. This includes:
- The program counter (where execution was)
- CPU registers
- Processor status flags
Think of it like bookmarking your page before answering the phone—you need to know exactly where to return.
3. Handler Execution
The CPU jumps to the interrupt handler (also called an interrupt service routine or ISR). This is specialized code that responds to the specific interrupt—reading keyboard input, acknowledging a timer tick, or handling a hardware error.
4. Acknowledge and Clear
The handler typically acknowledges the interrupt to the device or interrupt controller, clearing the signal so it doesn't trigger again immediately.
5. Context Restoration
Finally, the saved context is restored: registers reloaded, program counter reset. Execution resumes exactly where it left off, as if nothing happened.
The Analogy
Imagine you're reading a book (executing a program). Your phone rings (interrupt). You place a bookmark (save context), answer the call (handler), hang up, then return to your exact page and continue reading (restore context).
Key Takeaway: Interrupt handling is a save-pause-handle-restore cycle that lets the CPU respond to urgent events while preserving the state of interrupted programs.