Technopedia

The technologist’s encyclopedia

Operating System

The operating system mediates between programs and hardware: it schedules processes, manages memory, provides file abstractions, and drives I/O devices. It is relevant whenever a program’s resource usage, isolation, or I/O behavior must be understood or controlled.

Authoritative sources

Processes

A process is a running program with its own virtual address space, file descriptors, and resources, identified by a unique PID. A thread shares the process’s address space but has its own stack and registers. A core executes one thread at a time (or two with SMT/hyperthreading). Preemption is the OS’s ability to interrupt a running process and switch to another; cooperative scheduling requires the process to voluntarily yield.

For the deeper treatment of M:N scheduling, thread pools, race conditions, and the memory model, see Concurrency. For the hardware-level memory layout (text, data, bss, heap, stack segments) and the virtual-memory mechanism (MMU, TLB, page-table walk), see Memory Hierarchy.

Details

Scheduling algorithms:

Context switch: save the current process’s state (registers, PC, SP) to its PCB (Process Control Block); load the next process’s state. Cost: ≈1\approx 1–10μs10 \mu s (kernel) + cache/TLB flush (≈1000\approx 1000 cycles). The pipeline flush on context switch is a hardware cost described in CPU Architecture.

Virtual memory (OS role): the OS allocates page-table entries, handles page faults by loading pages from disk or swap, and decides which page to evict when RAM is full. The hardware mechanism (MMU translation, TLB caching, page-table walk) is covered in Memory Hierarchy.

Memory management

The OS’s primary contribution to memory management is deciding which page to evict when RAM is full, managing the swap area, and detecting thrashing. The hardware translation (MMU, TLB, page-table walk) belongs to Memory Hierarchy.

Details

Page replacement policies (the OS’s responsibility):

Swap management: when RAM is full, the OS writes inactive pages to a swap file (or swap partition) on disk. The swap area is typically 11–22× RAM. The OS uses a page-out policy (often the same replacement algorithm) to select which pages to write, and a page-in policy to bring them back on demand.

Page fault handling (OS role): on a fault, the OS (1) checks the page-table entry to determine the fault type (not-present, protection, write-on-clean), (2) allocates a free frame, (3) if the page is on disk, issues a DMA read, (4) updates the page table, (5) restarts the faulting instruction. The hardware TLB and page-table walk that triggered the fault are described in Memory Hierarchy.

Drivers and I/O interrupts

I/O (Input/Output) is the transfer of data between the CPU and a device (disk, network, keyboard). Interrupts are hardware signals that notify the CPU of an event (I/O complete, timer tick, exception). The hardware interrupt mechanism (APIC, interrupt vectors, pipeline flush) is described in CPU Architecture.

Details

I/O modes:

Interrupt handling (OS role): the kernel’s interrupt handler (ISR) acknowledges the device and schedules a bottom-half (softirq, tasklet, or workqueue) to do the heavy work in process context. NMI (Non-Maskable Interrupt): cannot be disabled; used for hardware errors.

Drivers: kernel modules that implement device-specific I/O operations. A device driver exposes a uniform interface (read, write, ioctl) to the rest of the OS, hiding hardware details behind the VFS (Virtual File System) layer.

Syscalls

A syscall (system call) is the interface between user space and kernel space. It is a controlled transition from user mode to kernel mode (via syscall on x86-64, int 0x80 on x86-32, or svc on ARM).

Details

Common syscalls:

The kernel validates the arguments, performs the operation, and returns a result (or an error code via errno). The user process is blocked until the syscall completes (or returns immediately for non-blocking operations).

Inter-Process Communication

IPC mechanisms allow processes to exchange data or synchronize. They are the OS-level primitives that higher-level concurrency patterns (CSP, actor model) build upon.

Details

Boot

The boot sequence takes the machine from power-on to a running user process.

Details
  1. Firmware (BIOS/UEFI): runs from ROM. Initializes hardware (POST), locates the bootloader.
  2. Bootloader (GRUB, UEFI boot manager): loads the kernel image and initramfs into memory.
  3. Kernel: decompresses, initializes subsystems (memory, scheduler, I/O), mounts the root file system.
  4. init (PID 1): the first user-space process. Spawns system services and the login shell.

The MBR (Master Boot Record) is the first 512 bytes of a disk: 446 bytes of bootloader code + 64-byte partition table + 2-byte signature. UEFI replaces the MBR with an EFI System Partition (FAT32) containing the boot manager.

File systems

A file system organizes data on a storage device. A file is a sequence of bytes. An inode stores metadata (size, permissions, timestamps, block pointers). A sector is the minimum unit of disk I/O (512 B for legacy HDDs, 4 KB for modern drives). A block is a file-system-level abstraction that groups one or more sectors (typically 4 KB); it is the unit in which the file system allocates and addresses data.

Details

File descriptors (fd): a small integer that refers to an open file. open() returns an fd; read(fd, buf, n), write(fd, buf, n), close(fd) operate on it. fd 0, 1, 2 are stdin, stdout, stderr.

Inode and block mapping:

Allocation strategies:

seek(fd, offset, whence): move the file offset. lseek(fd, 0, SEEK_END) returns the file size. append: open(path, O_APPEND) sets the offset to the end before each write.

rename(old, new): atomic on the same file system. link(old, new): create a new directory entry pointing to the same inode (hard link).

Journaling (ext4, XFS, NTFS): log metadata changes before applying them. After a crash, replay the journal to recover consistency. Copy-on-write (ZFS, Btrfs): modify a copy of the block, then atomically update the pointer. Crash-safe without a journal.

Simulation

Example problem

Problem: A process calls open("/data/file.txt", O_RDONLY), then read(fd, buf, 4096), then close(fd). Describe the sequence of syscalls, disk accesses, and cache interactions.

Solution
  1. open("/data/file.txt", O_RDONLY):

    • The kernel resolves the path: look up /data in the root directory’s inode, then file.txt in /data’s inode.
    • If the directory entries are not in the dentry cache or inode cache, the kernel reads the directory blocks from disk.
    • The kernel creates a file structure and assigns an fd (e.g., 3).
    • Disk accesses: 0–2 (if cached) or 2–4 (if cold: root dir + /data dir + inode).
  2. read(fd, buf, 4096):

    • The kernel checks the file’s current offset (0).
    • It looks up the block containing offset 0 in the inode’s extent tree.
    • If the block is in the page cache (buffer cache), it copies 4096 bytes to buf. Disk access: 0.
    • If not in the page cache, it issues a disk read (DMA), waits for the interrupt, then copies. Disk access: 1 (≈100μs\approx 100 \mu s for SSD, ≈10\approx 10 ms for HDD).
    • The offset advances to 4096.
  3. close(fd):

    • The kernel decrements the file’s reference count. If it reaches 0, it frees the file structure.
    • The inode and dentry remain in the cache (for future lookups).
    • Disk access: 0.

Cache interaction summary (cold start, all caches empty):

SyscallDentry cacheInode cachePage cache
open("/data/file.txt")MISS → loadMISS → load—
read(fd, buf, 4096)HITHITMISS → DMA read
close(fd)HIT (retained)HIT (retained)HIT (retained)

Total disk accesses: 0–6 (depending on cache state). The page cache is the most important: it makes repeated reads of the same file a memory copy instead of a disk read.