/***************************************************** * * * CSC468H1 2000 Fall * * Assignment 2 -- Problem 2 * * * * Student Name: Wei Wang * * : Jeff Xing * * Student Number: 984336700 * * : 990555323 * * * *****************************************************/ import java.io.*; import java.util.*; /** * A simulation on performance of three different page-replacement * algorithms: FIFO (first in, first out), * LFU (least frequently used) and * PFF (page fault frequency) * with different memory sizes (or thresholds), by giving a page * reference string, which contains s series of page refencece * calls. By comapring the performance of each case, a better * algorithm with a specific memory size (threshold) could be * concluded. * */ class a2q2 { /** * a2q2 constructor */ public a2q2() { super(); } public static void main(String[] args) { String filename = "a2_data.txt"; int pageReference[] = new int[1000]; int i = 0; // initialize the page reference to -1 (invalid page reference) for (int j=0; j<1000; j++) { pageReference[j] = -1; } // get input from command line if (args.length > 0) { filename = args[0]; } // read page reference strings from file and store into pageReference array try { RandomAccessFile inFile = new RandomAccessFile(filename, "r"); String inString; while ((inString = inFile.readLine()) != null) { if (i >= 1000) { System.out.println("More than 1000 page reference strings"); System.exit(0); } StringTokenizer st = new StringTokenizer(inString); while (st.hasMoreTokens()) { pageReference[i] = Integer.parseInt(st.nextToken()); i++; } } } catch (Exception e) { System.out.println("Exception: " + e); } Fifo fifo = new Fifo(); double x1 = fifo.run(pageReference, 6); double x2 = fifo.run(pageReference, 10); double x3 = fifo.run(pageReference, 16); Lfu lfu = new Lfu(); double[] y = lfu.run(pageReference); Pff pff = new Pff(); double z1[] = pff.run(pageReference, 8); double z2[] = pff.run(pageReference, 12); double z3[] = pff.run(pageReference, 16); Format x = new Format(); System.out.println("# of page references = " + i); System.out.println("=========================================================="); System.out.println("FIFO"); System.out.println("=========================================================="); System.out.println("Frames 6 10 16"); System.out.println("----------------------------------------------------------"); x.print(System.out, "Page faults %10.4f", x1); x.print(System.out, " %10.4f", x2); x.print(System.out, " %10.4f\n", x3); x.print(System.out, "Faults/frames %10.4f", x1/6); x.print(System.out, " %10.4f", x2/10); x.print(System.out, " %10.4f\n", x3/16); System.out.println("=========================================================="); System.out.println("LFU"); System.out.println("=========================================================="); System.out.println("Frames 6 10 16"); System.out.println("----------------------------------------------------------"); x.print(System.out, "Page faults %10.4f", y[0]); x.print(System.out, " %10.4f", y[1]); x.print(System.out, " %10.4f\n", y[2]); x.print(System.out, "Faults/frames %10.4f", y[0]/6); x.print(System.out, " %10.4f", y[1]/10); x.print(System.out, " %10.4f\n", y[2]/16); System.out.println("=========================================================="); System.out.println("PFF"); System.out.println("=========================================================="); System.out.println("Threshold 8 12 16"); System.out.println("----------------------------------------------------------"); x.print(System.out, "Frames (total) %10.4f", z1[1]); x.print(System.out, " %10.4f", z2[1]); x.print(System.out, " %10.4f\n", z3[1]); x.print(System.out, "Frames (mean) %10.4f", z1[1]/i); x.print(System.out, " %10.4f", z2[1]/i); x.print(System.out, " %10.4f\n", z3[1]/i); x.print(System.out, "Page faults %10.4f", z1[0]); x.print(System.out, " %10.4f", z2[0]); x.print(System.out, " %10.4f\n", z3[0]); x.print(System.out, "Faults/frames %10.4f", z1[0]/(z1[1]/i)); x.print(System.out, " %10.4f", z2[0]/(z2[1]/i)); x.print(System.out, " %10.4f\n", z3[0]/(z3[1]/i)); } }