分页存储管理算法 Java 版

<!--more-->
package FIFO;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
class Page{
    private int pagenum = 0;  //页号
    private int flag = 0;    //标志   是否在页框中
    private int memory = 0;    //页框号
    private int modify = 0;     //是否修改,1为修改
    private int location = 0;
    public int getPagenum(){
        return pagenum;
    }
    public void setPagenum(int pagenum){
        this.pagenum = pagenum;
    }
    public int getFlag(){
        return flag;
    }
    public void setFlag(int flag){
        this.flag = flag;
    }
    public int getMemory(){
        return memory;
    }
    public void setMemory(int memory){
        this.memory = memory;
    }
    public int getModify(){
        return modify;
    }
    public void setModify(int modify){
        this.modify = modify;
    }
    public int getLocation(){
        //地址偏移量
        return location;
    }
    public void setLocation(int location){
        this.location = location;
    }
}
class  FIFO{
    private static final int PRO_MEMORY = 4;
    private static int countOldPoint;  //记录最久的页面下标
    private static int count=4; //记录当前在使用的总页面数
    private static int lackTime;  //缺页次数
    private static Page[] pages = new Page[PRO_MEMORY];
    private List<Integer> usePageNumList = new ArrayList<Integer>(); //动态数组,用来存放页号
    public void init(){
        for(int i = 0; i<pages.length;i++){
            pages[i] = new Page();
        }
    }
    public void input(int pagenum,int localtion,int order){
        if (order == 1){
            setMod(pagenum);
        }
        usePageNumList.add(pagenum);
        System.out.println("页面:"+pagenum);
        pages[count-1].setLocation(localtion);
    }
    public void running(){
        Iterator<Integer> it = usePageNumList.iterator();  //列表置换
        while (it.hasNext()){
            countOldPoint = countOldPoint % PRO_MEMORY;
            int inPageId = it.next();  //查找页表中是否存在
            if (search(inPageId)){
                System.out.println("页面"+inPageId+"已在内存中");
                System.out.println(inPageId*1024+searchLoc(inPageId));
            }else if (count < PRO_MEMORY){
                pages[count].setPagenum((Integer)inPageId);
                System.out.println("页面"+pages[count].getPagenum()+"进入内存");
                count ++;
            }else {
                System.out.println("*"+inPageId);
                int tmp = searchMod(inPageId);
                if (tmp == 1){
                    System.out.println("OOTj");
                }else {
                    System.out.println("INL");
                }
                replace(inPageId);
                lackTime++;
                countOldPoint++;
            }
            display();
        }
        System.out.println("缺页次数为:"+lackTime+",缺页率是:"+(float)lackTime/usePageNumList.size());
    }
    public boolean search(int pageId){     //搜索内存中是否存在
        for(int i = 0; i < pages.length; i++){
            if(pages[i].getPagenum() == pageId){
                return  true;
            }
        }
        return false;
    }
    public int searchLoc(int pageId){    //搜错地址偏移量
        for (int i = 0; i < pages.length; i++){
            if (pages[i].getPagenum() == pageId) {
                return pages[i].getLocation();
            }
        }
        return 0;
    }
    public void setMod(int pageId){
        for (int i = 0; i < pages.length; i++) {
            if (pages[i].getPagenum() == pageId) {
                pages[i].setModify(1);
            }
        }
    }
    public int searchMod(int pageId){
        for (int i = 0; i < pages.length; i++) {
            if (pages[i].getPagenum() == pageId) {
                return pages[i].getModify();
            }
        }
        return 0;
    }
    public void replace(int pageId){
        int outPageId = -1;
        outPageId = pages[countOldPoint].getPagenum();
        pages[countOldPoint].setPagenum(pageId);
        System.out.println("页面"+pageId+"进入内存,"+outPageId+"被置换出内存");
    }
    public void display(){
        System.out.print("当前内存的页面:");
        for(Page page:pages){
            System.out.print((page.getPagenum()+" "));
        }
        System.out.println();
    }
    public static void main(String args[]){
        FIFO fifo = new FIFO();
        Scanner sc = new Scanner(System.in);
        fifo.init();
        int c = 10;
        int pagenum,localtion,order;
        while (c>0) {
            System.out.println("请输入指令页号:");
            pagenum = sc.nextInt();
            System.out.println("请输入偏移地址:");
            localtion = sc.nextInt();
            System.out.println("请选择指令:1、存指令  2、非存指令");
            order = sc.nextInt();
            fifo.input(pagenum, localtion,order);
            fifo.running();
            c--;
        }
    }
}