Skip to content
CurriculumLesson 04 ยท Foundations
Intermediate~14 min

Interrupts: Hardware & Software

Interrupts are how the outside world gets the CPU's attention. A timer, a disk, or a bad instruction can all force the kernel to run.

In plain English

An interrupt is a tap on the shoulder: stop, handle this now, then go back.

By the end you will be able to

  • Distinguish hardware interrupts from software traps and exceptions.
  • Explain the role of the interrupt vector and handler.
  • Trace the save-handle-return sequence.
  • Know why some interrupts can be masked and others cannot.
01

Hardware interrupts

A hardware interrupt is a signal from a device or timer. It may arrive asynchronously, at any instruction boundary. The CPU finishes the current instruction, saves enough state to resume, switches to kernel mode, and jumps to the handler selected by the interrupt vector. A timer interrupt is the clock that makes preemptive scheduling possible.

02

Software interrupts, traps, and exceptions

A software interrupt or trap is raised by an instruction. A system call uses a trap deliberately. An exception is raised by the CPU when an instruction cannot complete, such as a divide-by-zero, page fault, or invalid opcode. Some exceptions are faults that can be fixed and restarted; others are fatal. The same vector mechanism dispatches them.

lesson-04.osOS
/* conceptual handler table */
vector[0]  -> divide_by_zero_handler
vector[14] -> page_fault_handler
vector[32] -> timer_handler
vector[80] -> system_call_handler
03

Saving state, masking, and returning

The handler must preserve the interrupted program's registers and status. The kernel may run the handler on a kernel stack. Maskable interrupts can be temporarily disabled to protect critical sections; non-maskable interrupts cannot be ignored. When the handler finishes, the kernel restores the saved state and returns to the interrupted instruction or schedules another process.

Key takeaways

  • Hardware interrupts are asynchronous device or timer signals.
  • Traps are deliberate software entries; exceptions are CPU-detected faults.
  • The interrupt vector selects the handler.
  • The kernel saves state, handles the event, and restores or reschedules.
  • Maskable interrupts can be disabled briefly; non-maskable ones cannot.

Common pitfalls

  • Thinking an interrupt is always an error.
  • Assuming the interrupted instruction is lost; it is usually resumed.
  • Disabling interrupts for too long and increasing latency.

Kernel notes

  • Give one example each of a hardware interrupt, a trap, and an exception.
  • Explain why a timer interrupt enables preemption.
  • Know what the vector table stores.

Quick check

0 / 2 answered

1. Which event is an asynchronous hardware interrupt?

2. What does the interrupt vector contain?