OS Foundations
What an operating system does, user mode vs kernel mode, system calls, interrupts and traps, the boot process and kernel architectures (monolithic, microkernel, hybrid).
What an operating system does
An operating system is the software layer between applications and hardware. It has two jobs:
- Abstraction — turn messy hardware into clean, uniform concepts: CPUs become threads, RAM becomes a private address space, disks become files, network cards become sockets.
- Resource management and protection — share the hardware among many programs fairly and safely: decide who runs on the CPU, who gets memory, and stop programs from interfering with each other.
Dual-mode operation
The CPU itself enforces protection with a mode bit:
| User mode | Kernel mode | |
|---|---|---|
| Privileged instructions (I/O, change page tables, disable interrupts) | ✗ trap | ✓ |
| Memory accessible | only the process's mapped pages | everything |
| Who runs here | applications, libraries | kernel, (in monolithic kernels) drivers |
| A crash means | that process dies | the whole machine panics |
On x86 these are protection rings (0 = kernel, 3 = user; 1–2 unused by mainstream OSes). Virtualization adds a further level below ring 0 for the hypervisor.
System calls
A system call is the controlled entry point from user mode into the kernel. The program places the syscall number and arguments in registers and executes a trap instruction; the CPU switches to kernel mode and jumps to a fixed handler.
#include <unistd.h>
#include <fcntl.h>
int main(void) {
int fd = open("notes.txt", O_RDONLY); // syscall: open
char buf[128];
ssize_t n = read(fd, buf, sizeof buf); // syscall: read (may block)
write(STDOUT_FILENO, buf, n); // syscall: write
close(fd); // syscall: close
return 0;
}
import os
fd = os.open("notes.txt", os.O_RDONLY) # thin wrappers over the same syscalls
data = os.read(fd, 128)
os.write(1, data)
os.close(fd)
Run strace ./prog (Linux) or dtruss (macOS) to see every syscall a program makes — a great debugging and interview-story tool.
Categories of system calls
| Category | Examples (POSIX/Linux) |
|---|---|
| Process control | fork, execve, exit, wait, kill |
| File management | open, read, write, close, lseek, stat |
| Device management | ioctl, read/write on device files |
| Information | getpid, clock_gettime, uname |
| Communication | pipe, socket, send, recv, mmap, shmget |
| Protection | chmod, chown, setuid, seccomp |
Interrupts, exceptions and traps
| Event | Synchronous? | Cause | Example |
|---|---|---|---|
| Hardware interrupt | no (async) | device signals the CPU | timer tick, packet arrived, disk done |
| Exception / fault | yes | the current instruction | page fault, divide by zero, illegal instruction |
| Trap / syscall | yes | deliberate request | syscall instruction |
The timer interrupt is the foundation of multitasking: it fires periodically (or on demand in tickless kernels), returning control to the kernel so the scheduler can preempt a process that would otherwise never give up the CPU.
Interrupt handling is split into a fast top half (acknowledge the device, grab data, run with interrupts disabled briefly) and a deferred bottom half (softirqs, tasklets, workqueues) that does the heavier processing.
The boot process
- Firmware (UEFI) runs POST and initialises hardware.
- Loads the bootloader from the EFI system partition.
- Bootloader loads the kernel and initramfs, passes the kernel command line.
- Kernel initialises subsystems, mounts root, starts PID 1 (
systemd). - PID 1 starts services and the login manager.
Kernel architectures
| Architecture | Idea | Examples | Pros | Cons |
|---|---|---|---|---|
| Monolithic | everything in kernel space | Linux, FreeBSD | fast (function calls) | one bug can crash everything |
| Microkernel | minimal kernel; drivers/FS as user-space servers | seL4, QNX, MINIX 3 | isolation, verifiability | IPC overhead |
| Hybrid | microkernel structure, services in kernel space | Windows NT, macOS XNU | balance | complexity |
| Unikernel | app + library OS compiled into one image | MirageOS, Unikraft | tiny, fast boot | single app, harder debugging |
Virtualization and containers (the modern context)
- A hypervisor runs multiple OSes on one machine. Type 1 runs on bare metal (KVM, Xen, Hyper-V, ESXi); Type 2 runs on a host OS (VirtualBox). Hardware support (Intel VT-x, AMD-V, nested page tables) makes it fast.
- A container is not a VM: processes on the same kernel, isolated with namespaces (pid, net, mount, uts, ipc, user, cgroup — what a process can see) and limited with cgroups (CPU, memory, I/O — what it can use). Containers start in milliseconds and share the kernel, so isolation is weaker than VMs (hence gVisor, Firecracker microVMs, Kata).
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.