Operating Systems: Concepts and Mechanisms
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.
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
- Process management — creation, scheduling, termination.
- Memory management — allocation, virtual memory.
- File system management — directories, permissions.
- Device management — drivers, interrupts.
- Security and protection — authentication, access control.
- User interface — shells, GUIs.
- 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:
| Algorithm | Type | Idea | Pros / Cons |
|---|---|---|---|
| FCFS | Non-preemptive | First-come, first-served | Simple; convoy effect |
| SJF | Non-preemptive | Shortest job first | Optimal avg wait; starvation |
| SRTF | Preemptive | Shortest remaining time | Optimal; needs prediction |
| Priority | Either | Highest priority first | Flexible; starvation |
| Round Robin | Preemptive | Fixed time quantum | Fair; quantum size matters |
| Multilevel Queue | Either | Multiple queues with priorities | Flexible; complex |
| MLFQ | Preemptive | Process moves between queues | Adaptive; widely used |
- 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
- Producer-Consumer — bounded buffer with mutex + two semaphores.
- Readers-Writers — multiple readers, exclusive writer.
- Dining Philosophers — five philosophers, five chopsticks; deadlock avoidance.
Deadlock
Four Coffman conditions, all required:
- Mutual exclusion
- Hold and wait
- No preemption
- 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.