Introduction to Operating Systems
An Operating System (OS) is system software that manages computer hardware and software resources and provides common services for computer programs.
Key Functions
**Process Management**: Creation, scheduling, synchronization of processes
**Memory Management**: Allocation and deallocation of memory space
**File System Management**: Organization and manipulation of files
**Device Management**: I/O device communication
**Security**: Protection from unauthorized access
Process Management
Process vs Thread
| Feature | Process | Thread |
|---------|---------|--------|
| Memory Space | Separate | Shared |
| Creation Time | Slow | Fast |
| Context Switch | Expensive | Inexpensive |
| Isolation | Fully isolated | Shares with siblings |
| Communication | IPC needed | Direct memory access |
Process States
**New** — Process is created
**Ready** — Waiting for CPU
**Running** — CPU is executing
**Waiting** — Waiting for I/O or event
**Terminated** — Finished execution
CPU Scheduling Algorithms
**FCFS** (First Come First Serve): Non-preemptive, simple
**SJF** (Shortest Job First): Minimizes average waiting time
**Round Robin**: Time-sharing, fair allocation
**Priority Scheduling**: Higher priority runs first
**MLFQ**: Multiple queues with different priorities
Memory Management
Techniques
**Contiguous Allocation**: Process occupies contiguous memory
**Paging**: Fixed-size pages, eliminates external fragmentation
**Segmentation**: Variable-sized segments based on logical divisions
**Virtual Memory**: Allows execution of processes larger than physical RAM
Page Replacement Algorithms
**FIFO**: First-in, first-out (suffers from Belady's anomaly)
**LRU**: Least Recently Used (good performance, expensive)
**Optimal**: Replace page that won't be used longest (theoretical)
**Clock**: Approximation of LRU with reference bits
File Systems
File Allocation Methods
**Contiguous**: Fast sequential access, external fragmentation
**Linked**: No fragmentation, slow random access
**Indexed**: Fast random access, index block overhead
Directory Structure
Single-level
Two-level
Tree-structured
Acyclic graph
Deadlocks
Necessary Conditions
**Mutual Exclusion**: Resources are non-sharable
**Hold and Wait**: Process holds resources while waiting
**No Preemption**: Resources cannot be forcibly taken
**Circular Wait**: Circular chain of processes waiting
Handling Strategies
**Prevention**: Break one of the four conditions
**Avoidance**: Banker's algorithm
**Detection & Recovery**: Detect cycles, kill processes
**Ignore**: Ostrich algorithm (used in most systems)