Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4: Virtual Memory — Paging, TLB & Page Replacement

Lesson 13 of 17 in the free Logical Organization of Computer-II notes on Siksha Sarovar, written by Rohit Jangra.

3.1 Why Virtual Memory?

Virtual memory gives every process the illusion of a huge, private, contiguous address space, while physical RAM holds only the currently useful pieces. Benefits: programs larger than RAM, isolation between processes, relocation without recompiling, and sharing. Mechanism: CPU issues virtual addresses (VA); the MMU translates them to physical addresses (PA) page by page.

3.2 Paging: The Address Split

Memory is divided into fixed-size pages (virtual) and frames (physical) of the same size — fixed size means no external fragmentation, only internal (average half a page wasted per process, the standard trade-off question).

32-bit VA, 4 KB pages:
offset = log2(4096) = 12 bits, VPN = 32 - 12 = 20 bits (2^20 = 1M pages)

VA 0x004032A7 -> VPN = 0x00403, offset = 0x2A7
Page table[0x00403] = frame 0x1F2
PA = frame || offset = 0x001F2 2A7 = 0x001F22A7

The page table (one entry per virtual page) holds: frame number + valid bit (page in RAM?) + dirty bit (modified?) + referenced bit (for replacement) + protection bits (R/W/X). A flat 1M-entry table per process is huge — hence multi-level page tables, which allocate table pages only for used regions.

3.3 TLB: Caching Translations

Without help, every access needs 2 memory reads (page table + data). The TLB (Translation Lookaside Buffer) is a small fully-associative cache of recent VPN→frame translations.

EMAT with TLB time 10 ns, memory 100 ns, TLB hit ratio 0.9:
hit  : 10 + 100            = 110 ns
miss : 10 + 100 + 100      = 210 ns   (extra read: page table)
EMAT = 0.9 x 110 + 0.1 x 210 = 99 + 21 = 120 ns

3.4 Page Faults and Replacement Algorithms

Accessing a page with valid = 0 triggers a page fault: trap to OS → find/free a frame (evicting a victim; write it back if dirty) → read the page from disk → update tables → restart the instruction. Since a fault costs milliseconds, the replacement algorithm matters enormously.

Worked reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 with 3 frames.

FIFO (evict the oldest):

Ref123412512345
Frames11,21,2,32,3,43,4,14,1,21,2,51,2,51,2,52,5,35,3,45,3,4
Fault?FFFFFFFHHFFH

FIFO = 9 faults. Now run FIFO with 4 frames on the same string: faults = 1,2,3,4 (compulsory), then 5,1,2,3,4,5 all miss → 10 faults. More frames, more faultsBelady's anomaly, unique to FIFO-like algorithms and a guaranteed exam talking point.

  • LRU (evict least recently used): same string, 3 frames → 10 faults. LRU obeys locality and never exhibits Belady's anomaly (it's a stack algorithm), but needs recency tracking (counters or a stack).
  • Optimal (evict the page needed farthest in the future): same string → 7 faults. Unimplementable (needs the future) but the yardstick every real algorithm is measured against.

Summary here: OPT 7 ≤ FIFO 9 ≤ LRU 10 on this particular string — note LRU is usually better than FIFO in practice; single strings can flip the order, another nuance worth stating.

3.5 Segmentation

Segmentation divides a program by logical units — code, data, stack — each a variable-length segment addressed as (segment number, offset). The segment table stores base and limit: if offset ≥ limit → protection trap, else PA = base + offset. Example: segment 2 has base 4300, limit 400 → address (2, 53) → 4300 + 53 = 4353; address (2, 500) → trap (500 ≥ 400).

AspectPagingSegmentation
Divisionfixed-size, physicalvariable-size, logical
Visible to programmernoyes
Fragmentationinternalexternal
Table entryframe numberbase + limit
Sharing/protection unitpage (coarse)whole logical module (natural)

Real systems (x86 heritage) combined both: segmentation with paging — segments provide the logical view, each segment is paged underneath. Finally, know thrashing: too few frames → constant faulting → CPU utilisation collapses while disks saturate.

🎯 Exam Focus

  1. A 24-bit virtual address uses 2 KB pages. Give the VPN/offset split and the number of page-table entries; translate VA 0x3A7C4 if its page maps to frame 0x5D.
  2. What does a page-table entry contain? Explain the role of the valid, dirty and referenced bits.
  3. With TLB access 20 ns, memory 150 ns and TLB hit ratio 0.85, compute the effective memory access time.
  4. For the reference string 7,0,1,2,0,3,0,4,2,3,0,3,2 with 3 frames, count faults under FIFO, LRU and Optimal.
  5. State Belady's anomaly and demonstrate it using the string of Section 3.4 with 3 vs 4 frames under FIFO.
  6. Differentiate paging from segmentation (any six points) and define thrashing.