天天看點

模拟FCFS排程算法(先來先服務)沒錯,是篇好文章!

文章目錄

  • ​​一、FCFS的介紹​​
  • ​​二、代碼示範​​
  • ​​三、代碼分析​​
  • ​​1.使用節點模拟程序​​
  • ​​2.SimulateFCFS(核心模拟FCFS類)​​
  • ​​3.建立一個節點為n的隊列(模拟就緒隊列)​​
  • ​​4.核心計算分析​​
  • ​​5.輸入到達時間和服務時間(模拟程序到達和服務)​​
  • ​​6.出隊列(模拟完成所有程序工作)​​

一、FCFS的介紹

先來先服務的排程算法:最簡單的排程算法,既可以用于作業排程 ,也可以用于程式排程,當作業排程中采用該算法時,系統将按照作業到達的先後次序來進行排程,優先從後備隊列中,選擇一個或多個位于隊列頭部的作業,把他們調入記憶體,配置設定所需資源、建立程序,然後放入“就緒隊列”,直到該程序運作到完成或發生某事件堵塞後,程序排程程式才将處理機配置設定給其他程序。

簡單了說就是如同名字 “先來先服務” ;

二、代碼示範

package com.zsh.blog;

import java.util.Scanner;

/**
 * @author:抱着魚睡覺的喵喵
 * @date:2021/3/19
 * @description:
 */
public class SimulateSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        SimulateFCFS simulateFCFS = new SimulateFCFS();
        boolean flag = true;
        char at = ' ';
        System.out.println("a:Simulate multiple processes to form a queue");
        System.out.println("b:Assign a process to the queue");
        System.out.println("d:Complete all process work");
        System.out.println("e:Exit the simulated system");
        while (flag) {
            System.out.println("Please enter your instructions:");
            at = scanner.next().charAt(0);
            switch (at) {
                case 'a':
                    simulateFCFS.createQueue();
                    break;
                case 'b':
                    simulateFCFS.assignProcess();
                    break;
                case 'd':
                    simulateFCFS.finishAllProcessTask();
                    return;
                case 'e':
                    System.out.println("Simulated is end~");
                    return;
                default:
                    System.out.println("Your input is wrong, please re-enter!");
                    break;
            }
        }
    }

}

class Queue {
    int arrTime;            //timeOfArrival
    int serviceTime;        //timeOfService
    int finishTime;         //timeOfComplish
    int turnTime;           //timeOfTurnaround
    double weightTurnTime;     //timeOfWeightTurnaround
    String processName;      //process number
    Queue next;

    public Queue(int arrTime, int serviceTime, String processName) {
        this.arrTime = arrTime;
        this.serviceTime = serviceTime;
        this.processName = processName;
    }

    public Queue() {
    }
}

/**
 * Simulate FCFS algorithm class
 */
class SimulateFCFS {
    private Queue head = new Queue(-1, -1, null);
    private int timer = 0;
    private Queue tail = head;

    public void createQueue() {
        Queue arr = null;
        Queue temp = head;
        Scanner scanner = new Scanner(System.in);
        System.out.printf("Please enter the number of process tasks to initialize the simulation:");
        int n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            System.out.printf("Please enter the process number, start time, and service time of the %d process:",i);
            arr = new Queue();
            keyBordInput(arr, scanner);
            calTime(arr);
            temp.next = arr;
            temp = arr;

        }
        this.tail = arr;
        System.out.println("Simulation allocation is successful!");
    }

    /**
     * Completion time of calculation process - Turnaround time - Weighted turnaround time
     * @param arr
     */
    public void calTime(Queue arr) {
        Queue temp = arr;
        if (this.timer < temp.arrTime) {
            this.timer = arr.arrTime;
        } else {
            if (timer == 0) {
                this.timer = temp.arrTime;
            }
        }
        temp.finishTime = temp.serviceTime + this.timer;
        temp.turnTime = temp.finishTime - temp.arrTime;
        temp.weightTurnTime = (temp.turnTime * 1.0) / (temp.serviceTime * 1.0);
        this.timer += temp.serviceTime;
    }
    /**
     * Process number,arrival time,service time entered from the keyboard
     * @param arr
     * @param scanner
     */
    public void keyBordInput(Queue arr, Scanner scanner) {
        arr.processName = scanner.next();
        arr.arrTime = scanner.nextInt();
        arr.serviceTime = scanner.nextInt();
    }

    /**
     * Assign a process to the queue
     */
    public void assignProcess() {
        Queue newProcess = new Queue();
        Scanner scanner = new Scanner(System.in);
        System.out.printf("Please enter the add process number,start time,and service time of the process:");
        keyBordInput(newProcess, scanner);
        calTime(newProcess);
        this.tail.next = newProcess;
        this.tail = newProcess;
    }

    /**
     * Complish a task of process from the queue
     */
//    public void finishProcessTask() {
//        Queue workingProcess = head;
//
//    }
    /**
     * Complish all task of process from the queue
     */
    public void finishAllProcessTask() {
        if (isEmpty()) {
            return;
        }
        Queue cur = head.next;
        System.out.println("Process number========Arrive time======Service time=======finish Time=======Turn time======WeightTurn time");
        while (true) {
            System.out.printf("\t\t%s\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t%d\t\t\t\t%f",cur.processName,cur.arrTime,cur.serviceTime,cur.finishTime,cur.turnTime,cur.weightTurnTime);
            System.out.println();
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
    }

    public boolean isEmpty() {
        if (head.next == null) {
            System.out.println("Queue is null!");
            return true;
        }
        return false;
    }
}      
模拟FCFS排程算法(先來先服務)沒錯,是篇好文章!

三、代碼分析

1.使用節點模拟程序

因為需要計算完成時間、周轉時間、帶權周轉時間,是以需要事先給出每個程序到達時間和服務時間

模拟時至少需要以下幾個屬性(Queue類對象模拟程序)

class Queue {
    int arrTime;            //timeOfArrival
    int serviceTime;        //timeOfService
    int finishTime;         //timeOfComplish
    int turnTime;           //timeOfTurnaround
    double weightTurnTime;     //timeOfWeightTurnaround
    String processName;      //process number
    Queue next;

    public Queue(int arrTime, int serviceTime, String processName) {
        this.arrTime = arrTime;
        this.serviceTime = serviceTime;
        this.processName = processName;
    }

    public Queue() {
    }
}      

2.SimulateFCFS(核心模拟FCFS類)

模拟FCFS排程算法(先來先服務)沒錯,是篇好文章!

3.建立一個節點為n的隊列(模拟就緒隊列)

public void createQueue() {
        Queue arr = null;
        Queue temp = head;    
        Scanner scanner = new Scanner(System.in);
        System.out.printf("Please enter the number of process tasks to initialize the simulation:");
        int n = scanner.nextInt();    //建立節點數為n的隊列
        for (int i = 1; i <= n; i++) {
            System.out.printf("Please enter the process number, start time, and service time of the %d process:",i);
            arr = new Queue();
            keyBordInput(arr, scanner);//這個自定義的函數主要用來輸入程序的到達時間和服務時間
            calTime(arr); //該自定義函數用來計算完成時間、周轉時間、帶權周轉時間
            temp.next = arr;
            temp = arr;        //進行節點連接配接

        }
        this.tail = arr;
        System.out.println("Simulation allocation is successful!");
    }      

4.核心計算分析

//timer是全局變量,用來計算完成時間(解決上面的問題)
public void calTime(Queue arr) {
        Queue temp = arr;
        if (this.timer < temp.arrTime) {
            this.timer = arr.arrTime;
        } else {
            if (timer == 0) {
                this.timer = temp.arrTime;
            }
        }
        temp.finishTime = temp.serviceTime + this.timer;
        temp.turnTime = temp.finishTime - temp.arrTime;
        temp.weightTurnTime = (temp.turnTime * 1.0) / (temp.serviceTime * 1.0);
        this.timer += temp.serviceTime;
    }      

5.輸入到達時間和服務時間(模拟程序到達和服務)

public void keyBordInput(Queue arr, Scanner scanner) {
        arr.processName = scanner.next();
        arr.arrTime = scanner.nextInt();
        arr.serviceTime = scanner.nextInt();
    }      

6.出隊列(模拟完成所有程序工作)

public void finishAllProcessTask() {
        if (isEmpty()) {
            return;
        }
        Queue cur = head.next;
        System.out.println("Process number========Arrive time======Service time=======finish Time=======Turn time======WeightTurn time");
        while (true) {
            System.out.printf("\t\t%s\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t\t%d\t\t\t\t%d\t\t\t\t%f",cur.processName,cur.arrTime,cur.serviceTime,cur.finishTime,cur.turnTime,cur.weightTurnTime);
            System.out.println();
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
    }      

繼續閱讀