Skip to content

Introduction to Operating Systems

An operating system (OS) is system software that manages hardware resources and provides services To application programs. It serves as an intermediary between users and the underlying hardware.

Core responsibilities:

  • Process management: Create, schedule, and terminate processes.
  • Memory management: Allocate and protect memory; implement virtual memory.
  • File system management: Organise, store, retrieve, and protect data.
  • I/O management: Control and mediate access to I/O devices.
  • Protection and security: Enforce access controls and resource isolation.

Monolithic Kernel. All OS services (file system, device drivers, network stack, scheduler) run In a single address space in kernel mode. Examples: Linux, FreeBSD.

  • Advantage: Low overhead from function-call latency.
  • Disadvantage: A bug in any component can crash the entire system.

Microkernel. Only essential services (IPC, scheduling, basic memory management) run in kernel Mode. Other services run in user mode as separate processes. Examples: MINIX 3, seL4, QNX.

  • Advantage: Fault isolation; easier to verify formally.
  • Disadvantage: Higher overhead from message passing between user-mode servers.

Hybrid Kernel. A pragmatic compromise combining monolithic and microkernel ideas. Some Non-essential services run in kernel mode for performance, but the architecture is more modular Than a pure monolith. Examples: Windows NT, macOS XNU.

System calls provide the interface between user-mode applications and kernel-mode OS services. They Are invoked via software interrupts (e.g., syscall on x86-64, svc on ARM).

Categories:

CategoryExamples
Process controlfork``exec``wait``exit
File managementopen``read``write``close
Device I/Oioctl``read``write
Communicationpipe``shmget``mmap``socket
Informationgetpid``stat``sysconf
Protectionchmod``chown``setuid

System call overhead. A transition from user mode to kernel mode involves saving user Registers, switching to the kernel stack, validating arguments, executing kernel code, and Returning. Typical overhead: 1001000100\mathrm{--1000} ns on modern hardware.

A process transitions through several states during its lifetime:

StateDescription
NewThe process is being created.
ReadyThe process is in memory and waiting to be assigned to a CPU.
RunningInstructions are being executed by the CPU.
WaitingThe process is waiting for some event (e.g., I/O completion).
TerminatedThe process has finished execution.

Scheduling algorithms determine which ready process receives CPU time:

  • First-Come, First-Served (FCFS): Simple, but suffers from the convoy effect (short jobs wait behind long ones).
  • Shortest Job First (SJF): Optimal average waiting time but requires advance knowledge of CPU bursts.
  • Round Robin (RR): Each process receives a fixed time quantum; good responsiveness for interactive workloads.
  • Priority Scheduling: Processes with higher priority run first; can cause starvation of low-priority processes.

Virtual memory separates logical addresses seen by processes from physical memory addresses. The MMU (Memory Management Unit) translates virtual addresses to physical ones using page tables.

Paging divides memory into fixed-size frames (physical) and pages (virtual). A virtual address (p,d)(p, d) consists of a page number pp and an offset dd. The page table maps pp to a frame number ff, giving physical address (f,d)(f, d).

Page replacement policies (FIFO, LRU, Optimal) determine which page to evict when memory is full. Belady’s anomaly shows that FIFO can have higher miss rates with more frames; LRU and Optimal do not suffer from this.

An interrupt is a signal from hardware or software that causes the CPU to pause its current execution and run an interrupt handler. Types include:

  • Hardware interrupts: Generated by devices (keyboard, disk, timer) via the interrupt controller.
  • Software interrupts: Generated by instructions like int or syscall (used for system calls).
  • Exceptions: Generated by the CPU on error conditions (page fault, division by zero).

The interrupt vector table (IVT) maps interrupt numbers to handler addresses. After handling, the CPU returns to the interrupted instruction (or terminates the process for fatal exceptions).

File systems organise persistent data on storage devices. Common layouts:

  • Contiguous allocation: Fast sequential access but suffers from external fragmentation.
  • Linked allocation: Each block points to the next; no fragmentation but poor random access.
  • Indexed allocation: An index block contains pointers to data blocks; good for both sequential and random access (used in Unix inode-based systems).

Problem. Trace the execution path of a user-mode call to read(fd, buf, count).

Solution
  1. The C library wrapper (glibc) places arguments in registers and executes a syscall instruction.
  2. The CPU switches to kernel mode, saves the return address and user stack pointer.
  3. The system call handler dispatches to the kernel’s sys_read function based on the syscall number.
  4. sys_read validates the file descriptor, checks buffer accessibility, and invokes the file system or device driver.
  5. Data is transferred from kernel buffers to user-space memory (via copy_to_user).
  6. Control returns to user mode; the wrapper function returns the number of bytes read.

\blacksquare

A deadlock occurs when two or more processes are each waiting for resources held by the other. Four necessary conditions (Coffman conditions):

  1. Mutual exclusion: Resources cannot be shared.
  2. Hold and wait: Processes hold resources while waiting for others.
  3. No preemption: Resources cannot be forcibly taken.
  4. Circular wait: A cycle of processes each waiting for the next.

Deadlock prevention strategies: eliminate one of the four conditions. Deadlock avoidance (banker’s algorithm) requires advance knowledge of maximum resource needs. Detection and recovery (resource allocation graph) is used in practice.