1. 程式人生 > >頁式儲存的FIFO演算法和LRU演算法

頁式儲存的FIFO演算法和LRU演算法

註釋都在程式碼裡面,寫的很垃圾,沒有優化,,,(用佇列實現的)程式碼行數有點多是因為自己寫了佇列沒有直接用演算法的jar包  發文章主要是為了持之以恆的徽章。

FIFO演算法

package sy4.zw;
import java.lang.Cloneable;
public class FIFO {
    static int fail = 0;
    static int [] pageindex = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
    static void Show_Status(int [] array,Queue q)throws CloneNotSupportedException{

        for(int i=0;i<array.length;i++){
            System.out.print("當前頁號:"+array[i]+"\t\t");
            if(!q.isFull()){                       //對列沒有滿時
                if(q.isExists(array[i])!=-1){          //頁號在記憶體中
                    System.out.print("hited\t");
                    q.show();
                    continue;
                }
                else{                              //頁號不在記憶體中
                    fail++;
                    q.push(array[i]);
                    System.out.print("miss\t");
                    q.show();
                }

            }
            else{
                if(q.isExists(array[i])!=-1){        //頁號在記憶體中
                    System.out.print("hited\t");
                    q.show();
                    continue;
                }
                else{
                    fail++;
                    q.pop();
                    q.move();
                    q.push(array[i]);
                    System.out.print("miss\t");
                    q.show();
                }
            }
        }
    }
    public static void main(String [] args)throws CloneNotSupportedException{
        Queue q = new Queue(3);
        Show_Status(pageindex,q);
        System.out.println("缺頁率:"+(float)fail/pageindex.length+" 缺頁次數:"+fail);
        System.out.println();
        fail = 0;
        Queue q1 = new Queue(4);
        Show_Status(pageindex,q1);
        System.out.println("缺頁率:"+(float)fail/pageindex.length+" 缺頁次數:"+fail);

    }

}
class Queue implements Cloneable{
    int [] ary;                                    //用陣列實現佇列
    int  prior,next;                               //prior指向隊首,next指向隊尾
    Queue(){
        this.ary = new int [10];                   //佇列長度預設為10
        this.prior=0;                              //初始化佇列指標
        this.next =0;
        for(int i=0;i<10;i++){
            this.ary[i] = -1;                      //因為頁號可能為0,所以置-1,
        }
    }
    Queue(int length){
        this.ary = new int [length];                                 //指定長度的佇列
        this.prior=0;
        this.next =0;
        for(int i=0;i<length;i++){
            this.ary[i] = -1;                                     //因為頁號可能為0,所以置-1,
        }
    }
    protected Queue clone()throws CloneNotSupportedException{      //物件深克隆
        Queue anoqueue = (Queue)super.clone();
        anoqueue.ary = this.ary.clone();
        return anoqueue;
    }

    public boolean isEmpty(){                                        //判斷佇列是否為空
        return this.next==this.prior;
    }

    public boolean isFull(){
        return this.next == this.ary.length;     //判斷佇列是否滿
    }

    public int isExists(int a){               //判斷a是否在佇列中
        int i = this.prior;
        int j = this.next;
        for(int k=i;k<j;k++){
            if(a==this.ary[k]) return k;
        }
        return -1;
    }

    public void push(int a){                     //將a加入到對列中
        if(isFull()) throw new IllegalArgumentException("佇列已滿");
        this.ary[this.next++]=a;
    }

    public int pop(){                           //刪除對列首元素
        if(isEmpty()) throw  new IllegalArgumentException("佇列為空");
        int e = this.ary[this.prior++];
        return e;
    }
    public void move()throws  CloneNotSupportedException{
        if(this.prior==0) throw new IllegalArgumentException("首元素還在對列") ;
        Queue anoqueue = this.clone();                                           //其實用陣列就可以了,想試試深克隆而已(有時間改回用陣列)
        int index = 0;
        for(int k=anoqueue.prior;k<anoqueue.next;k++){
            this.ary[index++] = anoqueue.ary[k];
        }
        anoqueue = null;                //垃圾回收
        this.next -=this.prior;
        this.prior =0;

    }
    public void LRU_Move(int k){                        //LRU中的移動方法
        int temp = this.ary[k];                         //儲存最近訪問的
        for(int i=k+1;i<this.next;i++){
            this.ary[i-1] = this.ary[i];                //k後的所有頁號前移一位
        }
        this.ary[this.next-1] = temp;                   //把最近出現的放到隊尾

    }

    public void show(){
        System.out.print("當前主存中的頁號:\t");
        for(int k=this.prior;k<=this.next-1;k++){
            System.out.print(this.ary[k]+" ");
        }
        System.out.println();
    }
}

LRU演算法

package sy4.zw;

public class LRU {
    static int fail = 0;
    static int [] pageindex = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
    static void Show_Status(int [] array,Queue q)throws CloneNotSupportedException{
        for(int i=0;i<array.length;i++){
            System.out.print("當前頁號:"+array[i]+"\t\t");
            if(!q.isFull()){                       //對列沒有滿時

                if(q.isExists(array[i])!=-1){          //頁號在記憶體中
                    q.LRU_Move(q.isExists(array[i]));
                    System.out.print("hited\t");
                    q.show();
                    continue;
                }
                else{                              //頁號不在記憶體中
                    fail++;
                    q.push(array[i]);
                    System.out.print("miss\t");
                    q.show();
                }

            }
            else{
                if(q.isExists(array[i])!=-1){        //頁號在記憶體中
                    q.LRU_Move(q.isExists(array[i]));
                    System.out.print("hited\t");
                    q.show();
                }
                else{
                    fail++;
                    q.pop();
                    q.move();
                    q.push(array[i]);
                    System.out.print("miss\t");
                    q.show();
                }
            }
        }
    }
    public static void main(String [] args)throws CloneNotSupportedException{
        Queue q = new Queue(3);
        Show_Status(pageindex,q);
        System.out.println("缺頁率:"+(float)fail/pageindex.length+" 缺頁次數:"+fail);
        System.out.println();
        fail = 0;
        Queue q1 = new Queue(4);
        Show_Status(pageindex,q1);
        System.out.println("缺頁率:"+(float)fail/pageindex.length+" 缺頁次數:"+fail);

    }

}