G27
Self-checkedTier 1 13 min reados.paging

Paging and address translation

Non-contiguous allocation by fixed-size pages and frames: splitting a logical address into page number and offset, the page table, the translation to a physical address, the sizes of address spaces and page tables, internal fragmentation, the choice of page size, protection bits and sharing.

Why it exists

With contiguous allocation a process needs one big hole, holes fragment, and compaction is expensive. Paging removes the requirement of contiguity. Physical memory is cut into equal fixed-size frames, the logical address space into pages of the same size, and any page can go into any free frame. A page table records where each page is. The programmer still sees one contiguous address space; the hardware translates every address. Paging is the basis of virtual memory and is the most examined topic of operating systems in GATE, almost always as a calculation: how many bits is the page number, how big is the page table, what physical address does this logical address map to.

Prerequisites

You need the aim of memory management, fragmentation and the idea of logical and physical addresses from memory management basics and process address space.

Precise definitions

Pages and frames. Physical memory is divided into frames of size 2n2^n bytes. The logical address space of a process is divided into pages of the same size. A page is loaded into any free frame; the frames of one process need not be adjacent.

Logical address. For mm-bit logical addresses and a page size of 2n2^n bytes:

p⏟m−n bits: page numberd⏟n bits: offset\underbrace{p}_{m - n \text{ bits: page number}} \quad \underbrace{d}_{n \text{ bits: offset}}

The page number pp is the high-order m−nm - n bits; the offset dd is the low-order nn bits. Equivalently p=⌊L/2n⌋p = \lfloor L / 2^n \rfloor and d=L mod 2nd = L \bmod 2^n.

Page table. An array, one entry per page, indexed by the page number. Entry pp holds the frame number ff in which page pp is stored, plus control bits (valid/invalid, read/write/execute protection, and for virtual memory referenced and modified (dirty)).

Translation. For a logical address with page number pp and offset dd: look up f=page table[p]f = \text{page table}[p] (if the valid bit is 0, a page fault), and form the physical address

P=f⋅2n+d,P = f \cdot 2^n + d,

which in bits is simply the frame number followed by the unchanged offset. The offset is not changed, and neither is its size.

Sizes (all from the number of bits).

QuantityFormula
logical address space2m2^m bytes
page size = frame size2n2^n bytes
number of pages (page table entries)2m−n2^{m-n}
physical memory with kk-bit physical addresses2k2^k bytes
number of frames2k−n2^{k-n}
bits of a frame numberk−nk - n
page table sizenumber of entries ×\times entry size
minimum entry sizethe frame-number bits plus the control bits, rounded up to whole bytes in practice

Where the page table is. For speed the page table of the running process is conceptually in registers, but a large one lives in main memory, and a page-table base register (PTBR) (and a length register) points to it. Every memory reference then needs two memory accesses (one for the page-table entry, one for the data) unless a translation cache, the TLB, holds the entry (TLB). A context switch loads the PTBR of the new process.

Fragmentation. Paging has no external fragmentation (any free frame will do), but it has internal fragmentation: the last page of a process is usually only partly used. If a process needs SS bytes with pages of PP bytes, it takes ⌈S/P⌉\lceil S/P \rceil pages and wastes (−S) mod P(-S) \bmod P bytes. Averaged over random sizes the waste is about P/2P/2 per process (exactly (P−1)/2(P - 1)/2 over all residues).

Choice of page size. Smaller pages waste less in the last page but need larger page tables (and more disk transfers per byte); larger pages shrink the table but waste more. For an average process size ss and page-table entry size ee, the overhead per process is s ep+p2\dfrac{s\,e}{p} + \dfrac{p}{2}, minimized at

p=2 s e.p = \sqrt{2\,s\,e}.

For s=1s = 1 MB and e=8e = 8 bytes that is 4096 bytes: this is why 4 KB is a common page size.

