CSS Prepare

Operating Systems: Concepts and Mechanisms

9 min read

An operating system (OS) is the software layer that manages hardware resources and provides services to applications. Without it, every program would have to interact directly with raw hardware. For CSS Computer Science, command of OS fundamentals — processes, memory, scheduling, file systems — is essential.

Operating System

A program that acts as an intermediary between user applications and the computer hardware, managing CPU, memory, I/O devices, and storage to provide an efficient and convenient computing environment.

Functions of an OS

  1. Process management — creation, scheduling, termination.
  2. Memory management — allocation, virtual memory.
  3. File system management — directories, permissions.
  4. Device management — drivers, interrupts.
  5. Security and protection — authentication, access control.
  6. User interface — shells, GUIs.
  7. Networking — sockets, protocols.

Types of operating systems

  • Batch — jobs run sequentially without user interaction.
  • Time-sharing — multiple users share CPU via rapid switching.
  • Multiprogramming — multiple programs reside in memory simultaneously.
  • Real-time — strict timing guarantees (hard or soft).
  • Distributed — runs on multiple machines.
  • Embedded — runs on devices (IoT, routers).
  • Mobile — Android, iOS.

Major desktop/server OS families

  • Unix-like: Linux (Red Hat, Ubuntu, Debian), BSD, macOS.
  • Windows NT family: Windows 10/11, Windows Server.
  • Pakistan's government systems and ISPs run predominantly on Linux (Red Hat / CentOS / Ubuntu) for servers and a mix of Windows on desktops.

Process and thread

A process is a program in execution with its own address space. A thread is a lightweight execution unit within a process sharing code, data and files but with its own stack and registers.

Process states

New → Ready → Running → Waiting → Ready → Running → Terminated

Transitions are triggered by events (I/O completion, time-quantum expiry, system calls).

Process Control Block (PCB)

Stores process ID, state, program counter, CPU registers, memory pointers, open files, scheduling info.

CPU scheduling

The OS selects which ready process runs on the CPU. Scheduling algorithms:

AlgorithmTypeIdeaPros / Cons
FCFSNon-preemptiveFirst-come, first-servedSimple; convoy effect
SJFNon-preemptiveShortest job firstOptimal avg wait; starvation
SRTFPreemptiveShortest remaining timeOptimal; needs prediction
PriorityEitherHighest priority firstFlexible; starvation
Round RobinPreemptiveFixed time quantumFair; quantum size matters
Multilevel QueueEitherMultiple queues with prioritiesFlexible; complex
MLFQPreemptiveProcess moves between queuesAdaptive; widely used
Key Points
  • Turnaround time = completion − arrival.
  • Waiting time = turnaround − CPU burst.
  • Response time = first run − arrival.
  • Throughput = jobs completed per unit time.
  • A small RR quantum increases context-switch overhead; too large makes it FCFS.

Synchronisation

When threads share resources, race conditions arise. Synchronisation primitives:

  • Critical section — code accessing shared data, must run atomically.
  • Mutex / lock — only one thread enters at a time.
  • Semaphore (Dijkstra) — counter with P (wait) and V (signal); binary or counting.
  • Monitor — high-level construct combining lock + condition variables.
  • Spinlock — busy-wait variant, for very short critical sections.

Classical problems

  1. Producer-Consumer — bounded buffer with mutex + two semaphores.
  2. Readers-Writers — multiple readers, exclusive writer.
  3. Dining Philosophers — five philosophers, five chopsticks; deadlock avoidance.

Deadlock

Four Coffman conditions, all required:

  1. Mutual exclusion
  2. Hold and wait
  3. No preemption
  4. Circular wait

Strategies: prevention (eliminate at least one condition), avoidance (Banker's algorithm), detection and recovery, ignore (most OS).

Memory management

Address binding

Logical addresses (generated by CPU) are mapped to physical addresses by the Memory Management Unit (MMU).

Allocation strategies

  • Contiguous — fixed/dynamic partitions; suffers from external fragmentation.
  • Paging — fixed-size frames + pages; eliminates external fragmentation; internal fragmentation possible.
  • Segmentation — variable-size logical segments (code, data, stack).
  • Paged segmentation — combination.

Virtual memory

Memory addresses can exceed physical RAM. Pages not in RAM live on disk (swap space). When a referenced page is missing, a page fault occurs.

Page-replacement algorithms

  • FIFO — replace oldest page; suffers from Belady's anomaly.
  • LRU — least recently used; widely approximated.
  • Optimal (OPT) — replace the page used farthest in future; benchmark only.
  • Clock (Second-chance) — practical approximation of LRU.

Thrashing

When the system spends more time swapping pages than executing — a sign of insufficient frames per process.

File system

A file system organises bytes on disk into files and directories.

File operations

Create, read, write, seek, delete, open, close.

Directory structure

Single-level, two-level, hierarchical (tree), graph (with links).

Allocation methods

  • Contiguous — fast sequential access; external fragmentation.
  • Linked — each block points to next; no random access.
  • Indexed — index block holds pointers (Unix inodes).

Major file systems

  • ext4 (Linux), NTFS (Windows), APFS (macOS), FAT32/exFAT (portable), ZFS/Btrfs (advanced features).

I/O subsystem

  • Polling — CPU repeatedly checks device.
  • Interrupt-driven — device signals when ready.
  • DMA (Direct Memory Access) — bulk transfers bypass CPU.

Security and protection

  • Authentication — passwords, tokens, biometrics.
  • Access control — DAC, MAC, RBAC (role-based).
  • Logging and auditing.
  • Kernel separation — user vs. kernel mode.

Modern OS topics

  • Virtualisation — hypervisors (Type 1: VMware ESXi, KVM; Type 2: VirtualBox).
  • Containers — Docker, LXC; share kernel, isolate user space.
  • Microkernel vs. monolithic kernel vs. hybrid (Windows NT).
  • Real-time OS for embedded systems (FreeRTOS, VxWorks).
  • Distributed OS and cloud platforms.

A common CSS pitfall is to confuse paging (fixed-size physical/logical mapping) with segmentation (variable-size logical units). Remember: paging deals with addresses, segmentation with meaning (code, stack, data).

Relevance to Pakistan

Government IT systems (NADRA's identity database, FBR's IRIS, the Punjab Information Technology Board's services) run on Linux servers configured for high availability. The Ministry of IT and Telecommunication's Cloud First Policy mandates progressive migration of public-sector workloads to cloud infrastructure. Officers conversant with OS fundamentals can scrutinise procurement specifications and security architectures intelligently.

Operating Systems: Concepts and Mechanisms — Computer Science CSS Notes · CSS Prepare