Experiments and implementations · Intermediate

Calculating address translation for different page sizes

Decompose addresses under different page sizes and verify the distinction between virtual page numbers, offsets and physical frames with a small arithmetic model.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Define the model before running it
  2. Connect quotient and remainder to the program
  3. Separate page decomposition from the frame mapping
  4. Investigate boundaries and covered pages
  5. Keep arithmetic separate from performance
  6. Ask Logeia about this articleCheck your understanding

Define the model before running it

This experiment is an arithmetic model of address decomposition. It does not read the running process page table, discover physical addresses, or alter an operating-system setting. Represent an address as a small nonnegative integer, then compare page sizes of 4096 and 16384 bytes. The same address belongs to a different numbered page when the unit changes. Administrator privileges are unnecessary because all mappings are ordinary Python data. The purpose is to understand which part of translation is division and which part requires a separate mapping. Changing the page-size variable in this program is not evidence that the host uses that page size or that its hardware translation structures have changed.

Connect quotient and remainder to the program

For address a and page size P, the virtual page number is the integer quotient and the offset is the remainder. Python divmod returns both. With a=12345 and P=4096, the result is page 3 and offset 57. The code checks reconstruction at boundary values as well as ordinary addresses. A power-of-two page size also admits a bit-splitting explanation, but quotient and remainder establish the relationship without introducing bit masks first. Keep the nonnegative-input assumption explicit. Python defines division for negative integers too, but those rules should not be silently substituted for the unsigned address interpretation used in this model.

python
for size in (4096, 16384):
    for address in (0, 4095, 4096, 12345, 16384):
        page, offset = divmod(address, size)
        assert 0 <= offset < size
        assert page * size + offset == address
        print(size, address, page, offset)
frames = {3: 9}
page, offset = divmod(12345, 4096)
assert frames[page] * 4096 + offset == 36921

Separate page decomposition from the frame mapping

Suppose virtual page 3 maps to physical frame 9. The modeled physical address is 9×4096+57=36921. Another process can map its virtual page 3 to frame 20 without contradiction because its mapping is different. Reusing the virtual page number as the physical frame number would erase the important mapping step. The program supplies one dictionary entry explicitly rather than pretending to discover it from the host. A missing dictionary entry means this model cannot translate that page; do not substitute frame zero as a fallback. A real operating system distinguishes absent mappings, protection failures and other conditions using mechanisms beyond a Python dictionary lookup.

Investigate boundaries and covered pages

Addresses 4095 and 4096 cross a 4 KB page boundary but remain within one 16 KB page. A 4096-byte range also does not always fit in one 4 KB page: starting at address 1 makes its final byte address 4096, which lies in the next page. Count covered pages using both starting position and length. This explains why byte count alone can misrepresent the translation footprint of a data structure. The calculation concerns a contiguous virtual range. Adjacent virtual page numbers do not require adjacent physical frame numbers, so do not turn the arithmetic into a claim about physical contiguity. Compare aligned and unaligned starting addresses while holding the range length fixed.

Keep arithmetic separate from performance

A larger page covers more addresses per translation entry, but this program measures neither TLB hit rates nor page-fault latency. It also omits allocation costs, unused portions of a page and operating-system policy. A decision about huge pages needs workload and host evidence: working-set size, access order, allocation behavior and the page sizes actually used. The base page size varies across systems. Avoid copying 4096 as a universal constant into code that needs the host value; obtain that value through the relevant interface instead. Report the identities demonstrated by arithmetic and list the performance questions that remain unanswered rather than presenting this model as a benchmark.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 16

Understand the systems behind the web · STEP 6

Prepare for AI systems · STEP 6

Choose a learning path