Protection and sharing. The valid bit marks which pages belong to the process; protection bits allow read-only or execute-only pages. Pages can be shared between processes by putting the same frame number in several page tables (for example the code of a program or a library, which must be reentrant, not modified while running).

Frame table. The operating system keeps a table with one entry per frame: whether it is free, and if allocated, to which process.

How it works

The animation uses a 14-bit logical address space with 1 KB pages: the 4 high bits are the page number (16 pages) and the 10 low bits the offset. The physical memory has 32 KB (32 frames, 5-bit frame numbers). Five addresses are translated; two of them hit invalid pages. Watch the frame number replace the page number while the offset is copied unchanged.

Paging: translating logical addresses: start

Translation computed, not typed
page table (16 pages)
pageframevalid
051
191
221
3-0
4171
531
6-0
7201
811
9301
10-0
11121
12-0
13-0
14-0
15-0

No address yet.

Step 0

A 14-bit logical address space (16 KB) and a 15-bit physical memory (32 KB). The page size is 1 KB = 2^10, so the low 10 bits are the OFFSET and the upper 4 bits are the PAGE NUMBER (16 pages). A frame number needs 5 bits (32 frames). The page table maps each page to a frame; the valid bit says whether the page is in memory. The hardware does this for EVERY memory reference.

0 / 15
computed by script: the algorithm or the translation is executed, one event per step. python3 scripts/gen-os-mem-traces.py

Worked examples

EX 1Translating by program

Easy

The same page table and addresses as in the animation, translated in C by shifting and masking, then with division and remainder, and with the standard size calculations.

#include <stdio.h>
 
#define OFFSET_BITS 10
#define PAGES 16
 
int main(void) {
    /* page table of a process: frame number of each page, -1 = not in memory */
    int frame[PAGES] = {5, 9, 2, -1, 17, 3, -1, 20, 1, 30, -1, 12, -1, -1, -1, -1};
    unsigned logical[5] = {2565, 5123, 3082, 10216, 15360};
    for (int i = 0; i < 5; i++) {
        unsigned p = logical[i] >> OFFSET_BITS, d = logical[i] & ((1u << OFFSET_BITS) - 1);
        if (frame[p] < 0) { printf("logical %5u: page %2u offset %4u -> page fault\n", logical[i], p, d); continue; }
        unsigned physical = ((unsigned)frame[p] << OFFSET_BITS) | d;
        printf("logical %5u: page %2u offset %4u -> frame %2d -> physical %5u\n", logical[i], p, d, frame[p], physical);
    }
    /* the same arithmetic with division and remainder */
    printf("5123 = %u * 1024 + %u\n", 5123u / 1024u, 5123u % 1024u);
    /* sizes: 32-bit logical addresses, 4 KB pages, 4-byte page table entries */
    unsigned long long pages = 1ull << (32 - 12);
    printf("pages in the address space: %llu, page table size: %llu KB\n", pages, pages * 4 / 1024);
    /* a process of 100000 bytes needs how many 4096-byte pages, and how much is wasted in the last? */
    unsigned size = 100000, page = 4096;
    unsigned need = (size + page - 1) / page, last = size - (need - 1) * page;
    printf("process of %u bytes: %u pages, last page uses %u bytes, internal fragmentation %u bytes\n", size, need, last, page - last);
    /* the page size minimising (page table entries per process + wasted half page): s = 1 MB process, e = 8-byte entries */
    double s = 1 << 20, e = 8, best = 0, best_cost = 1e30;
    for (double pz = 256; pz <= 65536; pz *= 2) {
        double cost = s * e / pz + pz / 2;
        if (cost < best_cost) { best_cost = cost; best = pz; }
    }
    printf("best page size among powers of two: %.0f bytes (overhead %.0f bytes per process)\n", best, best_cost);
    return 0;
}
logical  2565: page  2 offset  517 -> frame  2 -> physical  2565
logical  5123: page  5 offset    3 -> frame  3 -> physical  3075
logical  3082: page  3 offset   10 -> page fault
logical 10216: page  9 offset 1000 -> frame 30 -> physical 31720
logical 15360: page 15 offset    0 -> page fault
5123 = 5 * 1024 + 3
pages in the address space: 1048576, page table size: 4096 KB
process of 100000 bytes: 25 pages, last page uses 1696 bytes, internal fragmentation 2400 bytes
best page size among powers of two: 4096 bytes (overhead 4096 bytes per process)

