← Computer Science notes

Unit 2 · Operating Systems

Chapter 5 · Memory Management

pdcampus.in
pdcampus.in • DSSSB TGT CS
Beginner Topper
Memory Management - Paging to Page Replacement Unit 2 • Ch 4 • Paging, Segmentation, Virtual Memory, Page Replacement FIFO LRU Optimal
Beginner Story Mode Complete Notes

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.

1. Paging - Fixed Size Blocks - Most Important
Paging Fixed Size Blocks handwritten
Fig 1: Paging - Logical Address = Page No + Offset, Page Table maps Page→Frame, Physical = Frame + Offset, TLB fast cache

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.

Logical = p + d | Page Table: p → f | Physical = f + d | Page size 4KB = 2^12 offset 12 bits | TLB hit = fast, miss = slow | No external fragmentation, internal fragmentation last page
2. Segmentation - Variable Size Logical Units
Segmentation Variable Size Logical Units handwritten
Fig 2: Segmentation - Process divided into Code Data Stack Heap segments, Segment Table Base Limit, Logical (seg#, offset) → Physical = Base + Offset if offset < Limit

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.

Logical = s + d | Check d < Limit? If no → fault | If yes Physical = Base + d | Paging fixed no external internal yes | Segmentation variable internal no external yes
3. Virtual Memory - HDD as RAM - Demand Paging
Virtual Memory HDD as RAM handwritten
Fig 3: Virtual Memory - Virtual address space larger than physical RAM, Demand Paging only needed pages loaded, Page Fault handling steps, Thrashing

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).

Virtual Memory = RAM + HDD swap | Demand Paging: Bring page only when needed page fault → trap OS → find frame → load from HDD → update page table → restart | Thrashing = too many page faults swapping more than executing
4. Page Replacement Algorithms - Which Page to Throw?
Page Replacement FIFO LRU Optimal handwritten
Fig 4: Page Replacement - Reference String 7 0 1 2 0 3 0 4 2 3, 3 Frames, FIFO faults 8 Belady's anomaly, LRU faults 7, Optimal faults 6, Second-Chance Clock

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.

FIFO: oldest out, Belady's anomaly more frames may increase faults | LRU: least recently used out, good locality no Belady's | Optimal: farthest future out, theoretical optimal not implementable | Clock: circular queue reference bit second chance approximation of LRU
5. DSSSB Traps & PYQ Practice
  • Q: Logical address in paging? → Page Number + Offset
  • Q: Physical address? → Frame Number + Offset
  • Q: Page size 4KB offset bits? → 12 bits (2^12=4096)
  • Q: Page table maps? → Page → Frame
  • Q: Without TLB how many memory accesses? → 2 (1 page table +1 data)
  • Q: TLB? → Fast cache for page table entries TLB hit fast 1 access miss 2 accesses
  • Q: Paging fragmentation? → No external internal yes last page
  • Q: Segmentation logical? → Segment Number + Offset
  • Q: Segmentation translation? → Check offset < limit? If no fault if yes Physical=Base+offset
  • Q: Paging vs Segmentation? → Paging fixed size no user view internal fragmentation no external, Segmentation variable size user view external fragmentation no internal
  • Q: Virtual Memory? → RAM+HDD swap demand paging bring page only when needed
  • Q: Page fault steps? → Trap OS check valid find free frame if none replace victim if dirty write to HDD read needed page from HDD update page table restart instruction
  • Q: Thrashing? → Too many page faults CPU spends more time swapping than executing due to too many processes less frames per process
  • Q: How to prevent thrashing? → Reduce degree multiprogramming suspend processes working set model keep working set in RAM
  • Q: Reference string 7 0 1 2 0 3 0 4 2 3 3 frames FIFO faults? → 8
  • Q: Same LRU faults? → 7
  • Q: Same Optimal faults? → 6
  • Q: Belady's anomaly in which? → FIFO (more frames may increase faults)
  • Q: Optimal implementable? → No needs future knowledge theoretical benchmark
  • Q: Real OS uses which? → Clock/Second-Chance approximation of LRU lower overhead
Paging p+d Page→Frame f+d No external Internal yes TLB fast | Segmentation s+d check d

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=limit Paging vs Segmentation: Paging fixed transparent internal fragmentation no external Segmentation variable user view external fragmentation no internal Segmented Paging both benefits. Virtual Memory RAM+HDD swap Demand Paging bring only needed page Page Fault steps trap check valid find free frame if none replace victim dirty write HDD read needed HDD update page table restart Effective Access Time (1-p)*mem + p*page fault Thrashing too many faults swapping more than executing CPU utilization drops reduce degree multiprogramming Working Set last Δ pages keep in RAM. Page Replacement: Reference String 7 0 1 2 0 3 0 4 2 3 3 frames FIFO queue oldest out [7][7 0][7 0 1][2 0 1][2 0 1]H[2 0 3][2 0 3]H[4 0 3][4 2 3][4 2 3]H faults 8 Belady's anomaly more frames may increase faults, LRU stack least recent out [7][7 0][7 0 1][2 0 1][2 0 1]H[3 0 1][3 0 1]H[3 0 4][3 2 4][3 2 4]H faults 7 no Belady's good locality, Optimal farthest future out faults 6 minimum theoretical not implementable future needed, Clock circular queue reference bit second chance if bit 1 give second chance set 0 move hand else replace approximation LRU real OS uses Clock. If you remember Logical p+d Physical f+d check d

Topper Revision 1 Page = Full Chapter

Paging

  • Logical = p + d (page no + offset) | Page size 4KB=2^12 offset 12 bits | Page Table p→f | Physical = f + d
  • Steps: CPU (p,d) → page table f → physical f,d → access
  • Without TLB 2 accesses (page table + data) | With TLB hit fast 1 access miss 2 accesses | EAT = hit*(TLB+mem) + (1-hit)*(TLB+page table+mem)
  • No external fragmentation fixed frames any free usable | Internal fragmentation last page avg half page
  • Page table size large 32 bits page size 4KB 20 bits page no 1M entries 4MB per process → multilevel paging

Segmentation

  • Process divided into logical segments Code Data Stack Heap variable size
  • Segment Table Base Limit | Logical = s + d | Check d < limit? If no fault If yes Physical = Base + d
  • Example (1,150) Base 2500 Limit 300 150<300 valid Physical 2650 (1,400) 400>=300 fault
  • User view matches program structure easy protect/share | No internal fragmentation variable exact | External fragmentation holes need compaction
Paging vs Segmentation: Paging fixed transparent internal yes external no | Segmentation variable user view external yes internal no | Segmented Paging both benefits

Virtual Memory

  • RAM+HDD swap Virtual address space > physical RAM Demand Paging bring only when needed
  • Page Fault steps: trap OS check valid find free frame if none replace victim if dirty write HDD read needed HDD update page table restart instruction
  • EAT = (1-p)*mem + p*page fault time p=fault rate fault time >> mem time
  • Thrashing too many faults swapping more than executing CPU drops | Cause too many processes few frames | Solution reduce degree multiprogramming suspend Working Set keep working set in RAM
  • Working Set = pages referenced in last Δ time window

Page Replacement

AlgoVictimFaults Example 7 0 1 2 0 3 0 4 2 3 3 framesBelady'sNotes
FIFOOldest8Yes more frames may increase faultsSimple queue
LRULeast Recently Used7NoGood locality stack/timestamp
OptimalFarthest future6NoTheoretical optimal not implementable future needed benchmark
ClockReference bit 0 replace else second chance bit 1→0 move hand~LRUNoApproximation LRU low overhead real OS uses
Fewer faults = better | Optimal 6 < LRU 7 < FIFO 8 for example | Real OS uses Clock Second-Chance

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 • Paging vs Segmentation table
• 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

© 2026 pdcampus.in · All rights reserved. · Made with ❤️ for Indian students.