📅  最后修改于: 2023-12-03 14:58:32.022000             🧑  作者: Mango
This problem is from the GATE-CS-2017 (Set 1) exam and tests the candidate's knowledge of operating systems and memory management. The problem provides a scenario where a range of Virtual Addresses (VA) is to be mapped to Physical Addresses (PA) using a Translation Lookaside Buffer (TLB) and page tables. The candidate is required to understand the memory management for virtual and physical addresses and implement the mapping of a given virtual address to the corresponding physical address.
The problem statement provides a scenario where a certain range of virtual addresses is to be mapped to corresponding physical addresses using a TLB and page tables. The TLB is a hardware device that stores the recently accessed virtual-to-physical mappings, while the page table is a software data structure that maps virtual pages to physical pages.
The problem requires the candidate to implement a function, computePhysicalAddress
, that takes as input the virtual address and returns the corresponding physical address. The function needs to utilize the TLB and page table to translate the virtual address to the physical address.
We need to define the data structures that will be used to store the TLB and page tables. The TLB can be represented as an array of entries, where each entry consists of a virtual page number, a physical frame number, and a validity bit. The page table can be represented as a two-level hierarchy of page directory and page table pages. Each page directory entry can point to a page table page, which in turn contains the physical frame numbers of the pages in that table.
class TlbEntry {
int vpn; // virtual page number
int pfn; // physical frame number
boolean valid; // validity bit
}
class PageTablePage {
int[] pfn; // physical frame numbers of pages in this table
}
class PageDirectory {
int[] pdn; // physical frame numbers of the page table pages
}
The computePhysicalAddress
function takes as input the virtual address and returns the corresponding physical address. The function needs to perform the following steps:
int computePhysicalAddress(int virtualAddress) {
int vpn = virtualAddress >> 12; // virtual page number
int offset = virtualAddress & 0xFFF; // offset
int pfn = -1; // physical frame number
// check TLB
for (int i = 0; i < TLB_SIZE; i++) {
TlbEntry entry = TLB[i];
if (entry.valid && entry.vpn == vpn) {
pfn = entry.pfn; // found in TLB
break;
}
}
if (pfn == -1) { // not found in TLB
int pdn = vpn >> 10; // page directory number
int ptn = vpn & 0x3FF; // page table number
PageDirectory pageDirectory = physicalMemory[pdn];
if (pageDirectory != null) {
PageTablePage pageTablePage = physicalMemory[pageDirectory.pdn[ptn]];
if (pageTablePage != null) {
pfn = pageTablePage.pfn[vpn & 0x3FF];
// update TLB
int tlbIndex = (int) (Math.random() * TLB_SIZE);
TlbEntry tlbEntry = new TlbEntry();
tlbEntry.vpn = vpn;
tlbEntry.pfn = pfn;
tlbEntry.valid = true;
TLB[tlbIndex] = tlbEntry;
}
}
}
if (pfn == -1) {
throw new PageFaultException(); // page fault
}
return (pfn << 12) | offset; // construct physical address
}
The time complexity of computePhysicalAddress
depends on the number of page directory and page table pages that need to be traversed to find the physical frame number. In the worst case, the function may need to traverse all the page directory and page table pages, resulting in a time complexity of O(2^n), where n is the number of bits in the virtual address. However, the TLB helps to reduce the number of page directory and page table page traversals by caching recently accessed VPN-PFN pairs. The size of the TLB and the hit rate greatly affect the time complexity.