页面调度算法——FIFO

来源:互联网 发布:贝因美积分网络兑换 编辑:程序博客网 时间:2024/06/05 21:40
在计算机中,页式虚拟存储器实现的一个难点是设计页面调度(置换)算法。其中一种实现方式是FIFO算法。
FIFO算法根据页面进入内存的时间先后选择淘汰页面,先进入内存的页面先淘汰,后进入内存的后淘汰。
假设Cache的大小为2,有5个页面请求,分别为 2 1 2 3 1,则Cache的状态转换为:(2)->(2,1)->(2,1)->(1,3)->(1,3),其中第1,2,4次缺页,总缺页次数为3。

现在给出Cache的大小n和m个页面请求,请算出缺页数。

package demo;import java.util.ArrayList;import java.util.Scanner;public class Demo0615_2 {public static void main(String[] args) { Scanner s=new Scanner(System.in);        while(s.hasNext()){            int space=s.nextInt();            int n=s.nextInt();            ArrayList<Integer> list=new ArrayList<Integer>();            int res=0;            for(int i=0;i<n;i++){                int temp=s.nextInt();                if(list.contains(temp))                    continue;                else{                    res++;                    list.add(temp);                }                if(list.size()>space)                    list.remove(0);            }            System.out.println(res);        }}}


0 0