4.2.25Operating Systems

Paging — page - frame size, page table structure

2,448 words11 min readdifficulty · medium2 backlinks

WHAT is a page / frame?


HOW an address is split (derive it from scratch)

Suppose:

  • Virtual address is mm bits wide → virtual address space =2m= 2^m bytes.
  • Page size =2p= 2^p bytes.

Question: how many pages, and how do we read an address?

A page holds 2p2^p byte-positions, so to name a byte inside a page we need exactly pp bits → the offset dd, where 0d<2p0 \le d < 2^p.

The remaining mpm - p high bits must name which page. So:

number of pages=2m2p=2mp\text{number of pages} = \frac{2^m}{2^p} = 2^{\,m-p}

Translation step. The MMU looks up pnump_{\text{num}} in the page table to get the frame number ff. The physical address is:

phys=f×2p+d\text{phys} = f \times 2^{p} + d

Figure — Paging — page - frame size, page table structure

The Page Table

How big is a (flat) page table?

#entries=2mp,page-table size=2mp×(PTE size in bytes)\#\text{entries} = 2^{\,m-p}, \qquad \text{page-table size} = 2^{\,m-p} \times (\text{PTE size in bytes})


Smarter page-table structures (WHY they exist)

1. Multi-level (hierarchical) page table

Split the page number itself into chunks; each chunk indexes one level. Inner tables for unused regions simply don't exist (their outer entry is null).

2. Inverted page table

One entry per physical frame (not per virtual page). Size \propto RAM, not \propto virtual space. Search by (process-id, page-number), usually via a hash.

  • Why: total entries = number of frames, independent of how many processes or how big their spaces are.
  • Cost: lookup is no longer a direct index → needs hashing.

3. The TLB (why translation isn't slow)

EAT=(1miss rate)(tTLB+tmem)+miss rate(tTLB+Ltmem+tmem)\text{EAT} = (1-\text{miss rate})\cdot(t_{TLB}+t_{mem}) + \text{miss rate}\cdot(t_{TLB}+ L\cdot t_{mem} + t_{mem}) where LL = page-table levels walked on a miss.


Worked examples


Common mistakes


Recall Feynman: explain it to a 12-year-old

Imagine a giant book your program thinks it owns, page 1, 2, 3... in order. But the library only has random empty shelf slots. So the librarian (the OS) puts your "page 5" on shelf 20, your "page 6" on shelf 2 — wherever there's room. They keep a little notebook: "page 5 → shelf 20." When you ask for something on page 5, they check the notebook, walk to shelf 20, and count the same number of steps into that shelf as you wanted into the page. The page moves around; the step count inside the page never changes. That notebook is the page table, the shelves are frames, and your imaginary book pages are pages.


Flashcards

What is the invariant relating page size and frame size?
They are always equal (and a power of two), so any page fits in any free frame.
Why must page size be a power of two (2p2^p)?
So an address splits by bit-slicing: low pp bits = offset, high bits = page number — no division hardware needed.
For an mm-bit address and 2p2^p-byte pages, how many pages exist?
2mp2^{m-p}.
Given virtual address AA and page size 2p2^p, how do you get the page number and offset?
page number =A/2p=\lfloor A/2^p\rfloor (high bits, i.e. ApA\gg p); offset =Amod2p= A \bmod 2^p (low pp bits, i.e. A&(2p1)A \,\&\,(2^p-1)).
In translation, what part of the address is NOT translated?
The offset — it is copied unchanged; only the page number → frame number is looked up.
Physical address formula given frame ff, page size 2p2^p, offset dd?
f×2p+df\times 2^p + d.
Flat page-table size for 32-bit address, 4KB pages, 4-byte PTEs?
2202^{20} entries ×4=4\times 4 = 4 MB per process.
What problem does a multi-level page table solve?
Sparse address spaces — inner tables for unused regions are never allocated (null outer entry), saving memory.
What does an inverted page table index, and how does its size scale?
Indexed per physical frame; size scales with RAM, not with virtual address space; lookup uses hashing on (PID, page#).
What kind of fragmentation does paging eliminate, and what does it introduce?
Eliminates external fragmentation; introduces internal fragmentation (partly-empty last page, avg ½ page per region).
Name 4 typical PTE control bits.
Valid/present, dirty, referenced, protection (R/W/X) (also user/supervisor).
Why does a TLB exist?
To cache recent page→frame translations so most accesses avoid extra memory reads of the page table (exploits locality).

Connections

  • Virtual Memory — paging is its foundation; demand paging uses the valid bit.
  • TLB and Address Translation — speeds up the lookup derived here.
  • Segmentation — alternative/complement; variable size → external fragmentation.
  • Page Replacement Algorithms — use the referenced/dirty bits in the PTE.
  • Internal vs External Fragmentation — the trade-off paging makes.
  • Cache Memory and Locality — same locality principle the TLB exploits.

Concept Map

chopped into

chopped into

same size as

power of two enables

high m-p bits

low p bits

indexes

returns

f x 2^p + d

copied unchanged

any page in any frame

Virtual address space

Pages

Physical RAM

Frames

Page size = 2^p bytes

Address bit-slicing

Page number p_num

Offset d

Page table

Frame number f

Physical address

No external fragmentation

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, paging ka basic idea simple hai: har program sochta hai uske paas ek lamba, continuous memory hai jo address 0 se start hoti hai. Lekin actual RAM toh shared aur tukdo me bati hoti hai. Isliye OS program ki virtual memory ko fixed-size blocks me kaat deta hai — inhe page kehte hain. Aur physical RAM ko same size ke blocks me kaatta hai — inhe frame kehte hain. Page size aur frame size hamesha equal hote hain, aur power of two (jaise 4 KB = 2122^{12}). Iska fayda: koi bhi page kisi bhi khali frame me fit ho jata hai, isliye external fragmentation khatam ho jati hai.

Address ko kaise todte hain? Agar page size 2p2^p hai, toh address ke niche ke pp bits = offset (page ke andar kaunsa byte), aur upar ke bits = page number. Hardware me ye bas bit-shift aur mask hai, koi division nahi. Phir page table me page number daal ke uska frame number nikalte hain. Important baat: offset ko translate nahi karte — wo seedha copy ho jata hai. Sirf page number → frame number badalta hai. Apartment ka building number badalta hai, par andar ka flat number same rehta hai.

Problem ye hai ki ek flat page table bahut bada ho jata hai — 32-bit address aur 4KB pages me 2202^{20} entries, yaani 4 MB per process! Isliye multi-level page table use karte hain: page number ko bhi chunks me todo, aur jo regions use nahi ho rahe unke inner tables banao hi mat (null rakho). Iska alternative hai inverted page table (har frame ke liye ek entry, RAM ke size pe depend karta hai). Aur translation slow na ho isliye TLB ek chhota fast cache rakhta hai recent mappings ka.

Exam tip: internal fragmentation yaad rakho — last page kabhi-kabhi adha khali rehta hai, average 12\frac12 page waste. Aur ye galti mat karna ki "bada page hamesha accha hai" — bada page table chhota karta hai par waste badhata hai, isliye 4 KB sweet spot hai.

Test yourself — Operating Systems

Connections