1. Introduction

In computing, memory management plays a distinct yet important role in resource utilization. Within the fundamental concepts of memory management are virtual pages and physical frames. These concepts form the foundational blocks of a virtual memory system, allowing modern operating systems to manage memory resources efficiently.

In this tutorial, we’ll examine virtual pages and physical frames: what they are, how the operating system (OS) utilizes them, and their key differences.

2. Virtual Memory

In computer systems, we’ve got the main memory, or Random Access Memory (RAM), which serves as the primary storage medium for executing programs and storing data.

The OS divides this physical memory space into storage cells and represents these cells with physical addresses. However, there might be instances where the capacity of this physical memory is limited. As a result, our systems couldn’t carry out more intensive workloads.

Therefore, most modern systems resolve this limitation through the use of virtual memory. A virtual memory technique allows programs to use more than is physically available by temporarily transferring data from a disk drive to the RAM.

The virtual memory uses two key components: the virtual page and page frames.

3. Virtual Pages and Page Frames

A page frame, otherwise called a frame or physical page, refers to a contiguous block of memory that corresponds to actual storage locations in the RAM. To aid comprehension, we’d refer to page frames as frames.

The hardware manages frames and uses them to store data and instructions for running processes. The OS allocates and deallocates page frames according to the memory requirements.

Conversely, a virtual page, or a logical page, is a fixed-length block of virtual memory that is mapped to physical addresses. Similarly, we’ll stick to the convention pages to depict virtual pages.

Virtual memory maps these virtual pages to physical addresses using virtual addresses. For example, we can have two processes with IDs 101 and 202, respectively, with the same virtual address but mapped to different locations on the physical memory.

Generally, we refer to this mapping process as paging.

4. Paging

Paging is a process that allows the OS to store and retrieve data from secondary storage devices for use in the main memory.

Typically, it retrieves this data on fixed-size pages of 4K or 4096 bytes to facilitate mapping:

Virtual address mapping to physical memory

For example, we have a 32K physical unit with a virtual memory capacity of 64K.

Let’s assume that the OS has temporarily retrieved data from the secondary storage unit in 4K blocks, meaning that the first page houses 0-4095 bytes, the second 4096-8191 bytes, etc.

When a program tries to access address 3101, the OS determines which page this resides. Here, it’s on page 0 of the virtual address space. Then, the OS translates that virtual address to its corresponding address in the physical memory that it has mapped to frame 2 (8 – 12K).

This mapping and translation of virtual and physical addresses is done with the use of a page table.

4.1. Page Tables

A page table is a data structure that virtual memory systems use to store mappings between virtual and physical addresses. The OS sets this table, and the Memory Management Unit (MMU) reads and writes to it.

To aid comprehension. let’s think of an analogy. Suppose we’ve got a book, and we’re looking for the 700th word in it. We could always start counting from the very first word on the first page until we reach our target, but that would be cumbersome. But imagine if we had an indexed list of how many words each page holds.

Consequently, we could be able to identify which page our target word resides on and easily move to that page to retrieve our word. We can liken page tables in memory systems to this index listing.

Each mapping of a virtual address to its corresponding physical address on the page table is known as a page table entry (PTE).

4.2. The Translation Process

MMU Translation Flowchart

When the MMU needs to translate a virtual address into a physical address, it first searches the TLB. The Translation Lookaside Buffer (TLB) is a cache of recently used mappings from the operating system’s page table. This MMU populates this cache.

If it finds a mapping for that virtual address in the TLB, the MMU returns the physical address, and memory access can continue. This is what we know as a TLB hit.

However, if we encounter a TLB miss, i.e. if we don’t find a mapping, it will typically check the address mapping in the page table. This search is what we call a page walk. If it finds one in the page table, it writes this mapping to the TLB and the entire process restarts.

Subsequently, the new translation process resolves in a TLB hit.

4.3. A Practical Example

Let’s look at an example of this translation using the previous process with virtual address 3101. We seek to translate this virtual address using a page table to get its corresponding physical address in the frame:

Page Table Example

Here, we already have a page table with mappings of virtual address blocks to physical addresses.

First, the MMU takes the first four bits of our virtual address, 0000, and looks up the page table to find its block. Next, it identifies the corresponding frame in the page table, which in this case is 0010, and this forms the first four bits of the physical address. Lastly, it copies the rest of the bits from the virtual address to the physical address.

The first four bits represent the page, while the copied bits represent the offset, which we depict by dashed lines in the diagram. The page table also has a column with flags that show if a page has a corresponding frame. This is the last column in our diagram.

If the MMU attempts to access an unmapped page, it throws a page fault. At the end of the translation, the MMU resolves our virtual address to the physical address of 11293.

5. Key Differences Between Pages and Frames

Let’s look at the key differences between pages and frames:

Category Pages Frames
Management mechanism Logical constructs managed by the OS that allow the temporary use of disk storage as main memory Physical entities managed by the hardware that stores the instructions of running processes
Abstraction Operates at a higher level of abstraction than frames, as they provide a logical view of the address space of a process Operates at a lower level of abstraction as it represents physical memory locations on the RAM
Page Tables It uses page tables that map page addresses to frames The MMU translates virtual addresses to physical addresses based on the mappings stored in the page table

6. Conclusion

In this article, we discussed virtual pages and page frames, learned what they are, how they are used in virtual memory systems, and identified their key differences.

We learned that virtual pages are logical constructs mapped to frames in virtual memory, enabling the OS to utilize more memory than is physically available. We also discussed how this mapping is done through a page table, exploring the translation process with an example.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments