/* 
 * LRU.java (Implements Paging Algorithm Least Recently Used for 
 *           replacing pages from frame system)   
 *
 * Written by : Nitin  Motgi (nmotgi@cs.ucf.edu)
 *
 * 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.
 *
 * ASSUMPTIONS : Uses a global counter described in super class.
 * 
 * $Id : LRU.java, v1.0.0 03/02/2001. $
 *            
 * Revision History:
 *
 * 1. Created basic structure           Nitin,        v1.0.0  03/14/2001.
 * 2. Added Documentation.              Nitin,        v1.0.1  03/14/2001.
 * 3. Final Documentation Check.        Nitin,        v1.4.3  03/26/2001.  
 * 4. Final Variable Name Check.        Nitin,        v1.4.4  03/26/2001.          
 * 5. Final Functionality Check.        Nitin,        v1.4.5  03/26/2001.          
*/

/* Import some of the libraries.*/
import java.lang.*;
import java.util.*;
import java.io.*;


class LRU extends PagingAlgorithm{
 String[][] szRequest;
 int nPageSize;
 int nNoOfRequest;

 public LRU(String[][] szRequest,int nNoOfRequest,
	    int nNoOfFrames, int nPageSize,boolean bDebug){
  this.szRequest = szRequest;
  this.nNoOfRequest = nNoOfRequest;
  this.nPageSize = nPageSize;
  super.nNoOfFrames = nNoOfFrames;
  super.bDebug = bDebug;
  super.Create();
 }/* End of constructor.*/

 public void read(int nProgName, int nPageIndex,int nAddress){
   super.read(nProgName, nPageIndex, nAddress);
 }/* End of read.*/

 public void load(int nProgName, int nProgSize){
  super.load(nProgName, nProgSize, nPageSize);
 }/* End of read.*/

 public void write(int nProgName, int nPageIndex,int nAddress){
  super.write(nProgName, nPageIndex, nAddress);
 }/* End of read.*/

 public void unload(int nProgName){
  super.unload( nProgName);
 }/* End of read.*/

 public void Start(){
  System.out.println("Using LRU ...");
  for(int nIndex=2; nIndex < nNoOfRequest; nIndex++){
   if(szRequest[nIndex][0].equals("load"))
      load(Integer.parseInt(szRequest[nIndex][1]),
	   Integer.parseInt(szRequest[nIndex][2]));
	    
   if(szRequest[nIndex][0].equals("read"))
      read(Integer.parseInt(szRequest[nIndex][1]),
	   (int)Integer.parseInt(szRequest[nIndex][2])/nPageSize,
	   Integer.parseInt(szRequest[nIndex][2]));

   if(szRequest[nIndex][0].equals("write"))
      write(Integer.parseInt(szRequest[nIndex][1]),
	    (int)Integer.parseInt(szRequest[nIndex][2])/nPageSize,
	    Integer.parseInt(szRequest[nIndex][2]));

   if(szRequest[nIndex][0].equals("unload"))
      unload(Integer.parseInt(szRequest[nIndex][1]));
		  
  }/* End for.*/
  System.out.println("Number of page faults for LRU:"+super.nPageFault);
 }/* End of Start.*/

 public int getVictim(int nProgName){
  Frame newFrame; 
  int nEmptyIndex=-1;
  long lTimeStamp = 99999999;
  int nLRUIndex=-1;

  for(int nIndex=0; nIndex < super.nNoOfFrames; nIndex++){
   newFrame = (Frame)super.FrameList.elementAt(nIndex);
   if(newFrame.bFrameStatus == false) return nIndex;
   else{
    if(lTimeStamp >= newFrame.lTimeStamp){
     nLRUIndex = nIndex;
     lTimeStamp = newFrame.lTimeStamp;
    }/* End if.*/
   }/* End if.*/
  }/* End for.*/
  return nLRUIndex;
 }/* End of getVicitim.*/
}/* End of LRU.*/

				 

