Part 5 of 8 · 4 min read
Three kinds
Think of a bank with three ways to get the manager's attention: a customer rings a bell at any moment, a clerk raises a hand because of a problem in the clerk's own task, and a clerk asks on purpose. The analogy stops in one place: the manager is one person, and the processor uses one set of machinery for all three.
In plain words: An interrupt is asynchronous: it comes from a device between any two instructions. A trap is synchronous: the running program causes it, by an error such as division by zero or by a deliberate instruction such as a system call. The hardware uses the same machinery for all of them, and the OS then reacts in a way that fits each kind.
The kernel is entered through three doors, and it helps to separate them.
| Kind | Source | When it happens | Example |
|---|---|---|---|
| Interrupt | A device or timer | At any time, unrelated to the current instruction | Disk read done, timer tick |
| Trap (system call) | The program, on purpose | At the program's own instruction | write() |
| Exception (fault) | The program, by mistake or need | At the instruction that caused it | Divide by zero, invalid memory address |
An interrupt is asynchronous: it can arrive between any two instructions and has nothing to do with them. A trap or an exception is synchronous: it comes from the instruction being executed. OSC calls a trap, also named an exception, a software-generated interrupt, caused either by an error (division by zero, invalid memory access) or by a request from a program for a service, which is a system call.
The hardware handles all of them with the same machinery: the interrupt vector, a handler, a switch to kernel mode and a return. Divide error is vector 0 on Intel processors.
What the OS does afterwards differs. On a device interrupt, it handles the event and goes back to the interrupted program. On a system call, it does the work and returns the result. On an error, it normally ends the program and may save a memory dump to a file. Some exceptions the OS repairs and retries. An example is a page fault, when a program touches memory that is valid but not currently in physical memory. The OS loads the data and restarts the same instruction (Level 5).
The key lines.
A program can learn that it caused a fault through a signal. A signal is a software notification the OS sends to a process (Level 1 covers signals fully). The program below installs a handler for the divide error, divides by zero, and the trap arrives as the signal SIGFPE:
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
static void on_fault(int sig) {
const char msg[] = "trap: caught divide error (SIGFPE), exiting\n";
(void)sig;
write(1, msg, sizeof msg - 1);
_exit(1);
}
int main(void) {
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = on_fault;
sigaction(SIGFPE, &sa, NULL);
volatile int zero = 0;
printf("about to divide by zero\n");
fflush(stdout);
int x = 10 / zero;
printf("not reached: %d\n", x);
return 0;
}
Output, compiled without optimisation:
about to divide by zero
trap: caught divide error (SIGFPE), exiting
The processor trapped on the division, the kernel handled the trap, and the kernel delivered a signal to the program. Without the handler, the program would simply be ended, with the message "Floating point exception".
Trap
A trap is a synchronous interrupt generated by the running program itself: by an error in an instruction (such as division by zero) or by a deliberate instruction (such as the one that makes a system call). It is synchronous because it happens at a known instruction of the program's own execution, unlike a device interrupt, which arrives at an unrelated moment.
Example: a divide by zero causes a trap, and the kernel ends the program or notifies it with a signal.
Check yourself:
1. Is a timer tick synchronous or asynchronous?
2. Which of the three kinds does write() belong to?
3. Why can the OS repair a page fault but not a division by zero?