页面置换策略:OPT、LRU、FIFO的JAVA代码

本文介绍了三种页面置换算法:最优置换(OPT)、先进先出(FIFO)和最近最久不用(LRU)。通过JAVA代码实现,用于计算不同页框数n(1到10)下的缺页数量,并分析了每种算法的缺页数量随页框数变化的曲线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

假设有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;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值