Interrupts
An interrupt is a signal that tells the CPU, when something that must be handled first comes up while a program is running, to pause what it is doing and handle that first. Once the urgent work is done, the CPU goes back to where it stopped and continues what it was doing.
At a Glance
- When something more important than the current work comes up (I/O completion, an error, a higher-priority operation, etc.), the CPU has to handle it first and then come back to the original work.
- Analogy: it is like the doorbell ringing while you are reading a book.
- You put a bookmark in the page you were reading. (Save the current state)
- You open the door and take the package. (Handle the interrupt)
- You go back to the bookmark and keep reading. (Restore the state and resume)
- Without a doorbell, you would have to go to the front door every few minutes to see whether a package had arrived. This way of checking over and over is called polling, and it is compared with interrupts below.
Types of Interrupts
Interrupts are divided into three types depending on where they come from.
- External and internal interrupts are raised by hardware signals. An external interrupt is a signal sent by a device outside the CPU, and an internal interrupt happens when the CPU itself finds a problem while executing an instruction.
- A software interrupt is raised on purpose when a program executes a specific instruction.
External Interrupts
These are raised by factors outside the CPU, such as I/O devices, timing devices like timers, and the power supply.
- Power failure (a blackout, etc.)
- Machine error (hardware fault)
- External signals (a timer reporting that the set time has come, an operator pressing a key)
- I/O (keyboard input arrives, a disk read finishes)
Internal Interrupts (Exceptions)
These are raised inside the CPU when an invalid instruction or invalid data is used. Depending on the textbook, they are called traps or exceptions.
- Division by zero
- Overflow (a value goes beyond the range it can hold)
- Misuse of an instruction (an instruction that does not exist, an instruction without permission)
- Accessing a memory address that must not be accessed
Software Interrupts (SVC Interrupts)
These interrupts are raised when a program requests them directly with an instruction. SVC stands for Supervisor Call.
- They are raised when a program asks for something that needs the operating system's help, such as reading a file, running a new program or allocating memory. This is exactly how a system call works.
- For example, if you launch another program while using one, the operating system allocates resources to the new process and arranges for several processes to share the CPU (time-sharing).
Terminology note: the name "trap" is used differently in different textbooks. Some books call internal interrupts (exceptions) traps, while others call software interrupts that a program raises on purpose, like system calls, traps. In an interview, it is safest to explain them by "where and why it happened."
| Type | Cause | Examples |
|---|---|---|
| External interrupt | A hardware signal from a device outside the CPU | Keyboard input, disk read complete, timer, power failure |
| Internal interrupt (exception) | An error the CPU finds while executing an instruction | Division by zero, overflow, invalid instruction |
| Software interrupt | A program requests it directly with an instruction | System calls (reading a file, creating a process) |
How an Interrupt Is Handled
Main program running
│
▼
① Interrupt occurs ───────────────────────────┐
│ │
② Finish only the current instruction, then stop │
│ │
③ Temporarily save the status register, PC │
(next instruction address), etc. to the stack (put in a bookmark) │
│ │
④ Run the interrupt service routine (ISR) ◀─────────┘
(take the package)
│
⑤ Restore the values saved on the stack
│
▼
⑥ Resume the main program from where it stopped- An interrupt occurs while the main program is running.
- The CPU finishes only the instruction it is currently executing and stops the program.
- It temporarily saves the status register and PC (Program Counter, the address of the next instruction to run), among others, on the stack.
- They are saved temporarily because, after the interrupt is handled, the CPU must return exactly to the original work.
- It jumps to and runs the interrupt service routine (ISR), the dedicated code that handles that interrupt. Which ISR to run for each kind of interrupt is listed in a table called the interrupt vector table.
- When the ISR finishes, the saved values are restored.
- The main program resumes from where it stopped.
Here is the same process as a picture. It saves the return address (③), finds the location of the handling routine in the interrupt vector and jumps there (④), and when handling is done, it loads the return address again (⑦) and goes back to where it was (⑧).

