Interrupts, Exceptions and Kernel Designs
Learn how interrupts, traps and exceptions hand control to the kernel, why the timer interrupt matters, and how monolithic and microkernel designs differ.
Welcome back!
Step 1 of 14Learn
The OS is event-driven
Most of the time, the kernel isn't running at all. Your programs are. The kernel wakes up only when an event hands it control. Understanding those events is the key to understanding how an OS keeps control of a machine it doesn't constantly occupy.
There are three kinds of events:
- Hardware interrupts come from devices. The network card has received a packet, the disk has finished a read, a key has been pressed, a timer has fired. They are asynchronous: they can arrive between any two instructions, unrelated to what the program is doing.
- Traps (software interrupts) are deliberate. A program executes a special instruction to make a system call. They are synchronous: they happen exactly at that instruction, every time it runs.
- Exceptions (faults) are caused by the current instruction going wrong: division by zero, an invalid instruction, or an access to memory that isn't mapped or isn't allowed. Also synchronous.
For each event type, the CPU looks up a handler address in a table that the kernel filled in at boot, often called the interrupt vector table or interrupt descriptor table. It saves the interrupted program's state, switches to kernel mode and jumps to the handler. When the handler finishes, the saved state is restored and the program continues, usually unaware anything happened.
Some exceptions are not errors at all. A page fault, for instance, often just means the data a program needs is not in RAM yet. The kernel loads it and lets the program retry, as you'll see in the virtual memory module.
Step 2 of 14Compare
Three ways the kernel gets control
| Aspect | Hardware interrupt | Trap (system call) | Exception (fault) |
|---|---|---|---|
| Source | A device or the timer | The program, on purpose | The program, by accident |
| Timing | Asynchronous: any time | Synchronous | Synchronous |
| Example | Key pressed, packet arrived | read(), write(), fork() | Divide by zero, page fault |
| After handling | Resume (maybe another process) | Return result to the caller | Fix and retry, or kill the process |
Step 3 of 14Practice · Sort into groups
Classify each event.
Tap an item, then tap its group (or drag it there).
Step 4 of 14Learn
The timer: how the OS keeps control
Suppose a program enters an infinite loop: while (true) {}. It makes no system calls and triggers no exceptions. If nothing else happened, the kernel would never run again, and the machine would belong to that one program forever.
The defense is a hardware timer that the kernel programs during boot to raise an interrupt at regular intervals, commonly every 1 to 10 milliseconds, or at precisely chosen moments on modern tickless kernels. Every timer interrupt forces the CPU into the kernel. The scheduler can then decide that the looping program has had its turn and switch to another one.
This is called preemptive multitasking: the OS can take the CPU away from a program whether it cooperates or not. Older systems such as classic Mac OS and early Windows used cooperative multitasking, where each program had to voluntarily give up the CPU. One badly behaved program could freeze the whole computer, which was a common experience in the early 1990s.
Only the kernel may change the timer settings or disable interrupts, because those are privileged instructions. If user programs could switch off the timer, preemption would be meaningless.
Step 5 of 14Practice · Diagram
How the timer keeps the kernel in charge
Which step can't a user program prevent, no matter what code it runs?
Tap the shape that answers the question, then press Check.
- Program runs
Even an infinite loop with no system calls.
- Timer interrupt
Hardware forces the CPU into kernel mode.
- Scheduler decides
Has this program used up its time slice?
- Context switch
Maybe resume the same program, maybe start another.
Step 6 of 14Practice · Choose one
Which mechanism lets the OS regain the CPU from a program stuck in an infinite loop that never makes a system call?
Step 7 of 14Note
Step 8 of 14Learn
Monolithic kernels and microkernels
How much should run inside the kernel? Designers disagree, and the two classic answers make different trade-offs.
A monolithic kernel puts nearly all OS services in kernel mode, in one large program: process scheduling, memory management, file systems, network protocols and device drivers. Components call each other as ordinary functions, which is fast. The downside is that every line runs with full privilege. A bug in an obscure driver can crash the whole system or open a security hole. Linux is the best-known example, though it can load and unload drivers as modules while running.
A microkernel keeps the kernel as small as possible: little more than scheduling, basic memory management and inter-process communication (IPC). File systems, drivers and network stacks run as separate user-mode servers that exchange messages. A crashed driver can be restarted without rebooting, and the small trusted core is easier to verify. The cost is extra message passing and mode switches. Examples include QNX, used in cars and industrial systems, and seL4, whose kernel has a formal mathematical proof of correctness.
Most popular desktop systems are hybrids. Windows NT and Apple's XNU kernel began with microkernel ideas but, for speed, run many services, including file systems and much of the graphics stack, inside kernel mode.
Step 9 of 14Compare
Kernel designs side by side
| Aspect | Monolithic | Microkernel |
|---|---|---|
| Where drivers run | In kernel mode | As user-mode server processes |
| How services talk | Direct function calls | Messages through IPC |
| Speed | Generally faster | Extra overhead per message |
| Fault isolation | A driver bug can crash everything | A crashed driver can often be restarted |
| Examples | Linux, traditional Unix | QNX, seL4, MINIX 3 |
Step 10 of 14Practice · Select all that apply
Which statements correctly describe a microkernel design? Select all that apply.
Select all that apply.
Step 11 of 14Learn
Starting the kernel
The kernel can only manage events once it's running, so it's worth seeing how it gets there.
At power-on, RAM is empty. The CPU starts executing firmware (UEFI on modern PCs) from a chip on the motherboard. Firmware initializes the hardware, then loads a boot loader from the system drive. On systems with Secure Boot, the firmware first checks the boot loader's digital signature, and each stage verifies the next, forming a chain of trust that makes it harder for malware to load before the OS.
The boot loader, such as GRUB on Linux or the Windows Boot Manager, loads the kernel image into memory and jumps to its entry point. The kernel then:
- Sets up its own memory management and the interrupt handler table.
- Programs the timer.
- Detects hardware and loads the matching drivers.
- Mounts the root file system.
- Starts the very first user-mode process:
initorsystemdon Linux,launchdon macOS, a session manager on Windows.
From then on, every other process descends from that first one. The kernel steps back and runs only when an interrupt, trap or exception calls it.
Step 12 of 14Diagram
From power button to login
Tap a shape with to see more.
- Firmware (UEFI)
Runs from a motherboard chip and initializes hardware.
- Secure Boot check
Verifies the boot loader's signature: the start of the chain of trust.
- Boot loader
GRUB or Windows Boot Manager loads the kernel image into RAM.
- Kernel initializes
Memory, interrupts, timer, drivers.
- First user process
systemd on most Linux systems; it starts services.
- Login screen
Step 13 of 14Practice · Put in order
Order what the kernel does after it receives control from the boot loader.
Drag the items, or use the arrow buttons, to put them in order.
- Start the first user-mode process
- Set up memory management and the interrupt table
- Program the timer
- Detect hardware and load drivers
- Mount the root file system
Step 14 of 14Practice · True or false
In a monolithic kernel, a bug in a network driver can crash the entire operating system.
Lesson complete!