Paging concept and page tables in Operating Systems - Time & Space Complexity
When using paging in operating systems, we want to know how the time to find data grows as the memory size increases.
We ask: How does looking up a page in the page table change when the number of pages grows?
Analyze the time complexity of the following page table lookup process.
function getPhysicalAddress(virtualAddress) {
pageNumber = Math.floor(virtualAddress / pageSize);
offset = virtualAddress % pageSize;
frameNumber = pageTable[pageNumber];
physicalAddress = frameNumber * pageSize + offset;
return physicalAddress;
}
This code finds the physical memory address by looking up the page number in the page table and adding the offset.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the page table array at the index of the page number.
- How many times: Exactly once per address translation.
Looking up a page number in the page table takes the same steps no matter how many pages there are.
| Input Size (number of pages) | Approx. Operations |
|---|---|
| 10 | 1 lookup |
| 100 | 1 lookup |
| 1000 | 1 lookup |
Pattern observation: The time to find a page does not increase as the number of pages grows.
Time Complexity: O(1)
This means the time to translate a virtual address to a physical address stays constant, no matter how big the memory is.
[X] Wrong: "Looking up a page takes longer as the number of pages grows because the page table gets bigger."
[OK] Correct: The page table is an array, so accessing any page number is done directly by index, which takes the same time regardless of size.
Understanding that page table lookups are constant time helps you explain how operating systems manage memory efficiently, a key skill in system design discussions.
"What if the page table was implemented as a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the concept of paging
Paging divides the computer's memory into fixed-size blocks called pages to simplify memory management.Step 2: Identify the main purpose
The main goal is to break memory into pages, not to affect CPU speed, encryption, or device management.Final Answer:
To divide memory into fixed-size blocks called pages -> Option DQuick Check:
Paging = dividing memory into pages [OK]
- Confusing paging with CPU speed improvement
- Thinking paging encrypts memory data
- Mixing paging with device management
Solution
Step 1: Define the role of a page table
A page table maps virtual memory pages to physical memory frames, helping the OS locate data.Step 2: Eliminate incorrect options
Page tables do not list processes, CPU instructions, or devices.Final Answer:
A table that stores the mapping from virtual pages to physical frames -> Option AQuick Check:
Page table = virtual to physical mapping [OK]
- Confusing page tables with process lists
- Thinking page tables store CPU instructions
- Mixing page tables with device lists
Solution
Step 1: Calculate bits for page offset
Page size is 4 KB = 2^12 bytes, so lower 12 bits represent the offset within a page.Step 2: Identify page number bits
The page number is given by the higher bits of the address excluding the lower 12 bits (offset).Final Answer:
The higher bits except the lower 12 bits -> Option CQuick Check:
Page number = address bits above offset bits [OK]
- Confusing offset bits with page number bits
- Using entire address as page number
- Selecting wrong bit segments
Solution
Step 1: Understand what causes a page fault
A page fault occurs when the page table does not have an entry for the requested virtual page, meaning it is not in physical memory.Step 2: Eliminate unrelated causes
CPU speed, invalid address, or page size do not directly cause missing page table entries.Final Answer:
The page is not currently loaded in physical memory -> Option BQuick Check:
Page fault = missing page in physical memory [OK]
- Blaming CPU speed for page faults
- Assuming invalid address always causes page fault
- Confusing page size with page fault cause
Solution
Step 1: Calculate number of pages
Page size = 4 KB = 2^12 bytes, virtual address size = 32 bits.Step 2: Calculate entries in page table
Number of pages = 2^(32 - 12) = 2^20 = 1,048,576 entries.Final Answer:
1,048,576 entries -> Option AQuick Check:
Entries = 2^(address bits - offset bits) [OK]
- Using page size directly as number of entries
- Confusing total address bits with offset bits
- Calculating entries incorrectly as 2^16 or 2^14
Ready to prove it?
Pass the test and this pattern moves unknown
