📜  门| GATE-CS-2017(套装1)|问题 25(1)

📅  最后修改于: 2023-12-03 14:58:32.022000             🧑  作者: Mango

GATE-CS-2017(套装1)- Problem 25

Introduction

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.

Problem Statement

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.

Solution
Data Structures

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
}
Algorithm

The computePhysicalAddress function takes as input the virtual address and returns the corresponding physical address. The function needs to perform the following steps:

  1. Calculate the virtual page number (VPN) and offset from the virtual address.
  2. Check if the VPN is present in the TLB. If the VPN is present in the TLB, retrieve the corresponding physical frame number (PFN) and set the valid bit.
  3. If the VPN is not present in the TLB, traverse the page directory and page tables to find the corresponding physical frame number.
  4. If the VPN is found in the page table, retrieve the corresponding physical frame number and update the TLB with the new VPN-PFN pair.
  5. If the VPN is not found in the page table, generate a page fault exception.
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
}
Time Complexity

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.