模拟FCFS调度算法(先来先服务)没错,是篇好文章!
生活随笔
收集整理的這篇文章主要介紹了
模拟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; //timeOfArrivalint serviceTime; //timeOfServiceint finishTime; //timeOfComplishint turnTime; //timeOfTurnarounddouble weightTurnTime; //timeOfWeightTurnaroundString processName; //process numberQueue 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;} }三、代碼分析
1.使用節點模擬進程
因為需要計算完成時間、周轉時間、帶權周轉時間,所以需要事先給出每個進程到達時間和服務時間
模擬時至少需要以下幾個屬性(Queue類對象模擬進程)
class Queue {int arrTime; //timeOfArrivalint serviceTime; //timeOfServiceint finishTime; //timeOfComplishint turnTime; //timeOfTurnarounddouble weightTurnTime; //timeOfWeightTurnaroundString processName; //process numberQueue next;public Queue(int arrTime, int serviceTime, String processName) {this.arrTime = arrTime;this.serviceTime = serviceTime;this.processName = processName;}public Queue() {} }2.SimulateFCFS(核心模擬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.核心計算分析
(如果是第一個進程) 完成時間 = 服務時間 + 到達時間
如果是n>1的進程就要考慮前面進程所花費的時間和該進程到達時間的長短問題。如果前面所花費的完成時間大于該進程的到達進程,則(完成時間 = 服務時間+上一個進程的完成時間)
反之則是 (完成時間= 服務時間+到達時間)
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;}}總結
以上是生活随笔為你收集整理的模拟FCFS调度算法(先来先服务)没错,是篇好文章!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DTO数据传输对象详解
- 下一篇: springboot+maven实现模块