Interrupts and the interrupt cycle
Interrupts and the interrupt cycle
Recorded by Dr. Krithika L.B. Slot m1-interrupts
How control reaches the kernel through interrupts, traps and exceptions; the principles that guide OS design; and how a kernel is built and brought to life at power-on.
By the end of this session you should be able to:
Imagine the CPU has asked the disk controller to read a block. It can either poll — repeatedly check the controller's status register until it says “done”, wasting millions of cycles — or carry on with other work and let the controller interrupt it when the data is ready. Modern operating systems are interrupt driven: after boot, the kernel runs only in response to events.
Maskable interrupts can be temporarily disabled by the CPU during critical kernel code. Non-maskable interrupts (NMI) signal serious errors such as memory failure and cannot be ignored.
Interrupts have priorities, so a high-priority interrupt (e.g. timer) can pre-empt the ISR of a low-priority one (e.g. printer).
When there are more devices than vector entries, each entry points to a list of handlers that are tried in turn.
The OS sets a hardware timer before giving the CPU to a user program. When it fires, the kernel regains control — so a program in an infinite loop cannot monopolise the CPU. This is what makes time-sharing and preemptive scheduling (Module 2) possible.
$ head -6 /proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 36 0 0 0 IO-APIC 2-edge timer
1: 0 0 9123 0 IO-APIC 1-edge i8042
8: 0 0 0 0 IO-APIC 8-edge rtc0
120: 0 184533 0 0 PCI-MSI 327680-edge xhci_hcd
122: 45821 0 0 398114 PCI-MSI 520192-edge nvme0q0Line 1 (i8042) is the keyboard, xhci_hcd the USB controller, nvme0q0 the SSD. For bulk
transfers an interrupt per byte would overwhelm the CPU, so controllers use Direct Memory Access (DMA): the
controller copies a whole block straight into memory and raises a single interrupt when the block is complete. DMA and device
management return in Module 5.
There is no complete recipe for designing an OS, but some principles have proved themselves.
Convenient to use, easy to learn, reliable, safe and fast.
Easy to design, implement and maintain; flexible, reliable, error-free and efficient.
These requirements are vague and sometimes conflicting, and they depend heavily on the type of system: a real-time controller, a phone, a laptop and a cloud server want different things. The first design step is always to fix the goals.
Mechanism = how to do something. Policy = what will be done, or which choice is made.
Policies change over time and from place to place. If the mechanism is general enough, changing a policy means changing a
parameter, not rewriting the kernel. Microkernels push this to the extreme (only basic mechanisms in the kernel); Linux lets
you choose a scheduling policy per process (SCHED_OTHER, SCHED_FIFO, SCHED_RR) on top
of one scheduler mechanism.
Early OSes were written in assembly. Today most are written in higher-level languages — Linux and Windows are mostly C (with some C++ in Windows and growing amounts of Rust in Linux), with small parts in assembly (boot code, context switch, interrupt entry). A high-level language makes the code faster to write, easier to understand and debug, and portable to new hardware. Compilers are good enough that the speed penalty is small; the real performance wins come from better data structures and algorithms, and critical routines can still be hand-optimised later.
| Issue | The question the designer must answer | Where it appears in this course |
|---|---|---|
| Efficiency / performance | How to keep CPU, memory and disks busy with low overhead? | Scheduling (M2), paging (M4), disk scheduling (M5) |
| Concurrency | How do many activities share data safely? | Synchronization (M3), deadlocks (M2) |
| Protection & security | Who may access what, and how to stop attacks? | Access matrix, capabilities, cryptography (M5) |
| Reliability & fault tolerance | What happens when hardware or software fails? | Microkernels (1.2), RAID (M5) |
| Extensibility | How to add new devices and features? | Modules (1.2), device drivers (M5) |
| Portability & compatibility | Can it run on new hardware and still run old programs? | APIs (1.3), virtualization (M5) |
| Scalability | Does it work on 1 core and on 128 cores? | Multiprocessor scheduling (M2) |
An OS can be built for one specific machine, but more often it is generic and must be configured for each site — a step called system generation (SYSGEN). Building a Linux kernel from source shows the steps clearly:
$ wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.6.tar.xz # 1. obtain the source
$ tar xf linux-6.6.tar.xz && cd linux-6.6
$ make menuconfig # 2. configure: choose drivers, file systems -> .config
$ make -j$(nproc) # 3. compile the kernel image and the modules
$ sudo make modules_install # 4. copy modules to /lib/modules/6.6.0/
$ sudo make install # 5. install kernel + initramfs in /boot, update GRUB
$ sudo reboot # 6. boot the new kernel, then check with: uname -rConfiguration choices trade size against generality: a kernel completely tailored to one machine is small and fast; one that is table-driven or uses loadable modules can adapt at run time. Distributions such as Ubuntu ship a generic kernel with most drivers as modules — the modular structure from Session 1.2 in practice.
When the power is switched on, RAM is empty. How does the kernel get loaded if loading programs is the kernel's own job? The answer is a short chain of progressively smarter loaders — booting, from “pulling yourself up by your bootstraps”.
0x7C00, checks that its last two bytes are the
boot signature 0x55 0xAA, and jumps to it. UEFI instead loads a boot program (an EFI
application) from a FAT-formatted EFI System Partition.init — on most Linux systems systemd,
with process id 1 — which starts services and finally the login screen. From here on, the kernel waits
for interrupts and system calls.$ ls /boot # kernel images, initramfs and GRUB live here
config-6.5.0-35-generic grub initrd.img-6.5.0-35-generic vmlinuz-6.5.0-35-generic
$ sudo dmesg | head -2 # first kernel messages after the boot loader handed over
[ 0.000000] Linux version 6.5.0-35-generic ...
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-6.5.0-35-generic root=UUID=... ro quiet splash
$ ps -p 1 -o pid,comm # the first user-space process
PID COMMAND
1 systemd
$ systemd-analyze # how long each boot stage took
Startup finished in 6.1s (firmware) + 2.9s (loader) + 3.2s (kernel) + 9.8s (userspace) = 22.0sIn Lab 2 you write a real 512-byte boot sector that the BIOS loads at 0x7C00, ending with the 0x55 0xAA signature, and boot it in the QEMU emulator. A small C program then checks the image exactly as the firmware would.
Interrupts and the interrupt cycle
Recorded by Dr. Krithika L.B. Slot m1-interrupts
OS design goals, mechanism and policy
Recorded by Dr. Krithika L.B. Slot m1-design-issues
Building and booting an operating system
Recorded by Dr. Krithika L.B. Slot m1-booting
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0An interrupt is asynchronous and comes from hardware (a device or the timer), unrelated to the current instruction. A trap is synchronous and caused by software — deliberately (a system call) or by an error (an exception such as divide by zero).
So that after the ISR finishes, the interrupted program can resume at exactly the next instruction with its registers unchanged — interrupts must be invisible to the program.
A user program that never makes a system call (e.g. an infinite loop) would never give control back to the kernel, so the OS could not schedule anything else. The timer guarantees the kernel regains control periodically.
(a) policy · (b) mechanism · (c) policy (enforced with the permission-bit mechanism).
They must contain the boot signature 0x55 0xAA, which marks the sector as bootable so the BIOS does not jump into random data. If it is missing, the BIOS tries the next boot device.
When systemd starts as PID 1, the machine is finally running processes — and from then on almost everything the kernel does is about them: creating them with fork(), choosing which one runs next, running them as threads, and keeping them out of deadlock. That is Module 2.