Last active
June 23, 2016 07:37
-
-
Save rohitvvv/f9386995221be52bfd56 to your computer and use it in GitHub Desktop.
LRU Simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.concurrent.ConcurrentHashMap; | |
/** | |
* Created by rohit on 2/1/16. | |
* Program to implement LRU algorithm. | |
* Min value priority : Indicates lease recently used. | |
* Max value priority: Indicates recently used. | |
* I Hit 2 Hit 1 Hit 3 Hit 2 Miss 6 | |
* |1 1| |1 0| |1 3| |1 2| |1 1| |6 3| | |
* |2 2| |2 3| |2 2| |2 1| |2 3| |2 2| | |
* |3 3| |3 2| |3 1| |3 3| |3 2| |3 1| | |
*/ | |
/* | |
*Container for the page. | |
*/ | |
class Frame{ | |
Page[] frame; | |
int frameSize; | |
//Hashmap which holds the prority for next insertion | |
//key: the page data. value: position in the frame | |
ConcurrentHashMap<Integer,Integer> priority; | |
//Initiaze the page frame | |
Frame(int size,Page[] pages){ | |
frame = new Page[size]; | |
frameSize = size; | |
int cnt=0; | |
priority = new ConcurrentHashMap<>(); | |
for(Page pg:pages){ | |
frame[cnt]=pg; | |
priority.put(pg.data,cnt); | |
cnt++; | |
} | |
} | |
public void printPageFrame(){ | |
for(int i=0;i<frameSize;i++) | |
System.out.println(frame[i].toString()); | |
System.out.println(); | |
} | |
//Returns true if its a hit | |
//Otherwise returns false implicitly indicating a miss | |
public boolean isHit(Page page){ | |
for(Page currPage:frame){ | |
if(currPage.equals(page)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
public int findMaxPriority() { | |
Set set = priority.keySet(); | |
Iterator itr = set.iterator(); | |
int max; | |
max = priority.get(itr.next()); | |
while (itr.hasNext()) { | |
int val = priority.get(itr.next()); | |
if (val > max) | |
max = val; | |
} | |
return max; | |
} | |
public int findMinPriority() { | |
Set set = priority.keySet(); | |
Iterator itr = set.iterator(); | |
int min; | |
min = priority.get(itr.next()); | |
while (itr.hasNext()) { | |
int val = priority.get(itr.next()); | |
if (val < min) | |
min = val; | |
} | |
return min; | |
} | |
//No page frame change is desired as its a hit. | |
//Page not found in the frame | |
public void setMissPriority(Page page){ | |
//Find min in the set. | |
int min = findMinPriority(); | |
int max = findMaxPriority(); | |
//At the hash key which has min put the max value | |
Iterator itr1 = priority.keySet().iterator(); | |
while(itr1.hasNext()){ | |
Integer pg = (Integer)itr1.next(); | |
if(priority.get(pg)==min){ | |
priority.put(page.data,max); | |
//Removed swapped out page of the frame | |
priority.remove(pg); | |
searchAndReplace(pg,page.data); | |
} | |
} | |
//Reduce other priorities | |
Iterator itr = priority.keySet().iterator(); | |
while(itr.hasNext()){ | |
int value = (Integer)itr.next(); | |
if(value != page.data){ | |
int pri = priority.get(value); | |
priority.put(value,--pri); | |
} | |
} | |
} | |
public void searchAndReplace(int x,int y){ | |
int i=0; | |
for(i=0;i<frame.length;i++){ | |
if(frame[i].data == x) | |
frame[i].data = y; | |
} | |
} | |
//Page found in the frame | |
public void setHitPriority(Page page){ | |
Set set = priority.keySet(); | |
//Find min in the value set. | |
int min= findMaxPriority(); | |
priority.put(page.data,min); | |
Iterator itr = priority.keySet().iterator(); | |
while(itr.hasNext()){ | |
int value = (Integer)itr.next(); | |
if(value!=page.data){ | |
int pri = priority.get(value); | |
priority.put(value,--pri); | |
} | |
} | |
} | |
public boolean addPage(Page page){ | |
if(isHit(page)){ | |
setHitPriority(page); | |
return true; | |
}//Its a miss replace the page | |
else{ | |
setMissPriority(page); | |
} | |
return false; | |
} | |
@Override | |
public String toString() { | |
StringBuffer buff = new StringBuffer(); | |
for(int i=0;i<frame.length;i++) | |
buff.append(" "+frame[i].data+" "); | |
return buff.toString(); | |
} | |
} | |
/* | |
* Represents the Page which will sit int he frame. | |
*/ | |
class Page{ | |
int data; | |
Page(int data){ | |
this.data=data; | |
} | |
@Override | |
public String toString() { | |
return Integer.toString(data); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if(obj instanceof Page){ | |
Page page =(Page)obj; | |
if(page.data == this.data) | |
return true; | |
} | |
return false; | |
} | |
} | |
public class Main { | |
public static void main(String[] args){ | |
Frame frame = new Frame(3,new Page[]{new Page(10),new Page(20),new Page(30)}); | |
System.out.println(frame); | |
frame.addPage(new Page(10)); | |
System.out.println(frame); | |
frame.addPage(new Page(11)); | |
System.out.println(frame); | |
frame.addPage(new Page(12)); | |
System.out.println(frame); | |
frame.addPage(new Page(13)); | |
System.out.println(frame); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment