Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to create a demand paging simulator in java, and I am having trouble getting the LRU part of the code to work properly.

I need to create a demand paging simulator in java, and I am having trouble getting the LRU part of the code to work properly. Here is what I have so far, I just need the LRU algorithm to be added before the LFU method.

import java.util.ArrayList; import java.util.LinkedList; public class Algorithms { private int pframes = 4; //[0-3] private String referenceString; //Sequence of pages to be read private int pageMethod; //0=fifo, 1=opt, 2=lru, 3=lfu; private int numOfAvaiableFrames = 4; private int numberOfPageFaults = 0; //constructor // set the reference string and algorthm type public Algorithms(String referenceString, int pageMethod) { this.referenceString = referenceString; this.pageMethod = pageMethod; } //executes the solution public void run() { switch (this.pageMethod) { case 0: // fifo fifo(this.referenceString); break; case 1: // opt opt(this.referenceString); break; case 2: // lru lru(this.referenceString); break; case 3: //lfu lfu(this.referenceString); break; default: } System.out.println("**************** RESULTS ********************************************"); System.out.println("\t" + "Simulation complete: " + this.referenceString.length() + " pages processed."); System.out.println("\t" + "Total faults: " + this.numberOfPageFaults); System.out.println("*********************************************************************"); } // FIFO algorithm using the stored reference @SuppressWarnings("empty-statement") void fifo(String rs) { // Set up array for physical frames ArrayList physicalFrames = new ArrayList(); physicalFrames.add(-1); physicalFrames.add(-1); physicalFrames.add(-1); physicalFrames.add(-1); int loopFault; int victimFrame; //for each vframe (character in string) referenced to process for (int i = 0; i loopFault = 0; victimFrame = -1; //covnvert current ref to int int currentPage = Integer.parseInt(Character.toString(this.referenceString.charAt(i))); Integer integerCurrentPage = new Integer(currentPage); System.out.println("Processing frame " + currentPage + " (frame " + i + " of " + this.referenceString.length() + ")"); //info //If there are null pframes add to that (also make sure it isn't already in there) if (this.numOfAvaiableFrames > 0 && (!physicalFrames.contains(integerCurrentPage))) { //System.out.println( "ADD ITEM TO OPEN PHY FRAME "); //push simulate physicalFrames.set(3, physicalFrames.get(2)); physicalFrames.set(2, physicalFrames.get(1)); physicalFrames.set(1, physicalFrames.get(0)); physicalFrames.set(0, currentPage); loopFault = 1; //for display processing this.numberOfPageFaults++; //track these this.numOfAvaiableFrames--; } else { // either do nothing if there is a match if (physicalFrames.indexOf(currentPage) // or No Match, time to make a victim and pagefault victimFrame = Integer.parseInt(physicalFrames.get(3).toString()); physicalFrames.set(3, physicalFrames.get(2)); physicalFrames.set(2, physicalFrames.get(1)); physicalFrames.set(1, physicalFrames.get(0)); physicalFrames.set(0, currentPage); loopFault = 1; //for display processing this.numberOfPageFaults++; } } //do a printout System.out.print("Ref String pg: " + currentPage + " |" + physicalFrames.toString().replaceAll("-1", "-")); if (loopFault == 1) { System.out.print(" PAGE FAULT "); } if (victimFrame != -1) { System.out.print(" VICTIM " + victimFrame); } System.out.print(" ---------------------------------- (press enter to continue) "); //System.out.print("PHYSICAL FRAMES:"+ physicalFrames.get(0).toString()+physicalFrames.get(1).toString()); try { System.in.read(); } catch (Exception e) { }; } //end for each ref }//-------------------------------------------------------- // OPT algorithm using the stored reference string @SuppressWarnings("empty-statement") void opt(String rs) { // Set up array for physical frames ArrayList physicalFrames = new ArrayList(); int[] futureUseBit = new int[4]; futureUseBit[0] = 0; futureUseBit[1] = 0; futureUseBit[2] = 0; futureUseBit[3] = 0; int loopFault; int victimFrame; //for each vframe (character in string) referenced to process for (int i = 0; i loopFault = 0; victimFrame = -1; //covnvert current ref to int int currentPage = Integer.parseInt(Character.toString(this.referenceString.charAt(i))); Integer integerCurrentPage = new Integer(currentPage); System.out.println("Processing frame " + currentPage + " (frame " + i + " of " + this.referenceString.length() + ")"); //info //If there are null pframes add to that (also make sure it isn't already in there) if (this.numOfAvaiableFrames > 0 && (!physicalFrames.contains(integerCurrentPage))) { for (Object physicalFrame : physicalFrames) { if (Integer.parseInt(physicalFrame.toString()) >= 0) { } } physicalFrames.add(currentPage); int addedat = physicalFrames.indexOf(currentPage); loopFault = 1; //for display processing this.numberOfPageFaults++; //track these this.numOfAvaiableFrames--; } else { // either do nothing if there is a match // determine number of future uses for each item in "physical frame" - pf for (int pf = 0; pf int numOfFutureUses = 0; for (int rin = i; rin if (Integer.parseInt(physicalFrames.get(pf).toString()) == Integer.parseInt(Character.toString(this.referenceString.charAt(rin)))) { numOfFutureUses += 1; } } futureUseBit[pf] = numOfFutureUses; } if (physicalFrames.indexOf(currentPage) // or No Match, time to make a victim and pagefault int leastUsefulElement = 0; int smallest = 7; for (int c = 0; c if (futureUseBit[c] leastUsefulElement = c; smallest = futureUseBit[c]; } } victimFrame = Integer.parseInt(physicalFrames.get(leastUsefulElement).toString()); physicalFrames.set(leastUsefulElement, currentPage); loopFault = 1; //for display processing this.numberOfPageFaults++; } } //System.out.println("futureUseBit " + futureUseBit[0] + futureUseBit[1] + futureUseBit[2] + futureUseBit[3]); //do a printout System.out.print("Ref String pg: " + currentPage + " |" + physicalFrames.toString().replaceAll("-1", "-")); if (loopFault == 1) { System.out.print(" PAGE FAULT "); } if (victimFrame != -1) { System.out.print(" VICTIM " + victimFrame); } System.out.print(" ---------------------------------- (press ENTER to continue) "); //System.out.print("PHYSICAL FRAMES:"+ physicalFrames.get(0).toString()+physicalFrames.get(1).toString()); try { System.in.read(); } catch (Exception e) { }; } //end for each ref }//-------------------------------------------------------- //LFU algorithm using the stored reference string @SuppressWarnings("empty-statement") void lfu(String rs) { // Set up array for physical frames ArrayList physicalFrames = new ArrayList(); int[] sinceUseBit = new int[4]; sinceUseBit[0] = 0; sinceUseBit[1] = 0; sinceUseBit[2] = 0; sinceUseBit[3] = 0; int loopFault; int victimFrame; //for each vframe (character in string) referenced to process for (int i = 0; i loopFault = 0; victimFrame = -1; //covnvert current ref to int int currentPage = Integer.parseInt(Character.toString(this.referenceString.charAt(i))); Integer integerCurrentPage = new Integer(currentPage); System.out.println("Processing frame " + currentPage + " (frame " + i + " of " + this.referenceString.length() + ")"); //info //If there are null pframes add to that (also make sure it isn't already in there) if (this.numOfAvaiableFrames > 0 && (!physicalFrames.contains(integerCurrentPage))) { physicalFrames.add(currentPage); int addedat = physicalFrames.indexOf(currentPage); sinceUseBit[addedat] += 1; loopFault = 1; //for display processing this.numberOfPageFaults++; //track these this.numOfAvaiableFrames--; } else { // either do nothing if there is a match int addedat = physicalFrames.indexOf(currentPage); try { sinceUseBit[addedat] += 1; } catch (Exception e) { } if (physicalFrames.indexOf(currentPage) // or No Match, time to make a victim and pagefault int leastFrequentElement = 0; int smallest = 7; for (int c = 0; c if (sinceUseBit[c] leastFrequentElement = c - 1; } } victimFrame = Integer.parseInt(physicalFrames.get(leastFrequentElement).toString()); physicalFrames.set(leastFrequentElement, currentPage); sinceUseBit[leastFrequentElement] = 0; loopFault = 1; //for display processing this.numberOfPageFaults++; } } //do a printout System.out.print("Ref String pg: " + currentPage + " |" + physicalFrames.toString().replaceAll("-1", "-")); if (loopFault == 1) { System.out.print(" PAGE FAULT "); } if (victimFrame != -1) { System.out.print(" VICTIM " + victimFrame); } //Press Enter System.out.print(" ---------------------------------- (press ENTER to continue) "); try { System.in.read(); } catch (Exception e) { }; } //end for each ref } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

More Books

Students also viewed these Databases questions

Question

Is it clear what happens if an employee violates the policy?

Answered: 1 week ago