OPERATING SYSTEMS / 3. THE KERNEL
The Linux Kernel — The Core of the OS
Process management, memory management, file systems, device drivers
EXPLANATION
The kernel is the most privileged piece of software running on your computer. It has direct access to every hardware register, every byte of RAM, every device. Everything else runs at the mercy of the kernel. The CPU has hardware privilege levels (rings): • Ring 0 (Kernel mode): unrestricted access to all hardware and memory • Ring 3 (User mode): restricted — cannot access hardware directly, cannot access other processes' memory • Rings 1 and 2: historically for device drivers, mostly unused today When user code needs hardware access (read a file, send a network packet), it invokes a system call. This triggers a software interrupt, CPU switches to Ring 0, kernel executes the operation, returns result, CPU switches back to Ring 3. This mode switch is expensive (~100ns) but necessary for safety. Process Management — the kernel's core job: • A process is a running program: it has its own PID, virtual address space, file descriptors, and CPU registers (saved when not running) • The scheduler decides which process runs on which CPU core at any given moment • Context switching: save current process state (registers) → restore next process state → run it • Round-robin with priorities: each process gets a time slice (~1-10ms), then gets preempted • Processes are isolated — process A cannot read process B's memory (virtual memory enforces this) Process states: • Running → currently executing on a CPU • Runnable/Ready → ready to run but waiting for CPU • Sleeping/Blocked → waiting for I/O (disk, network), or a signal • Zombie → finished but parent hasn't read its exit code yet Memory Management: • The kernel manages physical RAM — it decides which physical pages are allocated to which process • Virtual memory: each process thinks it has the entire address space (e.g. 0 to 2^48 on 64-bit). The MMU (hardware) translates virtual → physical using page tables • Page = 4 KB chunk of memory (the atomic unit of memory management) • When a process accesses a virtual address, the MMU looks up the page table. If there's no mapping → page fault → kernel allocates a physical page → updates page table → returns File System: • The kernel abstracts all storage (disk, USB, network share) into a unified file tree starting at / • VFS (Virtual File System) layer — provides a unified interface. Actual filesystems (ext4, NTFS, btrfs, ZFS, tmpfs, procfs) implement this interface • Everything is a file in Unix: regular files, directories, devices (/dev/sda), network sockets, pipes — all accessed via the same read/write/open/close syscalls • /proc — virtual filesystem that exposes kernel data structures as files (read /proc/meminfo and the kernel generates the content on the fly) Interrupts: • Hardware interrupts: keyboard press, network packet arrives, disk I/O completes → hardware signals CPU → kernel interrupt handler runs • Timer interrupt: fires every ~1ms → scheduler gets to preempt current process • Interrupt handlers run at Ring 0, must be fast, cannot block
DIAGRAM
KERNEL ARCHITECTURE: ┌──────────────────────────────────────────────┐ │ User Space │ │ [Process A] [Process B] [Shell] [Python] │ │ │ │ │ ├───────┼────────────┼─── SYSCALL INTERFACE ───┤ │ ↓ ↓ Ring 0 │ │ ┌────────────────────────────────────────┐ │ │ │ Linux Kernel │ │ │ │ Process Memory VFS Network │ │ │ │ Manager Manager Layer Stack │ │ │ │ │ │ │ │ │ │ │ │ Scheduler MMU Device Drivers │ │ │ └────────────────────────────────────────┘ │ ├──────────────────────────────────────────────┤ │ HARDWARE │ │ [CPU+MMU] [RAM] [Disk] [NIC] [GPU] │ └──────────────────────────────────────────────┘ VIRTUAL MEMORY PER PROCESS: Virtual Address Space (48-bit = 256 TB): 0x0000000000000000 ← NULL (unmapped, crash if accessed) 0x00007fff.... ← Stack (grows down) ...heap... ← Heap (grows up, malloc/new) ...mmap... ← shared libs (.so), mmap files 0x400000 ← Program text (.text segment) ─────────────────── kernel space (inaccessible from user)
CODE