Operating Systems
Virtual Memory; Paging; Segmentation
C-CAT
Virtual Memory
What is Virtual Memory?
Virtual Memory allows executing processes that are not completely in main memory.
- Processes see a virtual address space larger than physical RAM
- OS swaps parts of processes between RAM and disk
Benefits:
- Run programs larger than physical RAM
- More processes can run simultaneously
- Memory protection between processes
Swapping
Swapping moves entire processes between main memory and disk (swap space/page file).
RAM: [Active Process A] [Active Process B] [Free]
Disk: [Swapped Process C] [Swap Space]
When process A needs to swap out:
A → written to disk swap area
C → loaded from disk into RAM
Performance impact: Disk I/O is much slower than RAM → excessive swapping = "thrashing"
Virtual Address Space
Virtual Address Space (per process):
+------------------+
| Stack | 0xFFFF...
| ↓ |
| |
| ↑ |
| Heap |
+------------------+
| Data/BSS |
+------------------+
| Text | 0x0000...
+------------------+
Physical RAM:
+------------------+
| OS Kernel |
+------------------+
| Process Pages | (only needed pages loaded)
+------------------+
Demand Paging: Load pages into RAM only when accessed ("lazy loading").
Paging
What is Paging?
Paging divides:
- Physical memory into fixed-size blocks called frames
Virtual address space into fixed-size blocks called pages
Same size: page size = frame size (typically 4KB)
How Paging Works
Virtual Address: [Page Number | Offset]
| |
↓ |
Page Table |
+---------+ |
| Page 0→Frame 5| |
| Page 1→Frame 3| |
| Page 2→Frame 7| |
+---------+ |
| |
↓ ↓
Physical Address: [Frame Number | Offset]
Address Translation:
Virtual address: page_number=2, offset=100
Page Table: page 2 → frame 7
Physical address: frame_number=7, offset=100
Physical addr = 7 × 4096 + 100
Page Table Storage
Large page tables stored in memory; TLB (Translation Lookaside Buffer) caches recent translations.
TLB Hit: Address found in TLB → fast access TLB Miss: Access page table in memory → slower; TLB updated
Page Replacement Algorithms
When RAM is full and new page needed, which page to evict?
| Algorithm | Description | Advantage |
|---|---|---|
| FIFO | Replace oldest loaded page | Simple |
| LRU (Least Recently Used) | Replace least recently used page | Good performance |
| Optimal | Replace page not used for longest in future | Best (theoretical, not practical) |
| LFU (Least Frequently Used) | Replace page with lowest access frequency | Good for varying patterns |
| Clock (Second Chance) | FIFO with a reference bit | Approximates LRU; practical |
Thrashing
Thrashing occurs when process spends more time swapping pages than executing:
- Too many processes → each has too few frames
- Constant page faults → constant disk I/O
- CPU utilization drops drastically
Solution: Reduce degree of multiprogramming; use working set model.
Segmentation
What is Segmentation?
Segmentation divides virtual memory into variable-size segments based on logical divisions of a program:
Program Segments:
+------------------+
| Code Segment | ← program instructions
+------------------+
| Data Segment | ← global variables
+------------------+
| Stack Segment | ← function calls
+------------------+
| Heap Segment | ← dynamic allocation
+------------------+
Segmentation vs Paging
| Feature | Paging | Segmentation |
|---|---|---|
| Block size | Fixed (4KB) | Variable |
| Fragmentation | Internal | External |
| User view | Flat address space | Logical segments |
| Hardware support | Page table | Segment table |
| Protection | By page | By segment type |
Paged Segmentation
Modern systems combine both:
- Segmented with Paged Segments — each segment is paged
Used by x86-64 architecture
Continue learning
Related notes
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.