package items_9;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ymzh {
//最佳置换算法(OPT)--- 无法实现
public static void opt(List<Integer> list){}
//先进先出置换算法(FIFO)
public static void fifo(List<Integer> list){
ArrayList<Integer> block=new ArrayList<Integer>();//物理块
//int lockPage=0; //缺页否
int blockSum=2; //物理快内存
int startBlock=0; //起始物理快
int leftSpace=3; //物理快剩余空间
int len=list.size();
for(int j=0;j<len;){
startBlock=startBlock%3;
if(block.contains(list.get(j))){ //物理快中是否存在该数据
j++;
}else{
if(leftSpace>0){ //物理块中是否存在空余空间
block.add((Integer) list.get(startBlock));
}else{
block.set(startBlock, (Integer) list.get(j));
}
leftSpace--;
startBlock++;
j++;
}
}
//输出物理快中内容
for(int i=0;i<=blockSum;i++){
System.out.println(block.get(i));
}
}
public static int maxThree(int[] arr){
int max=arr[0];
int maxIndex=0;
for(int i=1;i<arr.length;i++){
if(arr[i]>=max){
max=arr[i];
maxIndex=i;
}
}
return maxIndex;
}
//最近最久未使用(LRU)算法
public static void lru(List<Integer> list){
ArrayList<Integer> block=new ArrayList<Integer>();//物理块
//int lockPage=0; //缺页否
int blockSum=2; //物理快内存
int startBlock=0; //起始物理快
int leftSpace=3; //物理快剩余空间
int len=list.size();
for(int j=0;j<len;){
if(block.contains(list.get(j))){ //物理快中是否存在该数据
j++;
}else{
if(leftSpace>0){ //物理块中是否存在空余空间
block.add((Integer) list.get(startBlock));
startBlock++;
}else{
int[] arr=new int[3];
ArrayList<Integer> newList=new ArrayList<Integer>();
for(int k=0;k<j;k++){
newList.add(list.get(k));
}
for(int i=0;i<=blockSum;i++){
arr[i]=j-newList.lastIndexOf(block.get(i));
}
int maxIndex=maxThree(arr);
block.set(maxIndex, (Integer) list.get(j));
}
leftSpace--;
j++;
}
}
//输出物理快中内容
for(int i=0;i<=blockSum;i++){
System.out.println(block.get(i));
}
}
// 时钟(CLOCK)置换算法
public static void clock(List<Integer> list){
ArrayList<Integer> block=new ArrayList<Integer>();//物理块
ArrayList<Integer> blockVal=new ArrayList<Integer>();//物理块标识位置
int flag=1;
int mflag=0;
//int lockPage=0; //缺页否
int blockSum=2; //物理快内存
int startBlock=0; //起始物理快
int leftSpace=3; //物理快剩余空间
int len=list.size();
for(int j=0;j<len;){
startBlock=startBlock%3;
if(block.contains(list.get(j))){ //物理快中是否存在该数据
j++;
}else{
if(leftSpace>0){ //物理块中是否存在空余空间
block.add((Integer) list.get(startBlock));
blockVal.add(flag);
}else{
block.set(startBlock, list.get(j));
for(int k=startBlock;k<=blockSum;k++){
blockVal.set(k, mflag);
}
}
startBlock++;
leftSpace--;
j++;
}
}
//输出物理快中内容
for(int i=0;i<=blockSum;i++){
System.out.println(block.get(i));
}
}
public static void main(String argv[]){
ArrayList<Integer> list=new ArrayList<Integer>();
Scanner sc=new Scanner(System.in);
while(true){
Integer val=sc.nextInt();
if(val==999){
break;
}
list.add(val);
}
//opt(list);
fifo(list); //每执行一个算法要注释其他算法函数调用
//lru(list);
//clock(list);
}
}
我的测试结果如下:
FIFO算法:
7
0
1
2
0
3
0
4
2
3
0
3
2
1
2
0
1
7
0
1
999
7
0
1
LRU算法:
7
0
1
2
0
3
0
4
2
3
0
3
2
1
2
0
1
7
0
1
999
1
0
7
clock算法:
4
3
2
1
4
3
5
4
3
2
1
5
999
5
2
1
测试数据(采自https://blog.youkuaiyun.com/huyang0304/article/details/82694526)