Introduction to Operating Systems
An Operating System (OS) is the most critical software that runs on a computer. It acts as an intermediary between the user, applications, and the computer hardware. The OS manages hardware resources, provides services to application programs, and creates an environment where users can interact with the system efficiently.
From the moment you press the power button, the OS takes control — initializing hardware, loading essential services, and providing the interface that allows you to run applications. Whether it's Windows, macOS, Linux, Android, or iOS, every operating system performs the same fundamental functions: process management, memory management, file system management, device management, and security.
1. Operating System Layers & Architecture
1.1 The Layered Architecture
Modern operating systems are organized into layers, each providing services to the layer above while hiding implementation details from layers below.
1.2 Kernel Types
| Kernel Type | Description | Examples |
|---|---|---|
| Monolithic Kernel | All OS services run in kernel space. Fast but less stable. | Linux, Unix, Windows (hybrid) |
| Microkernel | Minimal kernel; services run in user space. More stable but slower. | QNX, macOS (hybrid), MINIX |
| Hybrid Kernel | Combines monolithic and microkernel approaches. | Windows NT, macOS XNU |
| Exokernel | Minimal abstraction; applications manage hardware directly. | MIT Exokernel, Nemesis |
- Kernel Space: Privileged mode where kernel executes. Direct hardware access. If a kernel process crashes, entire system crashes.
- User Space: Unprivileged mode where applications run. Isolated from hardware. If an app crashes, OS continues running.
2. Process Management
A process is a program in execution. The OS manages processes through creation, scheduling, and termination. Each process has its own address space, registers, and program counter.
2.1 Process States
2.2 Process Control Block (PCB)
Each process is represented by a PCB containing:
- Process ID (PID): Unique identifier
- Process State: New, Ready, Running, Waiting, Terminated
- Program Counter: Address of next instruction
- CPU Registers: Saved during context switches
- Memory Management Info: Page tables, segment tables
- I/O Status: Open files, devices
- Accounting Info: CPU time used, process priority
2.3 Context Switching
Context switching is the process of saving the state of a running process and loading the state of another process. The scheduler determines which process runs next.
// Simplified context switch illustration
struct Process {
int pid;
int program_counter;
int registers[16];
int *memory_map;
};
void context_switch(Process* current, Process* next) {
save_state(current); // Save current process state
restore_state(next); // Restore next process state
// CPU now executes next process
}
2.4 Scheduling Algorithms
The scheduler decides which process runs next. Key algorithms:
| Algorithm | Description | Pros | Cons |
|---|---|---|---|
| First-Come, First-Served (FCFS) | Processes execute in arrival order | Simple, fair | Convoy effect, poor response time |
| Shortest Job First (SJF) | Shortest CPU burst first | Optimal average waiting time | Starvation, needs burst time prediction |
| Round Robin (RR) | Time quantum; cyclic execution | Fair, good response time | Context switch overhead |
| Priority Scheduling | Higher priority runs first | Supports real-time tasks | Starvation possible |
| Multilevel Feedback Queue | Multiple queues with aging | Adaptive, balances I/O and CPU bound | Complex to implement |
- Processes: P1 (8ms), P2 (5ms), P3 (2ms)
- Execution order: P1(4) → P2(4) → P3(2) → P1(4) → P2(1)
- Average wait time: (4+3+6)/3 = 4.33ms
3. Threads and Concurrency
Threads are lightweight processes that share the same address space. They enable parallelism and efficient resource utilization.
3.1 Synchronization Primitives
Concurrent access to shared resources requires synchronization to prevent race conditions.
- Mutex (Mutual Exclusion): Only one thread can lock; others wait.
- Semaphore: Counting mechanism for resource management.
- Monitor: High-level synchronization with condition variables.
- Spinlock: Busy-waiting for short critical sections.
// Mutex example in C
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void* thread_function(void* arg) {
pthread_mutex_lock(&lock);
// Critical section - only one thread at a time
shared_counter++;
pthread_mutex_unlock(&lock);
return NULL;
}
3.2 Deadlock
Deadlock occurs when two or more processes wait indefinitely for resources held by each other.
- Mutual Exclusion: Resources cannot be shared
- Hold and Wait: Process holds resources while waiting for others
- No Preemption: Resources cannot be forcibly taken
- Circular Wait: Circular chain of waiting processes
4. Memory Management
Memory management is responsible for allocating and deallocating memory to processes, ensuring efficient utilization and protection.
4.1 Memory Allocation Techniques
- Contiguous Allocation: Single partition, multiple partitions (fixed/variable size)
- Paging: Physical memory divided into frames; logical memory into pages
- Segmentation: Logical divisions (code, data, stack) with variable sizes
- Segmentation with Paging: Combined approach (used in modern OS)
4.2 Virtual Memory
Virtual memory abstracts physical memory, giving each process its own address space. It enables:
- Running programs larger than physical RAM
- Process isolation and protection
- Efficient memory utilization
4.3 Page Replacement Algorithms
When physical memory is full, the OS must choose which page to evict:
- FIFO: First-in, first-out — simple but can evict frequently used pages
- LRU (Least Recently Used): Evicts page not used for longest time — effective but hardware support needed
- Optimal (Belady's): Evicts page not used for longest future — theoretical minimum
- Clock (Second Chance): Approximation of LRU using reference bits
5. File Systems
File systems organize and manage persistent storage, providing a hierarchical structure for data organization.
5.1 File System Structure
5.2 File Allocation Methods
- Contiguous Allocation: Files stored in contiguous blocks — fast sequential access, but fragmentation
- Linked Allocation: Each block points to next — no fragmentation, but slow random access
- Indexed Allocation: Index block contains pointers to data blocks — supports random access, overhead for small files
5.3 Popular File Systems
| File System | OS | Features |
|---|---|---|
| ext4 | Linux | Journaling, backward compatibility, large file support |
| NTFS | Windows | Journaling, encryption, compression, permissions |
| APFS | macOS/iOS | Copy-on-write, snapshots, encryption |
| ZFS | Solaris, FreeBSD | Built-in RAID, compression, snapshots, data integrity |
6. I/O Management
The OS manages all input/output operations, providing a uniform interface for devices while handling device-specific details.
6.1 I/O Techniques
- Programmed I/O: CPU polls device status — wasteful of CPU time
- Interrupt-Driven I/O: Device interrupts CPU when ready — more efficient
- Direct Memory Access (DMA): Device transfers data directly to memory without CPU involvement — most efficient
6.2 Device Drivers
Device drivers are kernel modules that provide a standard interface to hardware devices. They handle device-specific commands, interrupt handling, and data transfer.
7. Security and Protection
Operating systems implement multiple layers of security to protect resources and user data.
7.1 Protection Mechanisms
- User/ Kernel Mode: Prevents user programs from accessing critical system resources
- Memory Protection: Processes cannot access memory outside their address space
- Access Control Lists (ACL): Fine-grained permissions for files and resources
- Capabilities: Tokens granting specific privileges
7.2 Authentication and Authorization
- Authentication: Verifying user identity (passwords, biometrics, 2FA)
- Authorization: Determining what authenticated users can do (permissions, roles)
- Address Space Layout Randomization (ASLR) — prevents buffer overflow exploits
- Data Execution Prevention (DEP) — prevents code execution from data segments
- Secure Boot — ensures only signed OS components load
- Sandboxing — isolates applications (containers, virtual machines)
8. Real-World Operating Systems
8.1 Linux
Open-source, monolithic kernel. Dominant in servers, supercomputers, and embedded systems. Features: modular kernel, extensive hardware support, strong security.
8.2 Windows NT Architecture
Hybrid kernel with microkernel influences. Features: Win32 API, registry, NTFS, robust security model.
8.3 macOS / iOS (XNU)
Hybrid kernel combining Mach microkernel and BSD components. Features: Unix compliance, advanced graphics (Core Graphics), sandboxed apps.
8.4 Android (Linux-based)
Linux kernel with modified userspace. Features: application sandboxing, Dalvik/ART runtime, HAL for device support.
Conclusion
Operating System Architecture is the foundation upon which all computing experiences are built. From process scheduling to memory management, from file systems to security, the OS provides the essential services that enable applications to run efficiently and securely.
Understanding these concepts is crucial for system programmers, software architects, and anyone seeking to optimize application performance or troubleshoot complex system issues.