Sub topic 4
Paging and Address Translation
This chapter assumes you can work with powers of two and hexadecimal, and that you know the earlier chapters on address spaces, contiguous allocation and fragmentation. It also assumes you can read short C or Python code. Paging is the most heavily tested topic in this part of GATE. Most numerical questions reduce to splitting an address into bits and multiplying by an entry size.
Why paging exists
Contiguous allocation needs each process in one unbroken block. That leads to external fragmentation: free memory exists but is scattered in holes too small to use. Paging removes the requirement that a process be contiguous.
- Physical memory is divided into fixed-size blocks called frames.
- Logical memory is divided into blocks of the same size called pages.
- A page table, kept per process, records which frame holds each page.
Any free frame can hold any page, so no hole is ever too small. The price is a translation step on every memory reference, plus the table itself.
Splitting an address
The page size is a power of two, 2^d bytes. That makes the split a bit-slicing operation. The low d bits are the offset, which says where the byte sits inside the page. The remaining high bits are the page number, which indexes the page table.
For a logical address space of 2^m bytes with pages of 2^d bytes:
- Offset bits: d
- Page number bits: m − d
- Number of pages: 2^(m−d)
The C program below splits a 32-bit address with 4 KB pages (d = 12).
#include <stdint.h>
#include <stdio.h>
int main(void) {
uint32_t va = 0x12345678; /* 32-bit virtual address */
uint32_t page = va >> 12; /* 4 KB pages: 12 offset bits */
uint32_t offset = va & 0xFFF; /* low 12 bits */
printf("page=0x%x offset=0x%x\n", (unsigned)page, (unsigned)offset);
return 0;
}It prints page=0x12345 offset=0x678. The shift and the mask are free in hardware, because the bits are just wires split into two groups. This is why page sizes are powers of two.
The translation procedure
The MMU (memory management unit) performs these steps on every reference:
- Split the logical address into page number p and offset d.
- Use p as an index into the page table, found through the page table base register (PTBR).
- Read the page table entry (PTE) and take the frame number f from it.
- Form the physical address by concatenating f and the offset, which is f × page size + offset.
The offset passes through unchanged. Only the page number is translated. A common mistake is to think the offset is also remapped.
A worked translation
Take a 16-bit logical address space with 1 KB pages. Then d = 10 and the page number has 6 bits, so there are 64 pages. Suppose the first four entries of the page table are: page 0 → frame 5, page 1 → frame 2, page 2 → frame 7, page 3 → frame 1.
Translate logical address 2500:
- Page number = 2500 div 1024 = 2.
- Offset = 2500 mod 1024 = 452.
- Page 2 maps to frame 7.
- Physical address = 7 × 1024 + 452 = 7620.
The same calculation in Python:
PAGE_SIZE = 1024
page_table = {0: 5, 1: 2, 2: 7, 3: 1} # page -> frame
def translate(va):
page, offset = divmod(va, PAGE_SIZE)
if page not in page_table:
raise ValueError(f"invalid page {page}")
return page_table[page] * PAGE_SIZE + offset
print(translate(2500)) # 7620Division and modulo give the same result as the shift and mask, because the page size is a power of two.
Page table entries
A PTE holds more than a frame number. A typical entry contains:
| Field | Purpose |
|---|---|
| Frame number | Where the page lives in physical memory |
| Valid/invalid bit | Whether the page is mapped to a frame |
| Protection bits | Read, write and execute permissions |
| Dirty (modified) bit | Set when the page has been written |
| Reference bit | Set when the page has been accessed |
The valid bit, dirty bit and reference bit matter again in the demand paging and page replacement chapters. For now, remember that the PTE is wider than the frame number alone.
Sizing calculations
GATE questions mix the three sizes below. Keep them separate.
- Logical address bits give the number of pages (with the page size).
- Physical address bits give the number of frames (with the page size), and so the minimum frame-number bits in a PTE.
- PTE size is a given number of bytes, usually rounded up to a power of two in the real system.
The formulas are:
- Number of pages = logical address space ÷ page size
- Number of frames = physical memory ÷ page size
- Page table size = number of pages × PTE size
Example: a 32-bit logical address, 4 KB pages, 1 GB of physical memory and 4-byte PTEs.
- Pages = 2^32 ÷ 2^12 = 2^20.
- Frames = 2^30 ÷ 2^12 = 2^18, so a frame number needs 18 bits.
- Page table size = 2^20 × 4 bytes = 4 MB per process.
The 4 MB figure is why a single flat table is wasteful. A process that uses only a few pages still pays for 2^20 entries. The multilevel page tables chapter addresses this.
The next table shows how the page size changes the result for the same 32-bit logical space and 4-byte PTEs.
| Page size | Offset bits | Pages | Page table size |
|---|---|---|---|
| 1 KB | 10 | 2^22 | 16 MB |
| 4 KB | 12 | 2^20 | 4 MB |
| 8 KB | 13 | 2^19 | 2 MB |
| 64 KB | 16 | 2^16 | 256 KB |
Larger pages shrink the table, but they cost something elsewhere, as the next section shows.
Internal fragmentation and the page size trade-off
Paging has no external fragmentation, but it has internal fragmentation. A process rarely fills its last page exactly, so the unused part of that page is wasted. On average the waste is half a page per process, which is p/2 for page size p.
Small pages waste less per process but need bigger tables. Let s be the average process size and e the PTE size. The total overhead per process is:
overhead = (s ÷ p) × e + p ÷ 2
The first term is the page table and the second is the internal fragmentation. Setting the derivative with respect to p to zero gives the optimal page size:
p = √(2 × s × e)
For s = 1 MB (2^20 bytes) and e = 8 bytes, p = √(2 × 2^20 × 2^3) = √(2^24) = 2^12 = 4096 bytes. This matches the 4 KB page size common in practice. Larger pages also reduce the number of disk transfers for page-ins, since disk I/O has a high fixed cost per request.
Hardware support and cost
The page table for a process sits in main memory. Two registers describe it:
- PTBR (page table base register) holds the address of the table.
- PTLR (page table length register) holds the table's size. An address whose page number is at or above the PTLR is out of range, and the hardware traps to the OS.
Without any cache, every data reference costs two memory accesses. The first reads the PTE and the second reads the data. A small hardware cache of recent translations, the TLB, removes most of the first access. The next chapter covers it and the effective access time calculation.
On a context switch the OS loads the new process's PTBR and PTLR. Each process has its own table, so two processes can use the same logical address and still reach different frames. This is the basis of memory isolation.
Common errors
- Using physical memory to count pages. The number of page table entries depends on the logical address space, not on physical memory. Physical memory sets the number of frames and the width of the frame number.
- Forgetting that memory is byte-addressable. If a question says the memory is word-addressable, convert the word size before you divide by the page size.
- Mixing up bits and bytes. A PTE of 32 bits is 4 bytes. Check the unit before you multiply.
- Translating the offset. The physical offset equals the logical offset.
- Ignoring the valid bit. A reference to a page with the valid bit clear raises a trap. It does not return a frame.
- Assuming 4 KB pages. Read the page size from the question. Powers of two such as 2 KB, 8 KB and 16 KB appear often.
Exercise
A system has a 32-bit logical address, 8 KB pages and 4-byte page table entries. How large is the page table of one process, and how many bits of a logical address form the page number?
Show answer
An 8 KB page is 2^13 bytes, so the offset is 13 bits and the page number is 32 − 13 = 19 bits. There are 2^19 pages, and the page table is 2^19 × 4 bytes = 2^21 bytes = 2 MB.
Exercise
The system has 256 MB of physical memory and 4 KB pages. A process issues logical address 0x2ABC, and its page 2 is mapped to frame 0x0F3. What is the physical address, and what is the minimum number of bits needed for a frame number?
Show answer
The offset is the low 12 bits. Address 0x2ABC splits into page 0x2 and offset 0xABC. Frame 0x0F3 joined with offset 0xABC gives the physical address 0x0F3ABC. Physical memory is 2^28 bytes, so there are 2^28 ÷ 2^12 = 2^16 frames, and a frame number needs at least 16 bits.
Exercise
The average process size is 2 MB and each page table entry is 4 bytes. Using overhead = s·e/p + p/2, what page size minimizes the overhead?
Show answer
p = √(2 × s × e) = √(2 × 2^21 × 2^2) = √(2^24) = 2^12 = 4096 bytes, which is 4 KB.