Reading it. Address 5123: 5123=5×1024+35123 = 5 \times 1024 + 3, so page 5, offset 3; page 5 is in frame 3, so the physical address is 3×1024+3=30753 \times 1024 + 3 = 3075. Page 3 and page 15 are not in memory: page faults. A 32-bit address with 4 KB pages has 2202^{20} pages; at 4 bytes per entry the page table is 4 MB for each process: too big to keep in registers (and a reason for multilevel tables). A 100000-byte process needs 25 pages (24.4 rounded up) and wastes 2400 bytes in the last.

EX 2Bits and sizes

Easy

Logical addresses are 32 bits, physical addresses 36 bits, pages 4 KB (2122^{12}). Offset: 12 bits. Page number: 32−12=2032 - 12 = 20 bits, so 2202^{20} pages and a page table with 2202^{20} entries. Frames: 236−12=2242^{36 - 12} = 2^{24}, so a frame number needs 24 bits. With 4 bytes per entry (enough for 24 bits of frame number plus 8 control bits) the page table is 220×4=42^{20} \times 4 = 4 MB. Physical memory is 2362^{36} bytes = 64 GB. The page size is the same for pages and frames, so the offset is the same in both addresses.

EX 3How much is wasted?

Medium

A process of 100000 bytes with pages of 4096 bytes needs ⌈100000/4096⌉=25\lceil 100000/4096 \rceil = 25 pages; the first 24 are full (24×4096=9830424 \times 4096 = 98304 bytes) and the last holds 100000−98304=1696100000 - 98304 = 1696 bytes, so 4096−1696=24004096 - 1696 = 2400 bytes are wasted. With pages of 1024 bytes the process needs 98 pages (the last holds 672 bytes) and wastes 352 bytes: smaller pages waste less. Over all possible sizes the average waste is (P−1)/2(P - 1)/2 per process: about half a page, which gives the rule of thumb "internal fragmentation ≈\approx page size / 2 per process".

EX 4What page size is best?

Medium

Each process has an average size s=1s = 1 MB and each page-table entry takes e=8e = 8 bytes. A process of size ss with page size pp has s/ps/p entries, costing s e/ps\,e/p bytes of page table, and wastes about p/2p/2 bytes in the last page. Total overhead =s e/p+p/2= s\,e/p + p/2. At p=4096p = 4096: 220×8/4096+2048=2048+2048=40962^{20} \times 8/4096 + 2048 = 2048 + 2048 = 4096 bytes. At p=2048p = 2048 it is 4096+1024=51204096 + 1024 = 5120; at p=8192p = 8192: 1024+4096=51201024 + 4096 = 5120. So 4096 is best, and 2se=224=4096\sqrt{2 s e} = \sqrt{2^{24}} = 4096 confirms it (a search over page sizes agrees). The overhead is smallest where the two terms are equal.

EX 5Two accesses per reference

GATE level

A system keeps its page table in main memory (no TLB). A memory access takes 100 ns. Each reference needs one access for the page table entry and one for the data: 2×100=2002 \times 100 = 200 ns, twice as slow as with no translation. A TLB that caches recent entries brings the average close to 100 ns (TLB and effective access time). With a two-level page table the cost without a TLB is three accesses, 300 ns. Another typical question: "the page table is stored in memory; a program of 20 instructions..." count each reference (instruction fetches and data accesses) as two memory accesses.

Cheat box

Cheat box

  • Page size == frame size =2n= 2^n. Logical address == page number (m−nm - n bits) ∥\| offset (nn bits). p=L÷2np = L \div 2^n, d=L mod 2nd = L \bmod 2^n.
  • Physical address =f⋅2n+d= f \cdot 2^n + d (frame number ∥\| same offset).
  • Number of pages =2m−n= 2^{m-n} == page table entries. Number of frames =2k−n= 2^{k-n}. Frame number has k−nk - n bits.
  • Page table size == entries ×\times entry size. Every process has its own table; PTBR points to it.
  • No external fragmentation; internal fragmentation ≈\approx page size / 2 per process (last page).
  • Without a TLB each reference costs 2 memory accesses (1 for the page table, 1 for the data).
  • Best page size 2se\sqrt{2 s e}. Shared pages: same frame in several tables.

Traps and GATE patterns

Connections

Paging replaces the single base register of process address space by a table, and fixes the external fragmentation of contiguous allocation. A page table of 2202^{20} entries is too large to store flat, which leads to multilevel, hashed and inverted tables; the cost of two accesses per reference is attacked by the TLB. Allowing the valid bit to mean "on disk" gives virtual memory and demand paging.

PYQs

10 previous year questions from your book. I re-solved each one myself (7 by running or simulating, 3 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · NATQ2 · page 340 of the book
A system has a Translation Lookaside Buffer
(TLB) that has a reach of 1 MB. TLB reach is
defined as the total amount of physical memory that
can be accessed through the TLB entries.

The paging system uses pages of size 4 KB. The
virtual address space is 64 GB and physical address
space is 1 GB.

If each TLB entry stores:
●
a 4-bit process id
●
page number
●
frame number
●
a 2-bit control field

then the size of the TLB (in bytes) is ______.

Note:
1K = 210, 1M = 220, 1G = 230.
GATE 2025 · NATQ4 · page 340 of the book
A computer system supports a logical address space
of 232bytes. It uses two-level hierarchical paging
with a page size of 4096 bytes. A logical address is
divided into a b-bit index to the outer page table, an
offset within the page of the inner page table, and an
offset within the desired page. Each entry of the inner
page table uses eight bytes. All the pages in the
system have the same size.The value of b is ______.
GATE 2025 · MCQQ6 · page 341 of the book
Consider a demand paging memory management
system with 32-bit logical address, 20-bit physical
address, and page size of 2048 bytes. Assuming that
the memory is byte addressable, what is the
maximum number of entries in the page table?
  • (a)221
  • (b)220
  • (c)222
  • (d)224
GATE 2025 · MCQQ7 · page 341 of the book
A disk of size 512M bytes is divided into blocks of
64K bytes. A file is stored in the disk using linked
allocation. In linked allocation, each data block
reserves 4 bytes to store the pointer to the next data
block. The link part of the last data block contains
a NULL pointer (also of 4 bytes). Suppose a file of
1M bytes needs to be stored in the disk. Assume,
1K = 210 and 1M = 220. The amount of space in
bytes that will be wasted due to internal
fragmentation is ______. (Answer in integer)
GATE 2024 · MSQQ10 · page 341 of the book
Which
of
the
following
tasks
is/are
the
responsibility/responsibilities
of
the
memory
management unit (MMU) in a system with paging-
based memory management?
  • (a)Allocate a new page table for a newly created process
  • (b)Translate a virtual address to a physical address using the page table
  • (c)Raise a trap when a virtual address is not found in the page table
  • (d)Raise a trap when a process tries to write to a page marked with read-only permission in the page table
GATE 2024 · NATQ11 · page 341 of the book
Consider a memory management system that uses a
page size of 2 KB. Assume that both the physical and
virtual addresses start from 0. Assume that the pages
0, 1, 2, and 3 are stored in the page frames 1, 3, 2, and
0, respectively. The physical address (in decimal
format) corresponding to the virtual address 2500 (in
decimal format) is _________

OPERATING SYSTEMS
GATE 2021 · MSQ · 1 markQ16 · page 342 of the book
In the context of the operating system, which of the
following statements is/are correct with respect to
paging?
  • (a)Paging helps solve the issue of external fragmentation.
  • (b)Page size has no impact on internal fragmentation.
  • (c)Multi-level paging is necessary to support pages of different sizes.
  • (d)Paging incurs memory overheads.
GATE 2015 · NATQ29 · page 344 of the book
Consider a system with byte-addressable memory, 32
bit logical addresses, 4 kilobyte page size and page
table entries of 4 bytes each. The size of the page table
in the system in megabytes is ___________
GATE 2015 · NATQ30 · page 344 of the book
A computer system implements 8 kilobyte pages and
a 32-bit physical address space. Each page table entry
contains a valid bit, a dirty bit, three permission bits,
and the translation. If the maximum size of the page
table of a process is 24 megabytes, the length of the
virtual address supported by the system is ________
bits.
GATE 2009 · MCQ · 1 markQ41 · page 346 of the book
The essential content(s) in each entry of a page
table is/are
  • (a)virtual page number
  • (b)page frame number
  • (c)both virtual page number and page frame number
  • (d)access right information

Self-check

Question 1

Logical addresses of 24 bits, pages of 2 KB. How many bits for the page number, the offset, and how many page-table entries?

Show answer

Offset: log⁡22048=11\log_2 2048 = 11 bits. Page number: 24−11=1324 - 11 = 13 bits. Entries: 213=81922^{13} = 8192.

Question 2

Page size 512 bytes; page 3 is in frame 7. What physical address does logical address 3×512+100=16363 \times 512 + 100 = 1636 give?

Show answer

7×512+100=36847 \times 512 + 100 = 3684.

Question 3

A process of 70000 bytes, page size 8 KB (8192). How many pages, and how much internal fragmentation?

Show answer

⌈70000/8192⌉=9\lceil 70000/8192 \rceil = 9 pages. The first 8 hold 65536 bytes; the last holds 44644464; waste 8192−4464=37288192 - 4464 = 3728 bytes.

Question 4

Physical addresses are 30 bits and pages 4 KB. How many bits does a frame number need?

Show answer

30−12=1830 - 12 = 18 bits.

Revision card

One-screen revision

  • Logical address = page number ∥\| offset; P=f⋅2n+dP = f \cdot 2^n + d.
  • Pages 2m−n2^{m-n}, frames 2k−n2^{k-n}, table size = entries ×\times entry size.
  • No external fragmentation; internal ≈\approx half a page per process.
  • 2 memory accesses per reference without a TLB. Best page size 2se\sqrt{2 s e}.

References

  • ·Silberschatz, Galvin, Gagne, Operating System Concepts, Chapter 9 Main Memory (paging: basic method, hardware support, protection, shared pages)
  • ·Tanenbaum and Bos, Modern Operating Systems, Chapter 3 Memory Management (paging, page tables, design issues: page size)
  • ·Stallings, Operating Systems: Internals and Design Principles, Chapter 7 Memory Management (paging)

Verification

The translations in the animation are computed by a script (scripts/gen-os-mem-traces.py) and the same five translations are performed by the C program on this page (npm run verify:c). scripts/enum-checks/os_mem.py translates EVERY logical address of 400 random small address spaces with three equivalent methods (bit slicing, division and remainder, and a byte-by-byte memory model that writes through the translation and reads back) and checks that distinct logical addresses give distinct physical addresses; it also checks the internal-fragmentation figures, that the average waste over all residues is (page - 1) / 2, and that the page size minimising overhead for a 1 MB process with 8-byte entries is 4096 = sqrt(2 s e) by a search over page sizes. tests/os-mem-traces.test.ts re-derives every translation. The model: byte-addressable memory, page and frame sizes equal and a power of two, one page-table entry per page. Entry formats of real processors have more bits (accessed, dirty, caching) and are not modelled. Textbook chapter titles are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand. No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never