问题
假设有10个页面,n个页框。页面的访问顺序为0, 9, 8, 4, 4, 3, 6, 5, 1, 5, 0, 2, 1, 1, 1, 1, 8, 8, 5, 3, 9, 8, 9, 9, 6, 1, 8, 4, 6, 4, 3, 7, 1, 3, 2, 9, 8, 6, 2, 9, 2, 7, 2, 7, 8, 4, 2, 3, 0, 1, 9, 4, 7, 1, 5, 9, 1, 7, 3, 4, 3, 7, 1, 0, 3, 5, 9, 9, 4, 9, 6, 1, 7, 5, 9, 4, 9, 7, 3, 6, 7, 7, 4, 5, 3, 5, 3, 1, 5, 6, 1, 1, 9, 6, 6, 4, 0, 9, 4, 3。
当n在[1,10]中取值时,用OPT、 LRU、 FIFO页面置换算法,分别计算缺页数量,画出缺页数量随页框数n的变化曲线(3条线) id="tmp_downloadhelper_iframe" style="display: none;">
介绍
一,最优置换(Optimal):从主存中移出永远不再需要的页面,如无这样的页面存在,则应选择最长时间不需要访问的页面。
二,先进先出算法(First-in, First-out):总选择作业中在主存驻留时间最长的一页淘汰。
三,最近最久不用的页面置换算法(Least Recently Used Replacement):当需要置换一页面时,选择在最近一段时间内最久不用的页面予以淘汰。
代码
import java.util.*;
public class t {
static int n=10;
static List<Integer> p=Arrays.asList(0, 9, 8, 4, 4, 3, 6, 5, 1, 5, 0, 2, 1, 1, 1, 1, 8, 8, 5, 3, 9, 8, 9, 9, 6, 1, 8, 4, 6, 4, 3, 7, 1, 3, 2, 9, 8, 6, 2, 9, 2, 7, 2, 7, 8, 4, 2, 3, 0, 1, 9, 4, 7, 1, 5, 9, 1, 7, 3, 4, 3, 7, 1, 0, 3, 5, 9, 9, 4, 9, 6, 1, 7, 5, 9, 4, 9, 7, 3, 6, 7, 7, 4, 5, 3, 5, 3, 1, 5, 6, 1, 1, 9, 6, 6, 4, 0, 9, 4, 3);
public static void main(String[] args) {
for(int i=1;i<=n;i++){
System.out.println("n="+i+":");
System.out.print("OPT:"+OPT(i)+"\t");
System.out.print("LRU:"+LRU(i)+"\t");
System.out.print("FIFO:"+FIFO(i)+"\t\n");
}
}
public static int OPT(int x){
int find=0;
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i=0;i<100;i++){
if(a.contains(p.get(i))){
find++;
continue;
}
if(a.size()<x)
a.add(p.get(i));
else{
ArrayList<Integer> temp = new ArrayList<Integer>();
int j;
for(j=i+1;j<100&&temp.size()<x-1;j++){ // 向后查找将用到的元素
if(a.contains(p.get(j))&&!temp.contains(p.get(j))){
temp.add(p.get(j));
}
}
if(temp.size()==x-1||j==100){ // 发现了最不会被用到的元素 或者 没发现但已到末尾
for(int k=0;k<x;k++){
if(!temp.contains(a.get(k))){
a.remove(k);
a.add(p.get(i));
break;
}
}
}
}
}
return 100-find;
}
public static int LRU(int x) {
int find=0;
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i=0;i<100;i++){
if(a.contains(p.get(i))){
find++;
a.remove(p.get(i));
a.add(p.get(i)); // 更新该元素位置(即Recent排序)
continue;
}
if(a.size()<x)
a.add(p.get(i));
else{
a.remove(0);
a.add(p.get(i));
}
}
return 100-find;
}
public static int FIFO(int x) {
int find=0;
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i=0;i<100;i++){
if(a.contains(p.get(i))){
find++;
continue;
}
if(a.size()<x)
a.add(p.get(i));
else{
a.remove(0);
a.add(p.get(i));
}
}
return 100-find;
}
}