Lesson 3 — Intermediate
OS: Memory Management
Memory Management OS ka important topic hai — process ko memory kaise allocate hoti hai, virtual memory kya hai, paging kaise kaam karti hai.
Memory basics
# Memory Layout of a Process:
+------------------+ High Address
| Stack | — Function calls, local variables
| — |
| |
| — |
| Heap | — Dynamic memory (malloc/new)
+------------------+
| BSS | — Uninitialized global variables
| Data | — Initialized global variables
| Text | — Program code (read-only)
+------------------+ Low Address
# Key Points:
- Stack: LIFO, automatic management, limited (1-8 MB)
- Heap: Dynamic, manual management, large
- Stack overflow: Zyada recursive calls — crash
- Memory leak: Heap se liya aur free nahi kiya
Virtual Memory
# Problem: Physical memory limited hai
? 4GB RAM hai, but 10 processes chal rahe hain
? Har process ko 4GB chahiye — impossible!
# Solution: Virtual Memory
? Har process ko lagta hai ki uske paas poora address space hai
? OS virtual addresses ko physical addresses mein map karta hai
? Process ko pata nahi physical memory kahan hai
# Benefits:
1. Memory isolation — ek process doosre ki memory nahi dekh sakta
2. More processes than physical memory
3. Memory protection — process apne code ke bahar access nahi kar sakta
4. Shared libraries — ek library ko multiple processes use kar sakte hain
# Address Translation:
Virtual Address — MMU (Memory Management Unit) — Physical Address
Page Table: Virtual page number — Physical frame number
Mental Model: Virtual Memory jaise ATM card. Tumhare paas ₹1000 cash nahi hain (physical memory), but ATM card se ₹10,000 tak withdraw kar sakte ho (virtual memory). Bank (OS) ensure karta hai ki tumhare account mein balance ho. Agar balance nahi toh ATM error dega (page fault).
Paging
# Paging: Memory ko fixed-size blocks mein divide karna
# Virtual Memory:
Page 0 | Page 1 | Page 2 | Page 3 (4KB each)
# Physical Memory:
Frame 0 | Frame 1 | Frame 2 | Frame 3 (4KB each)
# Page Table:
Virtual Page 0 — Physical Frame 2
Virtual Page 1 — Physical Frame 0
Virtual Page 2 — Not in memory (Page Fault!)
Virtual Page 3 — Physical Frame 1
# Page Fault:
? Jab virtual page physical memory mein nahi hota
? OS disk se page load karta hai (I/O operation)
? Page table update hota hai
? Process resume hota hai
# Page Replacement Algorithms:
1. FIFO: Oldest page replace karo
2. LRU (Least Recently Used): Sabse kam use hua page replace
3. Optimal: Future mein sabse kam use hone wala (theoretical)
4. Clock: Circular queue, reference bit use
Segmentation vs Paging
# Paging:
? Fixed-size blocks (4KB pages)
? Physical view — memory ko chunks mein todna
? Page table se mapping
? No logical meaning
# Segmentation:
? Variable-size blocks (logical units)
? Logical view — code segment, data segment, stack segment
? Segment table se mapping
ko meaningful segments dikhte hain
# Comparison:
| Feature | Paging | Segmentation |
|------------|---------------------|------------------------|
| Block Size | Fixed (4KB) | Variable |
| View | Physical | Logical |
| Table | Page Table | Segment Table |
| Fragmentation | Internal | External |
| Protection | Per page | Per segment |
# Modern OS: Dono use karte hain (Paged Segmentation)
Exercise
Question: Virtual Memory ka main purpose kya hai? Process ko kya deta hai jo physical memory nahi de sakti? (2-3 words)
Question: Page Fault hone pe OS page kahan se load karta hai? (1-2 words)
Common mistakes
- Virtual vs Physical confusion: Virtual memory process ko dikhta hai, physical memory actually available hoti hai. Address translation MMU karta hai.
- Page fault = error nahi: Page fault ek normal event hai — OS page load karta hai. Zyada page faults = slow performance (thrashing).
- Stack vs Heap: Stack automatic hai, heap manual. Stack overflow = crash, memory leak = heap issue.
- Paging vs Segmentation: Paging physical division hai, segmentation logical. Dono alag cheezein hain.
Lesson complete?
Memory Management samajh aa gayi? Ab CPU Scheduling seekhte hain — CPU kaunsa process pehle run karega.