Interrupts vs Polling: How an ISR Runs, With Numbers
A microcontroller can find out that a byte has arrived in two ways. It can keep asking the peripheral, which is polling, or it can let the peripheral interrupt it, which runs an interrupt service routine (ISR) the moment the byte lands. Polling is simple and predictable but spends time asking; interrupts answer fast and cost nothing while nothing happens, but bring their own bugs. This page compares both on a worked example, walks through what the CPU does to run an ISR, and shows in real C why a flag shared with an ISR must be volatile.
Learning objectives
- Compare polling and interrupts by worst-case latency and CPU time
- List the steps from a peripheral event to the first instruction of the ISR, and back
- Explain why a flag shared with an ISR must be volatile, and show the failure
- Spot a race on a variable shared between main code and an ISR, and fix it
Prerequisites
- C: globals, loops and functions
- Part 1: how a UART receives a byte
Polling
A polling loop reads the peripheral's status register every P microseconds. A byte that arrives just after a poll waits a whole period, so the worst-case latency is P plus the time of the poll itself. Every poll costs CPU time, whether a byte came or not. Shorter periods answer faster and waste more.
Interrupts
With an interrupt, the peripheral raises a request line when the byte lands. The CPU finishes its current instruction and switches to the ISR, and when the ISR returns, the interrupted code continues as if nothing happened. Nothing is spent while no bytes arrive, but each interrupt pays a fixed entry and exit cost.
How an ISR runs
- 1. The event: the UART receives a byte and sets its receive flag. If its interrupt is enabled, it raises a request to the interrupt controller.
- 2. The controller marks the interrupt pending and checks that it is enabled and has a higher priority than what is running now.
- 3. The CPU finishes, or abandons and later restarts, the current instruction, and saves the context it needs to resume: on an Arm Cortex-M the hardware pushes eight registers, including the program counter and status register, onto the stack.
- 4. It reads the ISR's address from the vector table, an array of handler addresses at a known place in memory, and jumps there.
- 5. The ISR reads the byte, which also clears the UART's flag, stores it in a buffer, and returns. It does as little as possible.
- 6. The return restores the saved registers, and the interrupted code continues at the next instruction. If another interrupt is pending, the CPU may go straight to it.
Worked example: 200 bytes, four ways
A worked example makes it concrete: a CPU at 1 MHz, so one cycle is one microsecond, receiving 200 UART bytes over 199.6 ms, 500 to 1500 us apart. A poll costs 5 cycles, an interrupt 12 cycles in and 12 out, and handling a byte 20 cycles either way. These cycle counts are example values; a real core's entry time is in its manual.
| Scheme | Worst latency | Mean latency | CPU spent on the UART |
|---|---|---|---|
| Polling every 10 us | 14 us | 9.7 us | 52.00% |
| Polling every 100 us | 104 us | 54.5 us | 7.00% |
| Polling every 1000 us | 1004 us | 494.0 us | 2.50% |
| Interrupt | 12 us | 12.0 us | 4.41% |
Polling every 10 us comes close to the interrupt's latency but spends more than half the CPU asking. Polling every 1000 us is cheap but answers up to a millisecond late, which already loses bytes at 115200 baud (exercise 2). The interrupt answers in 12 us using 4.4 percent of the CPU. Polling still wins when events are very frequent, so that each interrupt's entry and exit would cost more than a poll, or when the code must be strictly predictable.
Why shared flags must be volatile
A flag that an ISR sets and main code waits for must be declared volatile. Without it the compiler may assume nothing else changes the variable inside the loop, read it once, and loop forever. A POSIX signal stands in for the interrupt here, so the program runs on any Linux machine.
/* Why a flag shared with an interrupt handler must be volatile. A POSIX signal stands in for the interrupt: an
* interval timer raises SIGALRM after 100 ms and the handler sets a flag that the main loop waits for. Built with
* -O2, the compiler may read a plain global once and loop forever; volatile makes it read the flag every time.
* gcc -O2 -o plain volatile_demo.c && timeout 2 ./plain
* gcc -O2 -DUSE_VOLATILE -o vol volatile_demo.c && timeout 2 ./vol */
#include <signal.h>
#include <stdio.h>
#include <sys/time.h>
#ifdef USE_VOLATILE
static volatile sig_atomic_t done = 0;
#else
static int done = 0;
#endif
static void on_alarm(int sig) { (void)sig; done = 1; }
int main(void) {
struct itimerval it = { {0, 0}, {0, 100000} }; /* one shot, 100 ms */
signal(SIGALRM, on_alarm);
setitimer(ITIMER_REAL, &it, 0);
while (!done) { } /* polling the flag the handler sets */
puts("the main loop saw the flag");
return 0;
}
plain int, gcc -O2:
exit status 124 (124: stopped by timeout after 2 s)
volatile sig_atomic_t, gcc -O2:
the main loop saw the flag
exit status 0The plain int version never saw the flag: at -O2 the compiler hoisted the read out of the loop, and timeout killed it after 2 seconds. The volatile version reads the flag on every pass and exits after 100 ms. volatile makes every access happen, but it does not make a read-modify-write atomic; exercise 3 shows that separate problem.
Common mistakes
- Forgetting volatile on a flag shared with an ISR. The code works at -O0 and hangs at -O2.
- Doing slow work in the ISR, such as printing or waiting. Other interrupts are held off for the whole time; set a flag or fill a buffer, and do the work in main.
- Not clearing the peripheral's interrupt flag. The ISR returns and is entered again at once, forever.
- Updating a shared variable from main with a read-modify-write while the ISR can change it. Disable the interrupt for those few instructions, or use an atomic operation.
- Polling a status register so slowly that the hardware buffer overflows between polls.
Quick check
1. What sets the worst-case latency of a polling loop?
The polling period: a byte that arrives just after a poll waits a whole period, plus the time of the poll that finds it.
2. Where does the CPU find the address of an ISR?
In the vector table: one entry per interrupt, each holding the handler's address.
3. Does volatile make count++ safe between main and an ISR?
No. volatile makes each access happen, but count++ is still a load, an add and a store, and the ISR can run in between. It needs the interrupt disabled around it, or an atomic operation.
Exercises
E1 (easy). With a 5 us poll, what is the longest polling period that keeps the worst-case latency within 50 us?
Worst case is the period plus the poll: 45 us.
E2 (medium). A UART at 115200 baud delivers a byte every 86.8 us into a one-byte receive register. Polled every 100 us, how many of 1000 back-to-back bytes are lost?
131: whenever two bytes arrive between polls, the second overwrites the first. Polling must be faster than one byte time, or the UART needs a FIFO, or an interrupt.
E3 (medium). main increments count 10 times; the ISR increments it 3 times, each time between main's load and store. What is count at the end?
10, not 13: each ISR update is overwritten by main's store of its stale value. Disabling the interrupt around main's increment, or an atomic increment, gives 13.
E4 (hard). The ISR writes bytes into a ring buffer and main drains one byte every 40 us. Bytes come in bursts of 12, one every 10 us. How many slots does the buffer need?
With 4 slots 25 bytes are lost and with 8 slots 9, because a burst arrives four times faster than main drains it. 16 slots still lose 1 in this run: by the end of a burst only 3 bytes have been drained, so a burst of 12 needs 12 - 3 = 9 slots plus whatever the previous burst left. Size the buffer for the longest burst, minus what drains during it, with margin.
Interview questions
- Q1. When would you choose polling over interrupts? Answer: when events are so frequent that interrupt overhead dominates, when timing must be strictly predictable, or in a simple loop that has nothing else to do.
- Q2. What should an ISR do and not do? Answer: clear the source, move the data into a buffer or set a flag, and return. No blocking, no long loops, no printing.
- Q3. What is interrupt latency, and what adds to it? Answer: the time from the event to the first ISR instruction: finishing the current instruction, saving context, fetching the vector, plus any time interrupts are disabled or a higher priority ISR is running.
Summary
- Polling: worst latency is the period plus the poll, and it costs CPU even when idle.
- Interrupts: fast answers and no idle cost, paid for with entry and exit time on every event.
- An ISR saves context, runs from the vector table, does the minimum and returns.
- Share flags with ISRs through volatile, and protect read-modify-write updates.
Related
Sources
Last verified
The volatile example was built and run with gcc 15.2 on Linux, and every other number was computed and checked, on 27 November 2026.