In a computer operating system that uses paging for virtual memory management, page replacement algorithms decide which memory pages to page out (swap out, write to disk) when a page of memory needs to be allocated. Paging happens when a page fault occurs and a free page cannot be used to satisfy the allocation, either because there are none, or because the number of free pages is lower than some threshold.
When the page that was selected for replacement and paged out is referenced again it has to be paged in (read in from disk), and this involves waiting for I/O completion. This determines the quality of the page replacement algorithm: the less time waiting for page-ins, the better the algorithm. A page replacement algorithm looks at the limited information about accesses to the pages provided by hardware, and tries to guess which pages should be replaced to minimize the total number of page misses, while balancing this with the costs (primary storage and processor time) of the algorithm itself.
The page replacing problem is a typical online problem from the competitive analysis perspective in the sense that the optimal deterministic algorithm is known.
/*
* LRU.java (Implements Paging Algorithm Least Recently Used for
* replacing pages from frame system)
*
* Written by : Abdul Mugeesh
*
* 1. Created basic structure
* 2. Added Documentation.
* 3. Final Documentation Check.
* 4. Final Variable Name Check.
* 5. Final Functionality Check.
*/
/* 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.*/
Comments
Post a Comment