(操作系统实验)内存页面淘汰算法---FIFO、LRU、OPT

算法介绍

参考操作系统或计算机组成原理相关教材,很容易理解,介绍略。可以参考笔者后续实验结果,有助于理解算法。

源代码

由于算法简单,不再进行画图设计思路,笔者直接编写代码,遇到问题随之解决。下面是源代码文件,只有一个文件,Main.java,使用文件输入,输入文件是in.txt,注意把该输入文件和Main.java放在同一个文件夹下,再用命令行编译运行即可。


import java.util.*;
import java.io.*;
import java.time.format.FormatStyle;

public class Main{
//仅仅为了操作方便,本程序就是用面向对象的语言写结构化程序,都是public级别

	public ArrayList<Integer> pageList;//页走向序列
	public ArrayList<Integer> pageTable;//这个是存储页的表
	public int size;//页框大小
	public ArrayList<String> pathList;//用于记录路径
	public int missingPage;
	public static final int MAX = 10000;

	public Main(){}

	public static void main(String[] args){
		new Main().run();
	}

	public void run(){
		this.input();//文件输入数据
		System.out.print("请输入页框大小:");
		Scanner scan = new Scanner(System.in);
		this.size = scan.nextInt();
		this.OPT();
		this.FIFO();
		this.LRU();
	}
	
	public void input(){
		pageList = new ArrayList<Integer>();
		pageTable = new ArrayList<Integer>();
		pathList = new ArrayList<String>();
		try{
			File file = new File("in.txt");
			BufferedReader br = new BufferedReader(new FileReader(file));
			String str = br.readLine(); 
			if(str!=null){
				String[] splits = str.split(" ");
				for(int i = 0; i < splits.length; i++){
					this.pageList.add(Integer.valueOf(splits[i]));
				}	
			}
			br.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void print(){
		
		System.out.println("--------------------------------------------------------------------");
		System.out.print("pages");
		for(int i = 0; i < pageList.size(); i++){
			System.out.print(" "+pageList.get(i));
		}
		System.out.println();
		for(int i = 0; i < size; i++){
			System.out.print("page"+(i+1));
			for(int j =0; j< pathList.size(); j++){
				System.out.print("\t"+pathList.get(j).charAt(i));
			}
			System.out.println();
		}
		System.out.print("     ");
		for(int j =0; j< pathList.size(); j++){
			System.out.print("\t"+pathList.get(j).charAt(pathList.get(j).length()-1));
		}
		System.out.println();
		System.out.println("共发生"+missingPage+"次缺页中断");
		System.out.println("--------------------------------------------------------------------");
		System.out.println();
	}

	public void OPT(){
		boolean missingPageMark = false;
		missingPage = 0;
		for(int i = 0; i < this.pageList.size(); i++){
			missingPageMark = false;
			if(pageTable.size() < this.size){//如果当前页框尚没有满
				if(pageTable.contains(pageList.get(i))==false){
					missingPageMark = true;
					pageTable.add(0, pageList.get(i));
					missingPage++;
				}else {
					
				}
				
			}else{
				if(pageTable.contains(pageList.get(i))){//页框中含有该页
					
				}else{//缺页中断
					missingPageMark=true;
					missingPage++;
					//搜索一下当前表中的每个页距离下次出现还有多少距离
					int[] distances = new int[size];
					for(int j = 0; j < size; j++){
						int tmpCnt = 1;
						boolean flag = false;//标志下是否能找到再次出现
						for(int k = i; k < pageList.size(); k++){
							if(pageList.get(k) != pageTable.get(j)){
								tmpCnt++;
							}else{
								flag =true;
								break;
							}
						}
						if(flag){
							distances[j] = tmpCnt;
						}else{
							distances[j] = MAX;
						}
					}
					int max = -1;
					int loc = -1;
					//找到最大距离的页的位置
					for(int kk = 0; kk < size; kk++){
						if(distances[kk] > max){
							max = distances[kk];
							loc = kk;
						}
					}
					//把该页替换掉即可
					pageTable.set(loc, pageTable.get(0));
					pageTable.set(0, pageList.get(i));
				}
			}
			//记录路径
			String pathTmp = "";
			for(int m=0; m<pageTable.size();m++){
				pathTmp += pageTable.get(m);
			}
			for(int m = pageTable.size(); m< size; m++){
				pathTmp += " ";
			}
			if(missingPageMark) {pathTmp+="X";}
			else {pathTmp+="√";}
			pathList.add(pathTmp);
		}
		System.out.println("OPT:");
		print();
		pageTable.clear();//清空页表
		pathList.clear();
	}
	
	public void FIFO(){
		missingPage=0;
		boolean missingPageMark = false;
		for(int i = 0; i < this.pageList.size(); i++){
			missingPageMark=false;
			
			if(pageTable.size() < this.size){//如果当前页框尚没有满
				if(pageTable.contains(pageList.get(i))==false){
					missingPageMark = true;
					pageTable.add(0, pageList.get(i));
					missingPage++;
				}else {
					if(pageTable.size()==1) {
						
					}else {
						pageTable.remove(pageList.get(i));
						pageTable.add(0, pageList.get(i));
					}
				}
			}else{
				if(pageTable.contains(pageList.get(i))){//页框中含有该页
					
				}else{//缺页中断
					missingPageMark=true;
					missingPage++;
					pageTable.add(0, pageList.get(i));	
					pageTable.remove(pageTable.size()-1);
				}
			}
			//记录路径
			String pathTmp = "";
			for(int m=0; m<pageTable.size();m++){
				pathTmp += pageTable.get(m);
			}
			for(int m = pageTable.size(); m< size; m++){
				pathTmp += " ";
			}
			if(missingPageMark) {pathTmp+="X";}
			else {pathTmp+="√";}
			pathList.add(pathTmp);
		}
		System.out.println("FIFO:");
		print();
		pageTable.clear();//清空页表
		pathList.clear();
	}
	
	public void LRU(){
		missingPage=0;
		boolean missingPageMark = false;
		for(int i = 0; i < this.pageList.size(); i++){
			missingPageMark=false;
			if(pageTable.size() < this.size){//如果当前页框尚没有满
				if(pageTable.contains(pageList.get(i))==false){
					missingPageMark = true;
					pageTable.add(0, pageList.get(i));
					missingPage++;
				}else {
					if(pageTable.size()==1) {
						
					}else {
						pageTable.remove(pageList.get(i));
						pageTable.add(0, pageList.get(i));
					}
				}
			}else{
				if(pageTable.contains(pageList.get(i))){//页框中含有该页
					pageTable.add(0, pageList.get(i));
					pageTable.remove(pageTable.size()-1);
				}else{//缺页中断
					missingPageMark=true;
					missingPage++;
					pageTable.add(0, pageList.get(i));	
					pageTable.remove(pageTable.size()-1);
				}
			}
			//记录路径
			String pathTmp = "";
			for(int m=0; m<pageTable.size();m++){
				pathTmp += pageTable.get(m);
			}
			for(int m = pageTable.size(); m< size; m++){
				pathTmp += " ";
			}
			if(missingPageMark) {pathTmp+="X";}
			else {pathTmp+="√";}
			pathList.add(pathTmp);
		}
		System.out.println("LRU:");
		print();
		pageTable.clear();//清空页表
		pathList.clear();
	}
	
}

输入文件in.txt的数据如下:
4 3 2 1 4 3 5 4 3 2 1 5

(操作系统实验)内存页面淘汰算法---FIFO、LRU、OPT_第1张图片

你可能感兴趣的:(操作系统)