5.4.11 · HinglishMemory Hierarchy & Caches

Virtual memory and paging

2,134 words10 min readRead in English

5.4.11 · Hardware › Memory Hierarchy & Caches


Virtual memory exist kyun karta hai?

Teen problems jo yeh ek saath solve karta hai:

  1. RAM kaafi nahi. Programs ko physical RAM se zyada memory ki zaroorat pad sakti hai. Virtual memory humein pretend karne deta hai ki RAM disk jaisi badi hai — inactive pages ko disk par swap karke.
  2. Multiple programs, ek RAM. Agar program A ne directly physical address 0x4000 use kiya aur program B bhi 0x4000 chahta, toh woh collide karte. Virtual memory har process ko uska apna address space deta hai — isolation & protection.
  3. Fragmentation. Programs ko contiguous chunks chahiye; RAM time ke saath gaps mein cut ho jaati hai. Paging "physical RAM mein contiguous hona zaroori hai" wali constraint hata deta hai.

Key objects kya hain?


Ek address translate kaise hota hai? (derive karo)

Maano page size bytes hai. bits ka ek virtual address do parts mein split hota hai.

Split kyun? Kyunki ek single page ke andar, virtual aur physical addresses bilkul same hote hain — ek page ko frame mein move karne se internal layout identical rehta hai. Sirf kaunsa page/frame hai yeh alag hota hai. Isliye:

  • Low bits = page ke andar offset. Yeh kabhi translate nahi hote.
  • High bits = Virtual Page Number (VPN). Sirf yahi map hota hai.

Split ko arithmetically derive karna. Ek virtual address ke liye:

Mask/shift kyun? se divide karne par low bits chhoot jaate hain (page number milta hai); remainder exactly wahi hai jo page ke andar bacha hai.

Phir frame lookup karo:

PFN ko se multiply kyun karte hain? Kyunki frame number physical byte se shuru hota hai. Exact byte par pahunchne ke liye untouched offset add karo.

Figure — Virtual memory and paging

TLB — translation slow kyun nahi hoti

Effective access time (derive karo). Maano = TLB lookup time, = memory access, hit ratio . Hit par hum TLB + memory pay karte hain. Miss par hum TLB + page-table walk (ek memory access, single-level) + real memory access pay karte hain:

Last term kyun? Miss penalty exactly ek extra hai table walk ke liye, miss probability se weighted.


Worked examples


Common mistakes (steel-manned)


Recall

Recall Active recall — answers chhupa lo
  • Ek virtual address kin do parts mein split hoti hai, aur kaunsa part translate hota hai? ⟶ VPN (translated) + offset (untranslated).
  • Single-level page table mein entries ki sankhya ka formula? ⟶ .
  • TLB kya hai aur yeh kaam kyun karta hai? ⟶ Recent translations ka cache; locality ki wajah se kaam karta hai.
  • Kya page fault ek error hai? ⟶ Nahi — normal event; page disk par hai / lazily allocated hai.
  • Fixed-size pages kyun? ⟶ Koi bhi page kisi bhi frame mein fit hota hai ⇒ external fragmentation nahi + simple arithmetic translation.
Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho tumhari book mein page numbers 1, 2, 3... hain (yahi tum dekhte ho). Lekin librarian actually un pages ko backroom mein random shelves par rakhta hai. Tum hamesha "page 5" maangoge; librarian secretly ek choti notebook dekhta hai jisme likha hai "page 5 shelf 200 par hai" aur use le aata hai. Tum kabhi shelves nahi dekhte — tum bas ek saaf book enjoy karte ho. Woh "choti notebook" page table hai, "shelf number" physical frame hai, aur agar ek page deep storage (disk) mein hai, toh librarian use laane daudta hai — woh choti wait page fault hai. Har bacche ko unki apni notebook milti hai, isliye bacche A ka "page 5" aur bacche B ka "page 5" bilkul alag shelves hain.


Connections

  • Memory hierarchy — VM, RAM aur disk ke beech ka level hai (RAM disk pages cache karta hai).
  • CPU caches — TLB ek specialized cache hai; VA vs PA, VIPT/PIPT cache indexing ko affect karta hai.
  • Page replacement algorithms — LRU/Clock decide karte hain ki fault par kaunsa frame evict karna hai.
  • Multi-level page tables — gigantic single-level tables ka fix.
  • Locality of reference — yahi wajah hai ki TLBs aur demand paging actually kaam karte hain.
  • Segmentation — paging ka alternative/complement.
Virtual memory ek line mein kya hai?
Ek illusion jo har process ko ek bada, private, contiguous address space deta hai, page-by-page physical RAM (aur disk) par mapped.
Virtual address split mein kaunsa part translate hota hai?
Sirf Virtual Page Number (high bits); offset (low bits) unchanged copy hota hai.
Page size diya ho, address se offset aur VPN kaise extract karte ho?
offset ; VPN .
PFN aur offset se physical address kaise banate ho?
.
-bit VA aur -byte pages wale single-level page table mein kitni entries hoti hain?
.
Page fault kya hota hai?
Ek trap jo tab raise hota hai jab referenced page abhi RAM mein nahi hai; OS use disk se load karta hai aur resume karta hai — normal event hai, crash nahi.
TLB kya hai aur yeh fast kyun hai?
Recent VPN→PFN translations ka ek chota associative cache; locality ki wajah se effective hai, >99% hit rates deta hai isliye translation almost free hai.
Hit ratio ke saath effective access time (single-level table)?
.
Variable regions ki jagah fixed-size pages kyun?
Koi bhi page kisi bhi frame mein fit hota hai ⇒ external fragmentation nahi, aur translation shift+lookup+add tak reduce ho jaata hai.
Bahut bade pages ka downside kya hai?
Internal fragmentation (waste hua RAM) aur fault par heavier per-fault disk I/O.
Process A ka VA 0x4000 process B ke saath clash kyun nahi karta?
Har process ka apna page table hota hai, isliye identical VAs alag physical frames mein map hoti hain (isolation).

Concept Map

solves

splits memory into

maps to

split into

split into

passed unchanged to

looked up in

yields

combined with offset

cached recent maps in

performs

page not in RAM triggers

fetches page from

Virtual Memory Illusion

RAM limits, collisions, fragmentation

Page - virtual block

Frame - physical block

Virtual Address

VPN high bits

Offset low bits

Physical Address

Page Table

Frame Number

TLB

MMU Hardware

Page Fault

Disk