Unit 2 · Operating Systems
Imagine RAM is a big almirah with shelves. Programs are big boxes that need space. How to place boxes efficiently? That's Memory Management.
Paging = Cut all boxes into equal small pieces (pages) of fixed size 4KB, cut almirah shelves into equal frames 4KB, place pages into frames wherever free. Like cutting pizza into equal slices and placing slices into equal boxes.
Segmentation = Cut boxes into logical parts - code, data, stack, heap - each part different size variable. Like dividing almirah into sections - clothes section, books section, shoes section each different size.
Virtual Memory = Almirah is small (RAM 4GB) but you pretend it's big (8GB) by keeping some boxes in store room (HDD) and bringing only needed boxes to almirah when needed. Demand paging - bring page only when needed.
Page Replacement = Almirah full, new box comes, which old box to throw to store room? FIFO throws oldest box, LRU throws box not used for longest time, Optimal throws box that will be needed farthest in future.
Let's learn with diagrams so DSSSB numericals become easy.
What is Paging? Divide process into fixed size pages (e.g., 4KB each), divide physical memory into fixed size frames (same size 4KB), map pages to frames via page table. Eliminates external fragmentation.
Logical Address (generated by CPU): Two parts - Page Number (p) + Offset (d). Example: Page size 4KB = 4096 bytes = 2^12, so offset 12 bits, page number remaining bits. Logical address 32 bits = 20 bits page number + 12 bits offset.
Example: Logical address 0x00002A3C → Page Number = 2, Offset = 0xA3C (if page size 4KB).
Page Table: Table for each process maintained by OS, maps page number to frame number. Page table entry contains frame number + valid/invalid bit + protection bits.
Example: Page 0 → Frame 5, Page 1 → Frame 9, Page 2 → Frame 1 etc.
Physical Address (actual RAM): Frame Number (f) + Offset (d). Physical = f * page_size + d.
Example: Logical Page 2 Offset 0xA3C, Page Table says Page2→Frame1, Physical = Frame1 + 0xA3C = 1*4096+0xA3C.
Steps of Address Translation:
1) CPU generates logical address (p,d).
2) Look up page table using p to get frame number f. (If page table in RAM, need 1 memory access).
3) Physical address = f,d.
4) Access physical memory at f,d.
So without TLB, paging needs 2 memory accesses: 1 for page table, 1 for actual data.
TLB - Translation Lookaside Buffer - Fast Cache for Page Table:
TLB is small fast cache (hardware) that stores recently used page table entries. Like dictionary of recent translations. TLB hit → frame number found in TLB fast, only 1 memory access needed for data. TLB miss → need to access page table in RAM (2 accesses).
TLB hit ratio important. Effective Access Time = TLB hit ratio * (TLB access + memory access) + (1-hit ratio)*(TLB access + page table access + memory access).
Fragmentation in Paging:
- No External Fragmentation: Because fixed size pages/frames, any free frame can be used, no holes outside.
- Internal Fragmentation: Last page may not fully use frame, e.g., process needs 1025 bytes page size 1024 → needs 2 pages second page 1023 bytes wasted internal fragmentation average half page per process.
Page Table Size: If logical address 32 bits page size 4KB = 2^12 offset → page number 20 bits → page table entries 2^20 = 1M entries. Each entry 4 bytes → page table size 4MB per process big! Solution: Multilevel paging (page table of page table), Inverted page table, Hashed page table.
What is Segmentation? Divide process into logical segments of variable size - code segment, data segment, stack segment, heap segment etc. Each segment has name, base, limit. User view of memory - matches program structure.
Segment Table: Maps segment number to base address and limit. Each entry: Base (starting physical address), Limit (size of segment).
Example from Fig 2:
Segment 0 Code Base 1000 Limit 400
Segment 1 Data Base 2500 Limit 300
Segment 2 Heap Base 3500 Limit 350
Segment 3 Stack Base 5000 Limit 200
Logical Address: Segment Number (s) + Offset (d). Example: (1,150) means segment 1 offset 150.
Translation Steps:
1) Check if offset < limit? If offset >= limit → trap segmentation fault (invalid access).
2) If valid, Physical address = Base + offset.
Example: Logical (1,150) → Segment 1 Base 2500 Limit 300 → 150 < 300 valid → Physical = 2500+150=2650.
Example: Logical (1,400) → offset 400 >= limit 300 → fault, trap to OS.
Advantages of Segmentation:
- User view - matches logical structure code data stack.
- Easy to protect/share segments (each segment can have different protection read/write/execute, share code segment between processes).
- No internal fragmentation (variable size exact).
Disadvantages:
- External Fragmentation: Variable size segments leave holes outside as segments allocated/deallocated, need compaction.
- Allocation difficult, need to find contiguous hole large enough.
Paging vs Segmentation Comparison - DSSSB loves table:
- Paging: Fixed size page, no user view transparent, internal fragmentation, no external fragmentation, page table base+offset mapping via page number.
- Segmentation: Variable size segment, user view logical units code data stack, external fragmentation, no internal fragmentation, segment table base+limit check offset
Segmented Paging - Combination: Divide process into segments, each segment divided into pages. Each segment has its own page table. Logical = seg# page# offset. Benefits of both - logical view + no external fragmentation. Used in Intel x86.
RAM is costly small 4GB, HDD cheap large 1TB. Can we run programs larger than RAM? Yes using Virtual Memory - pretend RAM is big by using HDD as extension of RAM. Virtual address space (logical) larger than physical RAM, only needed pages kept in RAM, rest in HDD swap space.
Demand Paging: Bring page into RAM only when needed (on demand). Initially no pages in RAM, all pages in HDD. When CPU tries to access page not in RAM → page fault trap to OS, OS loads page from HDD to free frame in RAM, updates page table, restarts instruction. So only pages actually needed are loaded, saves RAM, allows more processes in RAM (higher degree of multiprogramming).
Page Fault Handling Steps - Must Remember Order:
1) CPU tries to access page, checks page table valid bit - if invalid (not in RAM) → trap to OS page fault exception.
2) OS checks if reference was valid (logical address valid, permission) or invalid (segfault). If invalid → kill process.
3) Find free frame in RAM. If no free frame, use page replacement algorithm to select victim page to throw to HDD.
4) If victim page modified (dirty bit set), write it to HDD swap space.
5) Read needed page from HDD into free frame.
6) Update page table: mark page valid, set frame number.
7) Restart instruction that caused fault - now page in RAM, will succeed.
Performance of Demand Paging:
Effective Access Time = (1-p)*memory access time + p*page fault time
Where p = page fault rate (0 to 1). Page fault time includes trap + disk read + restart, much larger than memory access (e.g., memory 100ns, page fault 8ms = 8,000,000 ns). So even small p large impact. Need low p.
Thrashing: When too many page faults, CPU spends more time swapping pages between RAM and HDD than executing. CPU utilization drops, processes wait for pages, OS thinks CPU free adds more processes, even more faults, vicious cycle. Like canteen almirah too small many boxes keep swapping boxes between almirah and store room more time swapping than eating.
Cause: Too many processes in RAM, each process gets too few frames, not enough to hold working set.
Solution: Reduce degree of multiprogramming (suspend some processes), use working set model or page fault frequency to control frames per process.
Working Set Model: Working set = set of pages process has referenced in last Δ time window. OS tries to keep working set of each process in RAM. If sum of working sets > RAM size → thrashing may occur. Use working set to decide how many frames each process needs.
Benefits of Virtual Memory:
- Run programs larger than physical RAM.
- More processes can be in RAM (only needed pages loaded), higher CPU utilization.
- Easy to share pages (code shared).
RAM full no free frame, new page needed, which existing page to throw to HDD? That's page replacement. Goal minimize page faults.
Example from Fig 4: Reference String 7 0 1 2 0 3 0 4 2 3, 3 Frames:
A) FIFO - First In First Out:
Throw oldest page first. Queue - first page loaded first to be thrown.
Steps 3 frames:
7→[7 - -] F fault
0→[7 0 -] F
1→[7 0 1] F
2→[2 0 1] F (7 oldest out)
0→[2 0 1] H hit (0 already in)
3→[2 0 3] F (1 out)
0→[2 0 3] H
4→[4 0 3] F (2 out)
2→[4 2 3] F (0 out)
3→[4 2 3] H
Total Faults 8 Hits 2
Simple but Belady's Anomaly: More frames may increase faults! With FIFO more frames not always fewer faults - anomaly, not good.
B) LRU - Least Recently Used:
Throw page not used for longest time. Uses locality - recently used pages likely to be used again soon.
Implementation: Stack or timestamp or reference bit. When page accessed move to top of stack, bottom is LRU.
Steps:
7→[7 - -]F 0→[7 0 -]F 1→[7 0 1]F 2→[2 0 1]F (7 LRU out) 0→[2 0 1]H 3→[3 0 1]F (2 LRU out) 0→[3 0 1]H 4→[3 0 4]F (1 LRU out) 2→[3 2 4]F (0 LRU out) 3→[3 2 4]H
Total Faults 7 Hits 3 - better than FIFO.
Good for locality, used widely, no Belady's anomaly (more frames never increase faults).
C) Optimal - Farthest Future Use - Theoretical Optimal:
Throw page that will be used farthest in future (or never used). Gives minimum faults optimal, but needs future knowledge - cannot know future reference string in advance, so not implementable, used as benchmark to compare other algorithms.
Steps:
7→[7 - -]F 0→[7 0 -]F 1→[7 0 1]F 2→[2 0 1]F (7 farthest future out? Actually 7 not used again? Check future: future references 0 3 0 4 2 3 - 0 used soon, 1 not used? Wait calculation) 0→[2 0 1]H 3→[2 0 3]F (1 farthest out) 0→[2 0 3]H 4→[2 4 3]F (0 out) 2→[2 4 3]H 3→[2 4 3]H
Faults 6 Hits 4 - minimum optimal.
Comparison:
FIFO 8 faults simple Belady's anomaly
LRU 7 faults good locality no Belady's
Optimal 6 faults best case impractical future needed
Fewer faults = better.
D) Second-Chance / Clock Algorithm - Approximation of LRU:
Circular queue with reference bit 0/1. Clock hand points to victim candidate. If reference bit 1 → give second chance set bit to 0 move clock hand next. If bit 0 → replace this page. Approximation of LRU lower overhead, real OS often uses Clock.
Idea: Balance simple implementation vs near-LRU performance.
Other Algorithms: LFU Least Frequently Used (count frequency), MFU Most Frequently Used, but LRU and Clock most used in real OS.
Final Story Recap: Almirah RAM shelves frames fixed 4KB pages fixed Paging: Logical p+d Page Table p→f Physical f+d 2 accesses without TLB TLB cache fast hit 1 access miss 2 accesses No external Internal last page wasted. Segmentation logical units Code Data Stack Heap variable size Segment Table Base Limit Logical s+d check d
Paging
Segmentation
Virtual Memory
Page Replacement
| Algo | Victim | Faults Example 7 0 1 2 0 3 0 4 2 3 3 frames | Belady's | Notes |
|---|---|---|---|---|
| FIFO | Oldest | 8 | Yes more frames may increase faults | Simple queue |
| LRU | Least Recently Used | 7 | No | Good locality stack/timestamp |
| Optimal | Farthest future | 6 | No | Theoretical optimal not implementable future needed benchmark |
| Clock | Reference bit 0 replace else second chance bit 1→0 move hand | ~LRU | No | Approximation LRU low overhead real OS uses |
PYQ Traps:
• Logical p+d Physical f+d
• Page size 4KB offset 12 bits
• Page table p→f
• Without TLB 2 accesses
• Paging no external internal yes
• Segmentation s+d check d
• Virtual RAM+HDD Demand Paging
• Page fault steps trap find frame load update restart
• Thrashing too many faults swapping > executing
• FIFO 8 LRU 7 Optimal 6 for 7 0 1 2 0 3 0 4 2 3
• Belady's FIFO only
• Optimal not implementable
• Real OS Clock