Operating System - Page Table



A page table is a data structure used during paging in virtual memory systems. We have already learned about paging and demand paging in previous sections. Read this chapter to learn the basics of page table and the data it stores. Following topics will be covered in this chapter −

What is Page Table?

A page table is a data structure inside the operating system that maps logical addresses and physical addresses. It stores other information about each page, such as whether it is in memory or on disk, whether it is read-only or read-write, and so on. These information will help the operating system to easily find the location of a page in main memory (RAM) when it is needed.

The following image shows the structure and entries of a simple page table −

Page Table Structure

The main purpose of a page table is to keep track of the mapping between the logical addresses used by a program and the physical addresses in the computer's memory. When a program try to access a memory location in RAM, the CPU first checks the page table to see the stored mapping for the corresponding logical address. If the page is already in memory, the CPU can directly access it.

A page fault will occur if the page is not in memory. In such cases, the operating system need to load the page from disk into memory and then update the page table.

Data Stored in Page Table

The page table stores important information about each page in the virtual memory. This will help the operating system to manage memory efficiently. Following are some of the key data stored in a page table:

1. Frame Number

The frame number is used to identify the physical location of a page in main memory (RAM). For every page in the virtual memory, there is a corresponding frame in the main memory. The page table stores the frame number for each page, which helps the operating system to locate the page in RAM when it is needed.

The number of bits required for each frame number can be calculated using the formula −

$$\mathrm{\text{Number of Frames} = \frac{\text{Physical Memory Size}}{\text{Frame Size}}}$$

$$\mathrm{\text{Number of Bits for Frame Number} = \log_2(\text{Number of Frames})}$$

For example, if the physical memory size is 64 MB and the frame size is 4 KB, then the number of frames will be 16384. Hence, the number of bits required for each frame number will be 14.

2. Valid/Invalid Bit

The valid/invalid bit is used to indicate whether a page is currently loaded in physical memory(RAM) or not. If the bit is set to valid (1), it means that the page is in memory and can be accessed. If the bit is set to invalid (0), it means that the page is not in memory. This bit is used to check whether a page fault will occur or not. Meaning, if a program tries to access a page that is marked as invalid, then the page fault will occur.

3. Protection Bits

The protection bits are used to describe the access permissions for a page. These bits determine whether a page can be read, written, or executed by a program. Common protection bits include −

  • Read (R) − Indicates that the page can be read.
  • Write (W) − Indicates that the page can be written to.
  • Execute (X) − Indicates that the page can be executed as code.

These bits prevents unauthorized access of memory pages by different processes. For example, a page containing code should not be writable, and a page containing data should not be executable.

4. Reference Bit

The reference bit is used by page replacement algorithms to keep track of whether a page has been accessed recently. When a page is accessed, the reference bit is set to 1. This information helps the operating system and page replacement algorithms to make decisions about which pages to replace when a page fault occurs. Pages that have been accessed recently (with reference bit set to 1) are less likely to be replaced compared to pages that have not been accessed recently (with reference bit set to 0).

5. Caching Bit

The caching bit is used to indicate whether a page can be cached in the CPU cache or not.

  • Caching bit is set to 1 → The page can be cached.
  • Caching bit is set to 0 → The page cannot be cached.

This bit is important for performance optimization. Pages that are frequently accessed should be cached to reduce memory access time. However, some pages, such as those used for memory-mapped I/O, should not be cached to ensure data consistency.

6. Dirty Bit

The dirty bit is used to indicate whether a page has been modified since it was loaded into memory.

  • Dirty bit is set to 1 → The page has been modified.
  • Dirty bit is set to 0 → The page has not been modified.

This information is important for page replacement algorithms. If a page with the dirty bit set to 1 is selected for replacement, it must be written back to disk before being replaced. This ensures that any changes made to the page are not lost.

Types of Page Tables

Depending on the system architecture and requirements, there are several types of page tables used in operating systems. Some of the common types include −

1. Single Level Page Table

In a single-level page table, there is a single table that maps all the pages of a process to their corresponding frames in physical memory. This type of page table is easy to implement. But it may not be suitable for systems with large address spaces, as the page table can become very large and consume a lot of memory.

2. Multi-Level Page Table

In a multi-level page table, the page table is divided into multiple levels. Each level contains a smaller page table that maps a portion of the address space. This hierarchical structure helps to reduce the size of the page table and makes it more manageable. The most common type of multi-level page table is the two-level page table.

3. Inverted Page Table

In an inverted page table, there is a single page table for the entire system. Each entry in the table corresponds to a frame in physical memory. The entry contains information about which page is currently stored in that frame. Also the process ID to which the page belongs. This type of page table is more memory efficient. Because it reduces the amount of memory needed to store page tables for multiple processes. However, it can be slower to access, as the system needs to search through the table to find the corresponding page for a given logical address.

Conclusion

A page table stores important information about the relationship between logical addresses and physical addresses in a virtual memory system. It helps the operating system in managing the memory efficiently and in handling page faults. The key data stored in a page table includes frame number, valid/invalid bit, protection bits, reference bit, caching bit, and dirty bit. There are different types of page tables, such as single-level, multi-level, and inverted page tables.

Advertisements