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 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 -bit logical addresses and a page size of bytes:
The page number is the high-order bits; the offset is the low-order bits. Equivalently and .
Page table. An array, one entry per page, indexed by the page number. Entry holds the frame number in which page 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 and offset : look up (if the valid bit is 0, a page fault), and form the physical address
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).
| Quantity | Formula |
|---|---|
| logical address space | bytes |
| page size = frame size | bytes |
| number of pages (page table entries) | |
| physical memory with -bit physical addresses | bytes |
| number of frames | |
| bits of a frame number | |
| page table size | number of entries entry size |
| minimum entry size | the 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 bytes with pages of bytes, it takes pages and wastes bytes. Averaged over random sizes the waste is about per process (exactly 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 and page-table entry size , the overhead per process is , minimized at
For MB and 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 | frame | valid |
|---|---|---|
| 0 | 5 | 1 |
| 1 | 9 | 1 |
| 2 | 2 | 1 |
| 3 | - | 0 |
| 4 | 17 | 1 |
| 5 | 3 | 1 |
| 6 | - | 0 |
| 7 | 20 | 1 |
| 8 | 1 | 1 |
| 9 | 30 | 1 |
| 10 | - | 0 |
| 11 | 12 | 1 |
| 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.
Worked examples
EX 1Translating by program
EasyThe 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: , so page 5, offset 3; page 5 is in frame 3, so the physical address is . Page 3 and page 15 are not in memory: page faults. A 32-bit address with 4 KB pages has 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
EasyLogical addresses are 32 bits, physical addresses 36 bits, pages 4 KB (). Offset: 12 bits. Page number: bits, so pages and a page table with entries. Frames: , 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 MB. Physical memory is 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?
MediumA process of 100000 bytes with pages of 4096 bytes needs pages; the first 24 are full ( bytes) and the last holds bytes, so 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 per process: about half a page, which gives the rule of thumb "internal fragmentation page size / 2 per process".
EX 4What page size is best?
MediumEach process has an average size MB and each page-table entry takes bytes. A process of size with page size has entries, costing bytes of page table, and wastes about bytes in the last page. Total overhead . At : bytes. At it is ; at : . So 4096 is best, and confirms it (a search over page sizes agrees). The overhead is smallest where the two terms are equal.
EX 5Two accesses per reference
GATE levelA 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: 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 . Logical address page number ( bits) offset ( bits). , .
- Physical address (frame number same offset).
- Number of pages page table entries. Number of frames . Frame number has bits.
- Page table size entries entry size. Every process has its own table; PTBR points to it.
- No external fragmentation; internal fragmentation 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 . Shared pages: same frame in several tables.
Traps and GATE patterns
Connections
Unlocks
Across subjects
No cross-subject links yet.
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 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
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.
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 ______.
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
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)
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
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
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.
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 ___________
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.
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 answerHide
Offset: bits. Page number: bits. Entries: .
Question 2
Page size 512 bytes; page 3 is in frame 7. What physical address does logical address give?
Show answerHide
.
Question 3
A process of 70000 bytes, page size 8 KB (8192). How many pages, and how much internal fragmentation?
Show answerHide
pages. The first 8 hold 65536 bytes; the last holds ; waste bytes.
Question 4
Physical addresses are 30 bits and pages 4 KB. How many bits does a frame number need?
Show answerHide
bits.
Revision card
One-screen revision
- Logical address = page number offset; .
- Pages , frames , table size = entries entry size.
- No external fragmentation; internal half a page per process.
- 2 memory accesses per reference without a TLB. Best page size .
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
My notes (saved in this browser)