1 最佳置换算法(OPT): 把将来最长时间不需要访问到的页面移出
2 先进先出置换算法(FIFO): 先进来的页面先淘汰
3 最近最久未使用算法(LRU): 把过去的一段时间内最久不用的页面移出
(与OPT对比 OPT是针对将来时, LRU则是针对过去时)
4 时钟置换算法(CLOCK): 最近未用算法
改进: 增加一个修改位m u则为使用位
最近未被访问,也未被修改(u=0, m=0)
最近被访问,但未被修改(u=1, m=0)
最近未被访问,但被修改(u=0, m=1)
最近被访问,被修改(u=1, m=1)
算法执行如下操作步骤:
从指针的当前位置开始,扫描帧缓冲区。在这次扫描过程中,对使用位不做任何修改。选择遇到的第一个帧(u=0, m=0)用于替换。
如果第1)步失败,则重新扫描,查找(u=0, m=1)的帧。选择遇到的第一个这样的帧用于替换。在这个扫描过程中,对每个跳过的帧,把它的使用位设置成0。
如果第2)步失败,指针将回到它的最初位置,并且集合中所有帧的使用位均为0。重复第1步,并且如果有必要,重复第2步。这样将可以找到供替换的帧。
若新页面不在主存,遍历时候,如果u为1则置0,遇到u为0的则替换,指针指向下一个
若新页面在主存中,则从当前指针遍历到新页面在主存的位置index,新页面设置u为0,刚遍历过的页面的u都置为0,指针指向下一个
每新增一个页面,指针都指向新增页面所在位置的下一个
public class ClockAlgo {
private int maxLength = 5;
private List<PageBean> list = null;
private int index = 0; //当前指针
public ClockAlgo(){
this.list = Lists.newArrayList();
}
public void clean (PageBean replaceBean){
if(list == null)
return;
Boolean removeFlag = false;
while(!removeFlag){
Integer tmpIndex = index;
while(tmpIndex < maxLength ){
PageBean currentPage = list.get(tmpIndex);
//替换
if(currentPage.getM() == 0 && currentPage.getU() == 0){
list.set(tmpIndex,replaceBean);
removeFlag = true;
index = tmpIndex;
break;
}else{
//否则置0
PageBean tmp = list.get(tmpIndex);
tmp.setM(0);
tmp.setU(0);
list.set(tmpIndex,tmp);
}
tmpIndex++;
}
}
}
public void add(PageBean replaceBean){
replaceBean.setU(1);
replaceBean.setM(0);
//已经存在于缓存中
if(list.contains(replaceBean)){
boolean flag = false;
for(int i=index;i<list.size();i++){
//所在页面置1
if(list.get(i).equals(replaceBean)){
list.set(i,replaceBean);
flag = true;
index = i;
break;
}else{
//遍历过的置0
PageBean tmp = list.get(i);
tmp.setU(0);
tmp.setM(0);
list.set(i,tmp);
}
}
//循环遍历 直到找到页面
if(!flag) {
for (int i = 0; i < index; i++) {
if (list.get(i).equals(replaceBean)) {
list.set(i, replaceBean);
index = i;
break;
} else {
PageBean tmp = list.get(i);
tmp.setU(0);
tmp.setM(0);
list.set(i, tmp);
}
}
}
}
//主存还有位置 直接添加
else if(list.size() < maxLength){
list.add(replaceBean);
}else{
clean(replaceBean);
}
//每次指针都指向下一个页面位置
index = (index+1) % maxLength;
}
public void visit(){
int tmpIndex = index;
for(int i=0; i < tmpIndex && i < list.size();i++){
System.out.print(list.get(i).getValue()+"\t");
}
tmpIndex = index;
for(;tmpIndex < maxLength && tmpIndex < list.size();tmpIndex++){
System.out.print(list.get(tmpIndex).getValue()+"\t");
}
System.out.println();
}
}