1. 程式人生 > >頁面置換策略:OPT、LRU、FIFO的JAVA程式碼

頁面置換策略:OPT、LRU、FIFO的JAVA程式碼

問題

假設有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條線)

介紹

一,最優置換(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; } }