Exercises — Virtual memory and paging
5.4.11 · D4· Hardware › Memory Hierarchy & Caches › Virtual memory and paging
Shuru karne se pehle, ek picture har problem ko jodti hai — ek address ki anatomy.

Recall Teen operations jo tum har jagah reuse karoge
Maano page size bytes hai. Ek virtual address ke liye:
- offset — neeche ke bits (page ke andar kaun sa byte).
- VPN — upar ke bits (kaun sa page).
- Rebuild karo: .
Neeche sab kuch inhi teen lines ka dhyaan se application hai.
Level 1 — Recognition
Exercise 1.1
Inmein se kaun sa MMU dwara translate hota hai, aur kaun sa unchanged copy hota hai: VPN ya offset?
Recall Solution
VPN translate hota hai (woh page table mein look up karke PFN ban jaata hai). Offset unchanged copy hota hai.
Kyun: kisi page ko frame mein move karna ek rigid shift hai — page ka byte 5, frame ka bhi byte 5 rehta hai. Sirf base badalta hai. Figure s01 dekho: peele offset bits seedha pass ho jaate hain, coloured VPN bits table se guzarte hain.
Exercise 1.2
Ek system 4 KiB pages use karta hai. Offset kitne bits ka hai, aur kya hai?
Recall Solution
bytes. Toh aur offset 12 bits ka hai.
Kyun: offset ko ek page ke andar ka har byte naam dena hoga. bytes per page hone par, se tak count karne ke liye exactly bits chahiye.
Level 2 — Application
Exercise 2.1
Page size (). Virtual address hai . Ise VPN aur offset mein split karo.
Recall Solution
Neeche ke bits aakhiri teen hex digits (har hex digit bits ka hota hai, toh ). Kyun last-3-digits shortcut kaam karta hai: bits se right shift karna aakhiri hex digits drop karne ke barabar hai. Toh VPN , offset .
Exercise 2.2
2.1 se aage: page table VPN PFN map karta hai. Physical address kya hai?
Recall Solution
Kyun PFN ko 12 se shift karte hain: frame number physical byte par shuru hota hai. Left shift by se woh base address banta hai (); untouched offset ko OR karne se exact byte milta hai.
Level 3 — Analysis
Exercise 3.1
Ek machine mein -bit virtual address, pages, aur har page-table entry bytes ki hai. Single-level page table ke liye entries ki sankhya aur har process ka total size compute karo.
Recall Solution
Entries (ek million ke lagbhag). Kyun: har virtual page ke liye ek entry; (address-space size)/(page size) pages hote hain. Kyun dhyan dein: processes ke saath woh RAM sirf translation tables par kharach hoti hai — yahi pressure Multi-level page tables ko motivate karta hai.
Exercise 3.2
, , TLB hit ratio (single-level table). Effective access time (EAT) nikalo.
Recall Solution
Formula dobara banao: hit par, TLB + memory pay karo. Miss par, TLB + ek table walk () + asli access () pay karo. Kyun sirf ns: extra table walk sirf unhi accesses par pay hoti hai jo TLB miss karte hain. Saare accesses ka average nikalo toh hai. Isliye TLB, Locality of reference exploit karke, translation ko almost free bana deta hai.
Level 4 — Synthesis
Exercise 4.1
-bit machine par two-level page table jo pages use karta hai, -bit VPN ko -bit outer index aur -bit inner index mein split karta hai. Ek process sirf apna pehla virtual memory use karta hai (ek page). Maan lo -byte entries hain aur har table (outer ya inner) ek page mein fit hoti hai — actually kitni page-table memory chahiye, count karo.

Recall Solution
Kyun VPN split karte hain: taaki inner tables sirf unhi regions ke liye allocate ho jinhein process actually touch kare. Figure s02 dekho — outer table mein slots hain lekin sirf ek real inner table ki taraf point karta hai.
- Outer table hamesha exist karta hai: entries bytes bytes .
- Exactly ek inner table chahiye (process ek page touch karta hai, jo ek outer slot ke neeche rehta hai): aur . Kyun yeh single-level cost ko crush karta hai: single-level ko chahiye tha (Ex 3.1). Two-level ko sparse process ke liye chahiye — saving. Unused outer slots simply khali rehte hain (koi inner table allocate nahi hoti).
Exercise 4.2
Wahi machine (two levels, , ). TLB miss par MMU ko dono levels walk karne padte hain (do memory reads) real access se pehle. Hit ratio ke saath, EAT nikalo.
Recall Solution
Kyun do extra reads: ek miss ab outer table padhta hai (1 access) phir inner table (1 access) PFN lane ke liye, phir real data. Kyun pehle ke ki jagah sirf : deeper walk cost karta hai ki jagah, lekin yeh abhi bhi sirf un par pay hota hai jo miss karte hain: . Locality ek lambi tree ko bhi sasta rakhti hai.
Level 5 — Mastery
Exercise 5.1
Design check. Tumhe -bit virtual address space pages ke saath support karna hai, aur tum chahte ho ki har level par har page table exactly ek page mein fit ho, -byte entries use karte hue. Page table ko kitne levels chahiye?
Recall Solution
Step 1 — offset. , toh VPN bits ka hai. Step 2 — har level mein kitne index bits. Ek page mein entries aati hain, toh har level VPN bits consume karta hai. Step 3 — divide karo. Levels levels. Answer: 4 levels (yeh exactly real x86-64 ka 4-level scheme hai, PML4 → PDPT → PD → PT). Kyun poora nikalta hai: exactly se divisible hai; agar nahi hota, toh top level kam bits use karta (ek partially-filled table).
Exercise 5.2
Capstone. Us -bit, -level machine par (, ), ek program ek huge array mein stride karta hai jo almost har access par ek fresh page touch karta hai, isliye TLB hit ratio par gir jaata hai. EAT compute karo, aur ek sentence mein batao ki program ne kaun sa principle violate kiya.
Recall Solution
Ek miss ab chaar levels walk karta hai () phir real access hoti hai: daalo: Yeh raw memory time se zyada slow hai. Principle violated: poor locality of reference — har access par ek naya page touch karke, program TLB ko defeat kar deta hai (dekho Locality of reference), toh almost har access poora -level walk pay karta hai.
Recall
Recall Ek-line self-test
- VA ka kaun sa part translate hota hai? ⟶ VPN; offset copy hota hai.
- -bit VA, pages ke liye single-level table entries? ⟶ ().
- EAT formula, single level? ⟶ .
- Multi-level tables memory kyun bachate hain? ⟶ inner tables lazily allocate hoti hain, sirf used regions ke liye.
- -bit VA, pages, -byte entries, tables ke liye levels? ⟶ .