Interrupt Priority and Masking
You'll learn how the CPU decides which interrupt to handle first and how to protect critical code from being interrupted.
The Problem: Too Many Interrupts at Once
Imagine you're working on an important task when your phone rings, a text arrives, and someone knocks on your door—all at the same time. You can't respond to everything instantly, so you need a system to decide what's most urgent.
The same challenge exists in computers. Multiple devices (keyboard, disk, network) might request the CPU's attention simultaneously through interrupts. Without priority rules, chaos would ensue.
Interrupt Priority Levels
CPUs assign each interrupt source a priority level. When multiple interrupts arrive:
- The highest-priority interrupt is handled first
- Lower-priority interrupts must wait
- A high-priority interrupt can even preempt (interrupt) the handling of a lower-priority one
For example: a critical hardware failure interrupt has higher priority than a routine keyboard input.
Interrupt Masking: Protecting Critical Code
Sometimes the OS needs to execute atomic operations—sequences of instructions that must complete without interruption (like updating shared data structures). If an interrupt occurred mid-operation, data could become corrupted.
Masking temporarily disables certain (or all) interrupts during these critical sections. Think of it like putting your phone on "Do Not Disturb" mode while signing an important document.
The CPU provides instructions to set/clear interrupt mask bits. Masked interrupts are held pending until unmasked, then processed according to priority.
This ensures that critical operations complete atomically—without being torn apart by asynchronous interrupt handling.
Key Takeaway: Interrupt priority determines which interrupt wins when multiple arrive simultaneously, while interrupt masking allows the OS to temporarily disable interrupts to guarantee critical code sections execute atomically.