What happens when an interrupt is taken

When a device, timer or other source raises an interrupt and it is enabled, the CPU saves the address of the next instruction together with state such as the privilege mode and interrupt-enable bit, in dedicated registers or on the stack depending on the architecture. It obtains a vector number for the cause, reads the handler's start address from the interrupt vector table set up by the OS, and jumps there. The handler saves the registers it uses, services the device, clears the interrupt source, restores state, and returns with a special instruction such as RISC-V mret or sret, which restores the mode and return address atomically.

Interrupts, exceptions and polling

Interrupts are asynchronous: a timer tick or network packet arrives regardless of which instruction is running. Exceptions such as page faults, division by zero or a deliberate system call (RISC-V ecall) are synchronous, caused by a specific instruction. Polling, in which the CPU repeatedly reads device status, is the alternative to interrupts. Interrupts are better when events are rare or the CPU should sleep, but each costs state saving and a jump, so at very high event rates, as in fast networking, batched polling can be more efficient.

Priorities, masking and latency

An interrupt controller arbitrates simultaneous requests, often with a priority encoder, and with nesting a higher-priority interrupt can preempt a lower-priority handler, as in the Arm Cortex-M NVIC. Software can mask interrupts, but a non-maskable interrupt (NMI) is reserved for serious events such as uncorrectable memory errors or watchdog timeouts. Worst-case interrupt latency is set mostly by the longest section that runs with interrupts disabled, so operating systems split handling into a short urgent part and deferred work done later with interrupts enabled. PCI Express devices signal interrupts by memory writes (MSI) instead of dedicated wires.

Pitfalls in embedded code

Handlers should be short: no printf or waiting loops, just moving data to a buffer or setting a flag for the main loop. Variables shared with a handler must be declared volatile, or the compiler may read them only once. Read-modify-write updates like reg |= mask can lose a bit changed by a handler in between, and an 8-bit microcontroller reading a 32-bit counter can see half-updated bytes; disabling interrupts briefly fixes both. A periodic timer interrupt, for example 16 MHz divided by 16 counting to 1000 for 1 ms, provides the system tick.

Try it: questions about Interrupt

What is the Arm WFI (Wait For Interrupt) instruction used for?

  1. ADisabling all interrupts to speed up processing
  2. BWaiting for a Wi-Fi signal to arrive
  3. CStopping the CPU in a low-power state while waiting for an interrupt when idle, resuming when one arrives
Show answer and explanation

Answer: C. Stopping the CPU in a low-power state while waiting for an interrupt when idle, resuming when one arrives

Waiting in an empty loop when idle keeps the CPU executing instructions and consuming power. Executing WFI puts the CPU into a low-power state, for example by stopping its clock, doing nothing until an interrupt arrives. It is used in OS idle loops and in microcontroller main loops waiting for the next interrupt. x86 has a similar HLT instruction.

While the main code updates a register bit with "reg |= mask;" (read-modify-write), an interrupt handler changes a different bit of the same register. Which pairs the possible problem with a fix correctly?

  1. AThe handler's change is lost when main writes back; disable interrupts during the update or use dedicated per-bit set/clear registers
  2. BThe register is permanently damaged; replace the microcontroller
  3. CNothing happens; no fix is needed
Show answer and explanation

Answer: A. The handler's change is lost when main writes back; disable interrupts during the update or use dedicated per-bit set/clear registers

"reg |= mask" is three operations: read the register, OR, and write back. If an interrupt arrives right after the read and the handler changes another bit, main writes back a value based on the stale read, wiping out the handler's change. Disabling interrupts during the operation, or using set and clear registers where only the bits written as 1 change, makes it a single write and avoids the problem.

FAQ

What is the difference between an interrupt and an exception?

An interrupt is asynchronous and comes from outside the instruction stream, such as a timer or I/O device. An exception is synchronous, triggered by executing a particular instruction, such as a page fault, illegal instruction or system call.

What is an interrupt vector table?

It is a table, set up by the OS or firmware, that maps each interrupt or exception number to the start address of its handler. The CPU uses it to jump to the right routine for each cause.

Why should an interrupt handler be kept short?

While a handler runs, interrupts of the same or lower priority are usually held off and the main program is stalled, so a long handler increases latency for everything else. Heavy work is deferred to a task or main loop that runs with interrupts enabled.

Questions that mention Interrupt (23)

Interrupt appears in 25 practice questions across 6 topics, including Embedded Systems & Firmware, Power Management, Logic circuits.

  1. A timer counts a 16 MHz clock divided by 16 with a prescaler and raises an interrupt every 1000 counts. What is the interrupt interval?
  2. What should be avoided inside an interrupt handler?
  3. When the program above is compiled with optimization, led_on() may never be called even after the timer interrupt fires. Which pairs the cause with the fix correctly?
  4. On an 8-bit microcontroller, a timer interrupt increments the 32-bit variable ticks, and the main code reads ticks to compute elapsed time. Why is a wildly wrong value occasionally read?
  5. Why is it bad practice to kick (reset) the watchdog timer unconditionally inside a 1 ms timer interrupt handler?
  6. On a microcontroller with interrupt priorities, a higher-priority interrupt occurs while a lower-priority handler is running. What generally happens (assuming nesting is allowed)?
  7. When is the output of a 2-input OR gate 0?
  8. Which circuit outputs, in binary, the number of the highest-priority input when several inputs are 1 at the same time?
  9. When several open-drain outputs are connected to one signal line with a pull-up resistor, how does the line behave?
  10. Which CPU mechanism temporarily suspends the current task to give priority to handling requests such as those from external devices?
  11. Which mechanism transfers data directly between peripherals and memory without going through the CPU?
  12. Which describes a spinlock?
  13. What property does a lock-free algorithm have?
  14. What is the idea behind the tickless kernel used in Linux and other OSes?
  15. What is commonly used to wake a battery-powered device from sleep at a set time?

Books on CPUs and architecture Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary