用java語言,模擬實現作業系統的程式排程演算法,先來先服務,高優先順序、高響應比、時間片輪轉和短作業

馮某r發表於2019-01-06

構思演算法的實現過程。

①先來先服務演算法
一開始我從最簡單的先來先服務開始想,既然要排序程式連結串列的執行順序,肯定要將連結串列的頭head作為引數傳入到方法中。其次為了之後方便一次性檢驗多種演算法,傳入進來的head連結串列我不會對它進行任何改變。所以建立一個新連結串列head2用CreateHead方法將連結串列進行復制,這樣我就可以拿head2這個複製品隨便搞,也不會影響之後呼叫其他演算法,因為head沒變。
當我拿到複製連結串列head2之後,要對他進行先來先服務演算法排序,肯定要比較所有程式的到達時間,從到達時間最小的程式先開始執行,需要注意的是將某個程式執行完畢之後,要將它從連結串列中移除,以確保下一次查詢的正確性。所以我用一個大迴圈,迴圈次數是連結串列的結點數,每一次迴圈都找到本次迴圈到達時間最小的那個結點,然後將這個結點的所有程式資訊進行更新,包括完成時間,週轉時間,帶權週轉時間,列印所有的資訊到控制檯。並且更新一下總時間線start(一開始為0,每次執行一個程式,它的值都加上這個程式的執行時間,相當於總時間線)。最後將這個結點從連結串列中刪除,再次迴圈從剩餘的節點中找到達時間最小的結點,直到所有結點都被刪除了。那麼所有結點的執行順序也已經全部被列印到控制檯上了。每次迴圈都記錄週轉時間和帶權週轉時間。最後迴圈結束計算一下平均值。下面是演算法流程圖:
在這裡插入圖片描述
②短作業優先演算法
短作業優先自然要選擇執行時間最短的程式先執行,和上面的演算法流程圖大體一致,就是要找出連結串列中執行時間最短的節點。並且要注意的是如果最短執行時間的程式的到達時間很遲,而有一個執行時間較長的程式先到達了,就不能讓CPU等著。應該先執行到達的程式,等到這個程式執行完畢之後,再次找執行時間最短的程式。直到程式退出。
③高優先順序優先演算法
高優先順序和前兩種也類似,主要是找到最高優先順序的程式先執行,而且也要和第二種演算法一樣注意程式的到達時間。
④高響應比優先演算法
此演算法和前三種稍有不同,雖然大體流程相同,但是響應比有自己的計算公式,響應比 = (等待時間+要求服務時間)/ 要求服務時間。
我封裝了一個方法專門求響應比,所以也可以和上面三種程式一樣先找到響應比最高的程式執行。需要注意的是,每一個程式執行完畢之後響應比都會發生變化。
⑤時間片輪轉演算法
這個演算法我想它大體是個先來先服務一樣的,所以我將所有程式一開始按到達時間排序,因為每次時間片執行完畢程式不一定結束,所以給程式的節點中新增兩個新屬性int newarrival和int newruntime用來儲存更新之後的到達時間和執行時間,每次時間片執行完畢,這個程式的到達時間就是此刻,然後將它的到達時間和連結串列中所有的程式的到達時間進行比較,插入它比它小和比它大的到達時間之間,如果遇到相等的到達時間就插入到本來存在節點的後面。然後本次時間片結束,頭指標向後移動執行下一個排隊的程式節點。

原始碼如下

import java.util.Scanner;
class Node{
    String name;
    int priority;
    int runtime;
    int arrivaltime;
    int starttime;
    int endtime;
    int turntime;  //週轉時間
    double dturntime;  //帶權週轉時間
    Node nextnode;
    int statu;
    int newarrival;
    int newruntime;

    public Node(String name,int priority,int runtime,int arrivaltime, int starttime, int endtime, int turntime, double dturntime){
        this.name = name;
        this.priority = priority;
        this.runtime = runtime;
        this.arrivaltime = arrivaltime;
        this.nextnode = null;
        this.starttime = starttime;
        this.endtime = endtime;
        this.turntime = turntime;
        this.dturntime = dturntime;
        this.newarrival = arrivaltime;
        this.newruntime = runtime;
    }

}

class Create{
    public Node createNode(Node head, String name, int priority, int runtime, int arrivaltime, int starttime, int endtime, int turntime, double dturntime){

        if(head == null){
            Node node = new Node(name,priority,runtime,arrivaltime,starttime,endtime,turntime,dturntime);
            head = node;
            return head;
        }
        Node cur = head;
        while(cur.nextnode!=null){
            cur = cur.nextnode;
        }
        Node node = new Node(name,priority,runtime,arrivaltime,starttime,endtime,turntime,dturntime);
        cur.nextnode = node;
        return head;
    }

    public void check(Node head){
        if(head == null){
            System.out.println("當前沒有節點資訊");
            return;
        }
        Node cur = head;
        while(cur!=null){
            System.out.println("名字:"+cur.name+"、優先順序:"+cur.priority+"、執行時間:"+cur.runtime+"、到達時間"+cur.arrivaltime);
            cur = cur.nextnode;
        }
    }
}

class Algorithm{
    private Node pre = null;
    private Node prev = null;
    private Node min = null;
    private int num = 0;
    private int start = 0;
    private double nums = 0.0;
    private int count = 0;
    private static Create create = new Create();
    private static Node CreateHead(Node head){
        Node head2 = null;
        Node cur = head;
        while(cur!=null){
            head2 = create.createNode(head2,cur.name,cur.priority,cur.runtime,cur.arrivaltime,cur.starttime,cur.endtime,cur.turntime,cur.dturntime);
            cur = cur.nextnode;
        }
        return head2;
    }

    private void endFun(){
        System.out.println("平均週轉時間:" + (double) this.num / this.count + "平均帶權週轉時間:" + this.nums / this.count);
        this.start = 0;
        this.num = 0;
        this.nums = 0;
        this.count = 0;
    }

    private static Node toolMethod(Node min,Node prev,int start,Node head){
        min.starttime = start;
        min.endtime = min.starttime + min.runtime;
        min.turntime = min.endtime - min.arrivaltime;
        min.dturntime = (double) min.turntime / (double) min.runtime;
        System.out.println("名字:" + min.name + "、優先順序:" + min.priority + "、執行時間:" + min.runtime + "、到達時間" + min.arrivaltime + "、開始時間:" + min.starttime + "、結束時間:" + min.endtime + "、週轉時間:" + min.turntime + "、帶權週轉時間:" + min.dturntime);
        if (prev == null) {
            if (min.nextnode == null) {
                return null;
            }
            return min.nextnode;
        } else {
            prev.nextnode = min.nextnode;
        }
        return head;
    }

    private static Node findMin(Node head){
        Node cur = head;
        Node real = null;
        int mintime = cur.arrivaltime;
        while(cur!=null){
            if(cur.arrivaltime<=mintime){
                mintime = cur.arrivaltime;
                real = cur;
            }
            cur = cur.nextnode;
        }
        return real;
    }

    public void Fcfs(Node head) {
        Node head2 = CreateHead(head);
        while (head2 != null) {
            min = null;
            pre = null;
            Node cur = head2;
            int mintime = cur.arrivaltime;
            while (cur != null) {
                if (cur.arrivaltime <= mintime) {
                    mintime = cur.arrivaltime;
                    prev = pre;
                    min = cur;
                }
                pre = cur;
                cur = cur.nextnode;
            }
            if (min.arrivaltime > start) {
                start = min.arrivaltime;
            }
            head2 = toolMethod(min,prev,start,head2);
            start = start + min.runtime;
            num += min.turntime;
            nums += min.dturntime;
            count++;
        }
        this.endFun();
    }

    public void Priority(Node head){
        Node head2 = CreateHead(head);
        while(head2!=null){
            min = null;
            pre = null;
            Node cur = head2;
            int mintime = 0;
            while(cur!=null){
                if(cur.priority >= mintime && cur.arrivaltime <= start){
                    mintime = cur.priority;
                    prev = pre;
                    min = cur;
                }
                pre = cur;
                cur = cur.nextnode;
            }
            if(min == null){
                min = findMin(head2);
            }
            if(min.arrivaltime > start){
                start = min.arrivaltime;
            }
            head2 = toolMethod(min,prev,start,head2);
            start = start + min.runtime;
            num += min.turntime;
            nums += min.dturntime;
            count++;
        }
        this.endFun();
    }

    public void ShortProcess(Node head){
        Node head2 = CreateHead(head);
        while(head2!=null){
            min = null;
            pre = null;
            Node cur = head2;
            int mintime = 1000;
            while(cur!=null){
                if(cur.runtime <= mintime && cur.arrivaltime <= start){
                    mintime = cur.runtime;
                    prev = pre;
                    min = cur;
                }
                pre = cur;
                cur = cur.nextnode;
            }
            if(min == null){
                min = findMin(head2);
            }
            if(min.arrivaltime > start){
                start = min.arrivaltime;
            }
            head2 = toolMethod(min,prev,start,head2);
            start = start + min.runtime;
            num += min.turntime;
            nums += min.dturntime;
            count++;
        }
        this.endFun();
    }

    private static double resRatio(Node node,int start){
        int waittime = start - node.arrivaltime;
        return (double)waittime/node.runtime;
    }
    public void Hreponse(Node head){
        Node head2 = CreateHead(head);
        while(head2!=null){
            min = null;
            pre = null;
            Node cur = head2;
            double mintime = 0.0;
            while(cur!=null){
                double resratio = resRatio(cur,start);
                if(resratio >= mintime && cur.arrivaltime <= start){
                    mintime = resratio;
                    prev = pre;
                    min = cur;
                }
                pre = cur;
                cur = cur.nextnode;
            }
            if(min == null){
                min = findMin(head2);
            }
            if(min.arrivaltime > start){
                start = min.arrivaltime;
            }
            head2 = toolMethod(min,prev,start,head2);
            start = start + min.runtime;
            num += min.turntime;
            nums += min.dturntime;
            count++;
        }
        this.endFun();
    }

    public static Node QueueHead(Node head){
        Node cur = head;
        Node nodemin = null;
        Node head2 = null;
        int min = 1000;
        int count = 0;
        while(cur!=null){
            count++;
            cur = cur.nextnode;
        }
        while(count!=0) {
            min = 1000;
            cur = head;
            while (cur != null) {
                if (cur.arrivaltime < min && cur.statu == 0) {
                    nodemin = cur;
                    min = cur.arrivaltime;
                }
                cur = cur.nextnode;
            }
            nodemin.statu = 1;
            count--;
            head2 = create.createNode(head2,nodemin.name,nodemin.priority,nodemin.runtime,nodemin.arrivaltime,nodemin.starttime,nodemin.endtime,nodemin.turntime,nodemin.dturntime);
        }
        return head2;
    }

    public static void insert(Node head,Node min){
        Node cur = head;
        Node pre = null;
        while(cur!=null){
            if(cur.arrivaltime > min.newarrival){
                pre.nextnode = min;
                min.nextnode = cur;
                return;
            }
            pre = cur;
            cur = cur.nextnode;
        }
        pre.nextnode = min;
        min.nextnode = cur;
    }

    public void Roundrobin(Node head,int Rtime){
        Node newnode = null;
        Node head2 = QueueHead(head);
        create.check(head2);
        System.out.println(head2.newruntime);
        System.out.println(head2.newarrival);
        while(head2!=null){
            min = head2;
            if(min.arrivaltime > start){
                start = min.arrivaltime;
            }
            if(min.newruntime > Rtime){
                min.newruntime -= Rtime;
                start += Rtime;
                min.newarrival += Rtime;
                newnode = new Node(min.name,min.priority,min.runtime,min.arrivaltime,min.starttime,min.endtime,min.turntime,min.dturntime);
                newnode.newarrival = min.newarrival;
                newnode.newruntime = min.newruntime;
                insert(head2,newnode);
                head2 = min.nextnode;
            }else{
                start += min.newruntime;
                min.endtime = start;
                min.turntime = min.endtime - min.arrivaltime;
                min.dturntime = (double) min.turntime / (double) min.runtime;
                head2 = min.nextnode;
                num += min.turntime;
                nums += min.dturntime;
                count++;
                System.out.println("名字:" + min.name + "、優先順序:" + min.priority + "、執行時間:" + min.runtime + "、到達時間" + min.arrivaltime + "、開始時間:" + min.starttime + "、結束時間:" + min.endtime + "、週轉時間:" + min.turntime + "、帶權週轉時間:" + min.dturntime);
            }
        }
        this.endFun();
    }
}

public class Test {
    public static void dispatchMenu(Node head){

        Scanner sc = new Scanner(System.in);
        Algorithm al = new Algorithm();
        int count = 1;
        while(count == 1){
            System.out.println("請選擇排程演算法:");
            System.out.println("1.先來先服務演算法");
            System.out.println("2.短作業優先演算法");
            System.out.println("3.高優先順序優先演算法");
            System.out.println("4.高響應比優先演算法");
            System.out.println("5.時間片輪轉演算法");
            System.out.println("0.退出");
            int num = sc.nextInt();
            switch(num){
                case 1:al.Fcfs(head);
                    break;
                case 2:al.ShortProcess(head);
                    break;
                case 3:al.Priority(head);
                    break;
                case 4:al.Hreponse(head);
                    break;
                case 5:al.Roundrobin(head,1);
                    break;
                case 0:count = 0;
                    break;
                default:System.out.println("輸入錯誤請重新輸入");
            }
        }
    }

    public static void mainMenu(){
        Create create = new Create();
        Node head = null;

        Scanner sc = new Scanner(System.in);

        int count1 = 1;
        while(count1 == 1){
            System.out.println("請選擇你需要的服務:");
            System.out.println("1.新增新程式");
            System.out.println("2.使用排程演算法進行排序");
            System.out.println("3.檢視當前程式資訊");
            System.out.println("0.退出");
            int num = sc.nextInt();
            switch(num){
                case 1:
                    String name;
                    int priority;
                    int runtime;
                    int arrivaltime;
                    System.out.println("請輸入程式名字:");
                    name = sc.next();
                    System.out.println("請輸入程式優先順序:");
                    priority = sc.nextInt();
                    System.out.println("請輸入進行執行時間:");
                    runtime = sc.nextInt();
                    System.out.println("請輸入程式到達時間:");
                    arrivaltime = sc.nextInt();
                    head = create.createNode(head,name,priority, runtime,arrivaltime,0,0,0,0);
                    break;
                case 2:Test.dispatchMenu(head);
                    break;
                case 3:create.check(head);
                    break;
                case 0:count1 = 0;
                    break;
                default:System.out.println("輸入錯誤請重新輸入");
            }
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test.mainMenu();
    }
}

相關文章