/* * Paging.java (Implements Paging System.). * * Written by : Nitin Motgi (nmotgi@cs.ucf.edu) * * This performs the main paging initialisation and request * dispatching to the appropriate algorithms. * * Portions copyright(c) 2001 to School of Electrical Engineering and * Computer Science, UCF, Orlando. * * Use and distribution of this source code are strictly governed by * terms and conditions set by the authors. * * $Id : Paging.java, v1.0.0 02/24/2001. $ * * Revision History: * */ /* Import some of the libraries.*/ import java.lang.*; import java.util.*; import java.io.*; public class Paging{ /* stores the name of the file.*/ public static String szFileName; /* Stores the complete program.*/ private static int nNoOfRequest; private static int nTotalSize; private static int nPageSize; private static boolean bDebugOption; /* Process the command line and simulates the requests for processors.*/ public static void main(String[] szArgs){ String[][] szRequest; /* Process command line and perform integrity check.*/ if(szArgs.length == 0){ System.out.println("** ERROR : No Input file specified."); System.out.println("Usage : Paging "); return; }/* End if.*/ /* Check if Debug is On.*/ if(szArgs.length == 2){ String szTemp = szArgs[1]; /* read in the second command line for the Debug Option.*/ if((szTemp.toLowerCase()).equals("debug")){ bDebugOption = true; } else{ System.out.println("** ERROR : Improper command parameter."); System.out.println("Usage : Paging "); System.exit(-1); /* Abnormal Termination.*/ }/* End if.*/ }else{ bDebugOption = false; }/*End if.*/ /* Store file name.*/ szFileName = szArgs[0]; szRequest = new String[500][3]; /* Process command line. Will return object containing set of parameters that will be used during simulation after parsing the input.*/ ProcessCommandLine(szRequest); System.out.println("Total memory space: " + szRequest[0][2]); nTotalSize = Integer.parseInt(szRequest[0][2]); nPageSize = Integer.parseInt(szRequest[1][2]); System.out.println("Number of frames: " + nTotalSize/nPageSize + "(" + szRequest[1][2] + " each)"); /* Simulate LRU.*/ LRU LRUalgo = new LRU(szRequest,nNoOfRequest,nTotalSize/nPageSize, nPageSize,bDebugOption); LRUalgo.Start(); if(bDebugOption){ System.out.print("Press any key to continue..."); kbhit(); }/* End if.*/ /* Simulates LFU.*/ LFU LFUalgo = new LFU(szRequest,nNoOfRequest,nTotalSize/nPageSize, nPageSize,bDebugOption); LFUalgo.Start(); if(bDebugOption){ System.out.print("Press any key to continue..."); kbhit(); }/* End if.*/ /* Simulate MFU.*/ MFU MFUalgo = new MFU(szRequest,nNoOfRequest,nTotalSize/nPageSize, nPageSize,bDebugOption); MFUalgo.Start(); if(bDebugOption){ System.out.print("Press any key to continue..."); kbhit(); }/* End if.*/ /* Simulates FIFO.*/ FIFO FIFOalgo = new FIFO(szRequest,nNoOfRequest,nTotalSize/nPageSize, nPageSize,bDebugOption); FIFOalgo.Start(); if(bDebugOption){ System.out.print("Press any key to continue..."); kbhit(); }/* End if.*/ }/* End main.*/ /* This function parses the input file and fills in the data structure will properties that are required to run the simulation. The parameters are returned as class which extends Object.*/ private static void ProcessCommandLine(String[][] szRequest){ /* Buffer to store the line.*/ String szLineBuffer; String szLowerCaseLine; /* Track Line #.*/ int nLineCount=0; /* Column count.*/ int nColumnCount; /* Store all the delimters in the string for processing input.*/ String szDelimiter = new String(":, "); /* Current Token.*/ String szToken; /* Current Token that is being proceesed.*/ try{ /* Open the Input file.*/ BufferedReader In = new BufferedReader(new FileReader(szFileName)); /* Initialise Line count.*/ nLineCount = 0; /* Read each line and process it.*/ while((szLineBuffer = In.readLine()) != null){ /* Check if first line of the character is EOL. if so then skip.*/ if(szLineBuffer.length() == 0) continue; /* Check if the Line is comment or no, if yes then it useless to continue tokenizing the string.*/ if(szLineBuffer.length() >= 2) if(szLineBuffer.charAt(0) == '/') if(szLineBuffer.charAt(1) == '/') continue; else { System.out.println(szFileName + ": Line: " + nLineCount + " Improper commenting. (skipping)"); continue; }/* End if.*/ /* Turn the line into lower case.*/ szLowerCaseLine = szLineBuffer.toLowerCase(); /* Tokenize all the Objects in the string.*/ StringTokenizer Tokens = new StringTokenizer(szLowerCaseLine, szDelimiter); nColumnCount = 0; while(Tokens.hasMoreTokens()){ try{ szToken = Tokens.nextToken(); szRequest[nLineCount][nColumnCount] = szToken; if(nColumnCount == 2){ String szLastToken = szRequest[nLineCount][nColumnCount]; int nLastIndex = szLastToken.length()-1; if(szLastToken.charAt(nLastIndex) == 'K' || szLastToken.charAt(nLastIndex) == 'k'){ StringTokenizer kToken = new StringTokenizer(szLastToken, "kK"); String szNumber = kToken.nextToken(); szNumber = szNumber + "000"; szRequest[nLineCount][nColumnCount] = szNumber; }/* End if.*/ }/* End if.*/ nColumnCount++; }/* End of try.*/ catch(NumberFormatException e){ e.printStackTrace(); }/* End of catch.*/ }/* End of while.*/ nLineCount++; }/* End of while.*/ In.close(); }catch(Exception e){ System.out.println("** ERROR : Input file " + szFileName + " not found."); System.out.println("Abnormal Termination."); System.exit(-1); }/* End of try catch Block.*/ nNoOfRequest = nLineCount; }/* End of ProcessCommandLine.*/ /* Wait till key is pressed. Upon receiving that skip all the characters that were pressed before pressing enter. If these characters are not skipped then it will intefere when next read is generated.*/ private static void kbhit(){ try{ System.in.read(); System.in.skip(System.in.available()); }catch(Exception e){ e.printStackTrace(); }/* End of try-catch block.*/ System.out.println(""); }/* End of kbhit.*/ }/* End of Paging.*/