Skip to content

44. Interrupts & Exceptions

Both divert the CPU from what it's doing, but from different sources: an interrupt is triggered externally (hardware or software), asynchronously — it can happen any time. An exception is triggered by the CPU itself, synchronously, at a specific instruction (divide-by-zero, page fault, invalid opcode).

Types of interrupts: hardware (external device signals — keyboard, disk, network, timer) and software/traps (a program deliberately triggers one to make a system call — int 0x80 on old x86, syscall on x86-64).

Types of exceptions: faults (recoverable — page fault → OS loads the page and retries; divide-by-zero → SIGFPE), traps (intentional, used for debugging — breakpoints), and aborts (unrecoverable — a double fault or hardware machine-check, leading to a kernel panic/BSOD).

Handling sequence: finish current instruction → check interrupt line → save state → (temporarily disable further interrupts) → look up the handler in the IDT (Interrupt Descriptor Table) → jump to the ISR → restore state, re-enable interrupts, resume.

Priority/masking: NMI (Non-Maskable Interrupt, e.g. hardware failure) can never be disabled; ordinary maskable interrupts can be disabled temporarily (CLI/STI) during critical sections. Priority order: NMI > hardware IRQ > software interrupt.

Interrupt vs polling: polling repeatedly asks "are you done?" (wastes CPU); interrupts let the CPU do other work and get notified when the device is actually ready.

Hardware Interrupt Exception Software Interrupt
Triggered byExternal deviceCPU itselfProgram (syscall)
TimingAsyncSyncSync
ExampleTimer, keyboardPage fault, div/0System call