Image source: gyoogle Tech Interview Notes, Interrupt
Saving and restoring state makes it similar to context switching, which swaps processes. In fact, the timer interrupt is the most common trigger for the operating system to switch processes.
Polling vs Interrupts
Without interrupts, the controller would have to keep checking the status to know when to do something. This is called polling. While polling, it cannot focus on its original work, so the downside is that many functions cannot be carried out properly.
To sum up, there are two ways for a controller (an MCU, a small computer chip such as a microcontroller) to notice input and decide what to do first (priority).
Polling
- The program uses instructions to keep reading the value of an input pin to detect changes.
- For priority too, it checks the interrupt request flags (marks a device raises to say "I have a request") one by one in software to find the highest-priority request and runs the matching service routine.
- It is slower than the hardware approach.
Interrupts
- The controller itself detects changes in hardware and performs the set action only when there is a change.
- When several devices request interrupts at the same time, the hardware decides the priority. There are two typical methods.
| Method | How it works | Analogy |
|---|---|---|
| Daisy Chain | Devices are connected in series in a single line in priority order. The CPU's acknowledge signal passes from the first device onward, so among the requesting devices, the one closest to the front (highest priority) is handled first | Asking people in a line, starting from the front, "Do you need something?" |
| Parallel priority | Each device has its own line, and a priority resolver circuit compares them at the same time to pick the highest request | Everyone holds up a number ticket at once and the smallest number is picked immediately |
Comparing the Two
| Item | Polling | Interrupts |
|---|---|---|
| Detecting changes | The program keeps checking directly | The hardware detects and reports it |
| CPU waste | Cannot do other work while checking | Handles things only when they happen |
| Response speed | Can be late by up to one check interval | Responds immediately |
| Requirements | Possible without special hardware | Needs hardware support |
Interrupts have the constraint of needing hardware support, but they can respond much faster than polling. So they are an essential feature wherever real-time response is needed.
In short, an interrupt is the fastest way to respond to events whose timing is hard to predict.
Getting a Feel With Code: "Notifications" a Process Receives
Real hardware interrupts are handled inside the operating system kernel, so ordinary programs cannot see them directly. Instead, the signals the operating system sends to a process follow exactly the same flow as interrupts: "stop what you are doing, run the registered function, then go back to where you were." That is why signals are sometimes called "the software version of interrupts." (Signals are also covered in the SIGKILL part of the Memory Leaks and OOM article.)
import signal, time
def on_alarm(signum, frame): # function to run when the notification arrives (acts as the service routine)
print(" [handling notification] 1 second has passed!")
signal.signal(signal.SIGALRM, on_alarm) # register "when this notification arrives, run that function"
signal.alarm(1) # schedule a notification for 1 second from now
for i in range(1, 5): # the original work
print(f"working {i}")
time.sleep(0.4)
print("work done")Output (run on macOS or Linux):
working 1
working 2
working 3
[handling notification] 1 second has passed!
working 4
work done- The program never once checked whether 1 second had passed. (No polling)
- At the 1-second mark,
on_alarmcut in and ran in the middle of the work, and when it finished, control went back to the original loop and continued with "working 4".
Summary
| Concept | Plain-language summary |
|---|---|
| Interrupt | A doorbell that says "stop what you're doing and handle the urgent thing first" |
| External interrupt | A doorbell pressed by outside devices like the keyboard, disk, timer or power supply |
| Internal interrupt (exception) | The CPU stops on its own because a problem came up during execution, like division by zero |
| Software interrupt | A program deliberately asks the operating system for help (system call) |
| Saving and restoring state | Put in a bookmark, handle the task, then return to that spot |
| Interrupt service routine (ISR) | A handling procedure set in advance for each doorbell |
| Polling | Going to the front door every few minutes to check for a package. Slow and wasteful |
| Daisy chain / Parallel priority | How the hardware picks who to serve first when several requests overlap |
