Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The input address are : 16916 62493 30198 the output must be as follows: 0 0 29 my program run well but its not giving
The input address are : 16916 62493 30198 the output must be as follows:
0 0 29 my program run well but its not giving me the right answer can you fix it for me and till me why did that happens this is my program : import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Scanner; public class VirtualMemory { private static final int PAGE_SIZE = 256; private static final int NUM_PAGES = 256; private static final int TLB_SIZE = 16; private static final int PAGE_TABLE_SIZE = 256; private static final int PHYSICAL_MEMORY_SIZE = 65536; private static int[] pageTable = new int[PAGE_TABLE_SIZE]; private static TLBEntry[] TLB = new TLBEntry[TLB_SIZE]; private static byte[] physicalMemory = new byte[PHYSICAL_MEMORY_SIZE]; public static void main(String[] args) throws IOException { try { Scanner input = new Scanner(new File("addresses.txt")); while (input.hasNextInt()) { int logicalAddress = input.nextInt(); int pageNumber = logicalAddress >> 8; int offset = logicalAddress & 0xff; int frameNumber = searchTLB(pageNumber); if (frameNumber == -1) { frameNumber = pageTable[pageNumber]; if (frameNumber == -1) { frameNumber = handlePageFault(pageNumber); } updateTLB(pageNumber, frameNumber); } int physicalAddress = (frameNumber << 8) + offset; byte value = physicalMemory[physicalAddress]; System.out.println("Logical address: " + logicalAddress + " Physical address: " + physicalAddress + " Value: " + value); } input.close(); } catch (FileNotFoundException e) { System.out.println("File not found"); System.exit(1); } } private static int handlePageFault(int pageNumber) throws IOException { // code to handle page fault // This method is called when the page is not present in physical memory. // First, the page is loaded from the backing store (BACKING_STORE.bin) into a free frame in physical memory. // Next, the page table is updated with the frame number. // Finally, the method returns the frame number. int frameNumber = -1; for (int i = 0; i < PHYSICAL_MEMORY_SIZE; i += PAGE_SIZE) { if (physicalMemory[i] == 0) { frameNumber = i / PAGE_SIZE; break; } } if (frameNumber == -1) { // physical memory is full, need to implement page replacement algorithm } else { try { RandomAccessFile backingStore = new RandomAccessFile("BACKING_STORE.bin", "r"); backingStore.seek(pageNumber * PAGE_SIZE); int count = backingStore.read(physicalMemory, frameNumber * PAGE_SIZE, PAGE_SIZE); if (count != PAGE_SIZE) { System.out.println("Error reading from backing store"); } backingStore.close(); } catch (FileNotFoundException e) { System.out.println("Backing store not found"); } catch (IOException e) { System.out.println("I/O error"); } pageTable[pageNumber] = frameNumber; } return frameNumber; } private static void updateTLB(int pageNumber, int frameNumber) { // code to update TLB // This method is called when a page is successfully mapped to a frame in physical memory. // The method updates the TLB with the mapping of the page to the frame. // First, it checks if the TLB is full. If it is, the oldest entry is replaced. // If the TLB is not full, a new entry is added to the end of the TLB. for (int i = 0; i < TLB_SIZE; i++) { if (TLB[i] == null) { TLB[i] = new TLBEntry(pageNumber, frameNumber); break; } } if (TLB[TLB_SIZE - 1] != null) { // TLB is full, replace oldest entry for (int i = 0; i < TLB_SIZE - 1; i++) { TLB[i] = TLB[i + 1]; } TLB[TLB_SIZE - 1] = new TLBEntry(pageNumber, frameNumber); } } private static int searchTLB(int pageNumber) { // code to search TLB for (int i = 0; i < TLB.length; i++) { if (TLB[i] != null && TLB[i].pageNumber == pageNumber) { return TLB[i].frameNumber; } } return -1; } private static class TLBEntry { int pageNumber; int frameNumber; public TLBEntry(int pageNumber, int frameNumber) { this.pageNumber = pageNumber; this.frameNumber = frameNumber; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started