/** * Class to test simulation of page replacement. * *
 * Command-line argument gives number of page frames (and, optionally,
 *   debug to produce extra output -- need not implement this).
 *
 * Input (from standard input) is a line giving number of pages, followed 
 *   by more lines, each giving:
 *
 *   R/W (read/write reference, single character)
 *   Page number (integer)
 *
 * Output is, for each page replacement algorithm implemented, the following 
 *   information:
 *   
 *   Total number of page references
 *   Number of page references that changed the page ('W')
 *   Number of page faults
 *   Number of times a page had to be written out
 *
 * Assume that all page references are valid -- if the page is not in
 *   memory, it's on disk.
 * 
*/ import java.util.List; import java.util.ArrayList; import java.util.Scanner; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class PageReplacerTest { /** * Main method. * @param args see description of class */ public static void main(String[] args) { int numPages = 0; int numFrames = 0; // process arguments String usageMsg = "Need argument: numFrames [debug]"; if (args.length < 1) { System.err.println(usageMsg); System.exit(1); } try { numFrames = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println(usageMsg); System.exit(1); } if (numFrames <= 0) { System.err.println(usageMsg); System.exit(1); } boolean dbg = (args.length > 1) && (args[1].equals("debug")); // read input List refs = null; Scanner input = new Scanner(System.in); try { numPages = input.nextInt(); if (numPages <= 0) throw new InputMismatchException("invalid number of pages"); refs = buildList(input, numPages); } catch (InputMismatchException e) { if (e.getMessage() == null) { System.err.println("Error in input"); } else { System.err.println("Error in input: " + e.getMessage()); } System.exit(1); } catch (NoSuchElementException e) { if (e.getMessage() == null) { System.err.println("Error in input"); } else { System.err.println("Error in input: " + e.getMessage()); } System.exit(1); } new PageReplacerFIFO(numPages, numFrames).simulate(refs, dbg).print(); // TODO: add more } /** * Read input and build list of page references. */ private static List buildList(Scanner input, int numPages) throws InputMismatchException { List refs = new ArrayList(); while (input.hasNext()) { String rw = input.next(); boolean writeRef = false; if (rw.equals("W")) { writeRef = true; } else if (!rw.equals("R")) { throw new InputMismatchException("R/W expected"); } int pageNum = input.nextInt(); if ((pageNum < 0) || (pageNum >= numPages)) { throw new InputMismatchException("invalid page number"); } refs.add(new PageReference(pageNum, writeRef)); } return refs; } }