Hashed Page Table in OS
Hash the virtual page number to a bucket and search that bucket's chain. The table holds only pages that are mapped, so its size does not depend on how big the address space is.
Looking Up a Page by Hashing
- Hash the virtual page number to get a bucket index.
- Compare the page number with the first entry in that bucket's chain.
- On a match, use that entry's frame number.
- Otherwise follow the next pointer and compare again.
- If the chain ends with no match, raise a page fault.
A hashed page table stores page-table entries in a hash table keyed by the virtual page number. A hash function turns the page number into a bucket index, and each bucket points to a linked list (a chain) of the entries whose page numbers hashed there.
Each entry has three fields: the virtual page number, the frame it maps to, and a pointer to the next entry in the chain. The page number has to be stored because many page numbers share a bucket; the bucket only says where to start looking.
The point is size. A 64-bit address space with 4 KB pages has 2⁵² possible pages. A flat table cannot hold that, and a multilevel table handles it only by adding levels. A hashed table holds one entry per page that is actually mapped, whatever the width of the address.
- Entry = (virtual page, frame, next)
- Average lookup: one hash plus one or two comparisons
- Worst case: every page in one bucket, so a scan of the whole table
A Worked Lookup
Take 4 buckets and the hash h(p) = p mod 4. The process has four pages mapped: 4 → f2, 17 → f9, 53 → f4 and 7 → f1. Pages 17 and 53 both leave remainder 1, so bucket 1 holds the chain 17 → 53. Bucket 0 holds 4, bucket 3 holds 7, and bucket 2 is empty.
- Buckets: 4, h(p) = p mod 4
- Mapped: 4 → f2, 17 → f9, 53 → f4, 7 → f1
To translate page 53: 53 mod 4 = 1, so start at bucket 1. The first entry stores 17, not 53, so follow the next pointer. The second entry stores 53, a match, so the frame is f4. That took 2 comparisons.
Page 99 shows the failure path. 99 mod 4 = 3, bucket 3 holds only page 7, and the chain ends without a match. Page 99 is not mapped, so the hardware raises a page fault.
- Page 53: bucket 1, 2 comparisons, frame f4
- Page 17: bucket 1, 1 comparison, frame f9
- Page 99: bucket 3, 1 comparison, page fault
Where It Is Used
Hashing suits address spaces that are huge and sparse. A 64-bit process may map a few thousand pages scattered across billions of possible ones; the hashed table costs memory only for those few thousand, and a lookup costs one hash and a short chain walk no matter how wide the address is.
Real designs include the PowerPC hashed page table and the IA-64 virtual hash page table (VHPT); UltraSPARC keeps a hashed table of recent translations in software. x86-64 and ARM chose multilevel tables instead, because a hardware walker can follow fixed-size tables without dealing with chains.
The design depends on the hash. A good hash with about one entry per bucket keeps chains at one or two entries. A poor one piles pages into a few buckets, and the lookup degrades toward a linear search.
- Memory: one entry per mapped page, plus the bucket array
- Speed: depends on chain length, so on the hash and the load factor
- Weak point: walking a linked chain is hard to do in hardware
Terms, operations, and practical uses
Core vocabulary
- BucketOne slot of the hash array; it points to the start of a chain.
- ChainThe linked list of entries whose page numbers hashed to the same bucket.
- CollisionTwo page numbers with the same hash, such as 17 and 53 under mod 4.
- Clustered page tableA hashed table whose entries each map a block of consecutive pages.
Formulas and rules
- Bucketh(virtual page), for example page mod number of buckets
- Load factormapped pages ÷ buckets; keep it near 1 for short chains
- Lookup cost1 hash + chain length comparisons in the worst case
- Clustered slotblock = page ÷ 16, slot = page mod 16
Related lessons
- Hash TablesChaining and load factor in general.
- Inverted Page TableOne row per frame, found through a hash.
- Multilevel PagingThe tree of tables used by x86-64 and ARM.
- Page Table StructuresEvery layout side by side.
Insert and look up in a hashed page table
BUCKETS = 4
class Node:
"""One entry: the page it maps, the frame, and the next entry in the chain."""
def __init__(self, page, frame):
self.page = page
self.frame = frame
self.next = None
table = [None] * BUCKETS
def insert(page, frame):
"""Append to the end of the bucket's chain, preserving insert order."""
node = Node(page, frame)
index = page % BUCKETS
if table[index] is None:
table[index] = node
return
tail = table[index]
while tail.next is not None:
tail = tail.next
tail.next = node
def lookup(page):
"""Hash to a bucket, then walk the chain. Returns (frame, comparisons)."""
index = page % BUCKETS
comparisons = 0
node = table[index]
while node is not None:
comparisons += 1
# The page number is stored because hashing is lossy.
if node.page == page:
return node.frame, comparisons
node = node.next
return None, comparisons
def main():
for page, frame in ((4, 0x2), (17, 0x9), (53, 0x4), (7, 0x1)):
insert(page, frame)
for page in (53, 17, 99):
frame, comparisons = lookup(page)
where = "not mapped" if frame is None else f"frame f{frame:x}"
print(f"page {page:<4} h={page % BUCKETS} -> {where:<12} "
f"({comparisons} comparisons)")
if __name__ == "__main__":
main()#include <cstdio>
#include <iostream>
#include <vector>
constexpr int BUCKETS = 4;
// One entry: the page it maps, the frame, and the next entry in the chain.
struct Node {
int page;
int frame;
Node* next;
};
std::vector<Node*> table(BUCKETS, nullptr);
// Append to the end of the bucket's chain, preserving insert order.
void insert(int page, int frame) {
int index = page % BUCKETS;
Node* node = new Node{page, frame, nullptr};
if (table[index] == nullptr) {
table[index] = node;
return;
}
Node* tail = table[index];
while (tail->next != nullptr) tail = tail->next;
tail->next = node;
}
// Hash to a bucket, then walk the chain. Returns -1 if not mapped.
int lookup(int page, int& comparisons) {
int index = page % BUCKETS;
comparisons = 0;
for (Node* node = table[index]; node != nullptr; node = node->next) {
comparisons++;
// The page number is stored because hashing is lossy.
if (node->page == page) return node->frame;
}
return -1;
}
int main() {
insert(4, 0x2);
insert(17, 0x9);
insert(53, 0x4);
insert(7, 0x1);
for (int page : {53, 17, 99}) {
int comparisons = 0;
int frame = lookup(page, comparisons);
char where[16];
if (frame < 0) std::snprintf(where, sizeof where, "not mapped");
else std::snprintf(where, sizeof where, "frame f%x", frame);
std::printf("page %-4d h=%d -> %-12s (%d comparisons)\n", page, page % BUCKETS, where, comparisons);
}
}class Main {
static final int BUCKETS = 4;
// One entry: the page it maps, the frame, and the next in the chain.
static class Node {
int page;
int frame;
Node next;
Node(int page, int frame, Node next) {
this.page = page;
this.frame = frame;
this.next = next;
}
}
static final Node[] table = new Node[BUCKETS];
static int comparisons = 0;
// Append to the end of the bucket's chain, preserving insert order.
static void insert(int page, int frame) {
int index = page % BUCKETS;
Node node = new Node(page, frame, null);
if (table[index] == null) {
table[index] = node;
return;
}
Node tail = table[index];
while (tail.next != null) tail = tail.next;
tail.next = node;
}
// Hash to a bucket, then walk the chain. Returns -1 if not mapped.
static int lookup(int page) {
int index = page % BUCKETS;
comparisons = 0;
for (Node node = table[index]; node != null; node = node.next) {
comparisons++;
// The page number is stored because hashing is lossy.
if (node.page == page) return node.frame;
}
return -1;
}
public static void main(String[] args) {
insert(4, 0x2);
insert(17, 0x9);
insert(53, 0x4);
insert(7, 0x1);
for (int page : new int[] {53, 17, 99}) {
int frame = lookup(page);
String where = frame < 0
? "not mapped"
: String.format("frame f%x", frame);
System.out.printf("page %-4d h=%d -> %-12s (%d comparisons)%n",
page, page % BUCKETS, where, comparisons);
}
}
}Step through it
Running on 4 buckets, h(p) = p mod 4; map 4 → f2, 17 → f9, 53 → f4, 7 → f1; look up 53 and 99
Clustered Page Tables
A clustered page table is a hashed table where each entry maps a block of consecutive pages, for example 16, instead of a single page. The hash uses the block number (page ÷ 16), and the entry holds 16 frame numbers.
Programs touch memory in runs of neighbouring pages, so one entry usually serves many lookups. The table has fewer entries, chains are shorter, and the page number and next pointer are stored once per 16 mappings instead of once per page.
The cost appears when access really is scattered. A single page used in a block still needs the whole 16-frame entry, the same waste as internal fragmentation anywhere memory is handed out in fixed blocks.
- Block number = page ÷ 16; slot in the entry = page mod 16
- Page 53 is block 3, slot 5
- Good for dense regions, wasteful for isolated pages
Hashed vs Inverted
The comparison with an inverted page table comes up because both search instead of indexing. The difference is what the table is built around: a hashed table has an entry per mapped virtual page, while an inverted page table has an entry per physical frame and uses a hash only to find the right row.
| Hashed page table | Inverted page table | |
|---|---|---|
| One entry per | Mapped virtual page | Physical frame |
| How many tables | Per process, or global with a process id in the key | One for the whole machine |
| Size grows with | Pages mapped | Amount of RAM |
| Key stored in entry | Virtual page number | Process id and virtual page number |
| Frame number | Stored in the entry | The row's position |
| Shared pages | One entry in each sharer's table | Awkward: one row per frame |
- Both need a hash to avoid a linear search
- Hashed tables keep sharing simple; inverted tables keep total size fixed
- Page table structures compares these with flat and multilevel tables