模拟FCFS调度算法(先来先服务)没错,是篇好文章!
文章目錄
- 一、FCFS的介紹
- 二、代碼演示
- 三、代碼分析
- 1.使用節(jié)點(diǎn)模擬進(jìn)程
- 2.SimulateFCFS(核心模擬FCFS類(lèi))
- 3.創(chuàng)建一個(gè)節(jié)點(diǎn)為n的隊(duì)列(模擬就緒隊(duì)列)
- 4.核心計(jì)算分析
- 5.輸入到達(dá)時(shí)間和服務(wù)時(shí)間(模擬進(jìn)程到達(dá)和服務(wù))
- 6.出隊(duì)列(模擬完成所有進(jìn)程工作)
一、FCFS的介紹
先來(lái)先服務(wù)的調(diào)度算法:最簡(jiǎn)單的調(diào)度算法,既可以用于作業(yè)調(diào)度 ,也可以用于程序調(diào)度,當(dāng)作業(yè)調(diào)度中采用該算法時(shí),系統(tǒng)將按照作業(yè)到達(dá)的先后次序來(lái)進(jìn)行調(diào)度,優(yōu)先從后備隊(duì)列中,選擇一個(gè)或多個(gè)位于隊(duì)列頭部的作業(yè),把他們調(diào)入內(nèi)存,分配所需資源、創(chuàng)建進(jìn)程,然后放入“就緒隊(duì)列”,直到該進(jìn)程運(yùn)行到完成或發(fā)生某事件堵塞后,進(jìn)程調(diào)度程序才將處理機(jī)分配給其他進(jìn)程。
簡(jiǎn)單了說(shuō)就是如同名字 “先來(lái)先服務(wù)” ;
二、代碼演示
package com.zsh.blog;import java.util.Scanner;/*** @author:抱著魚(yú)睡覺(jué)的喵喵* @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.使用節(jié)點(diǎn)模擬進(jìn)程
因?yàn)樾枰?jì)算完成時(shí)間、周轉(zhuǎn)時(shí)間、帶權(quán)周轉(zhuǎn)時(shí)間,所以需要事先給出每個(gè)進(jìn)程到達(dá)時(shí)間和服務(wù)時(shí)間
模擬時(shí)至少需要以下幾個(gè)屬性(Queue類(lèi)對(duì)象模擬進(jìn)程)
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類(lèi))
以下分析核心模擬類(lèi)
3.創(chuàng)建一個(gè)節(jié)點(diǎn)為n的隊(duì)列(模擬就緒隊(duì)列)
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(); //創(chuàng)建節(jié)點(diǎn)數(shù)為n的隊(duì)列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);//這個(gè)自定義的函數(shù)主要用來(lái)輸入進(jìn)程的到達(dá)時(shí)間和服務(wù)時(shí)間calTime(arr); //該自定義函數(shù)用來(lái)計(jì)算完成時(shí)間、周轉(zhuǎn)時(shí)間、帶權(quán)周轉(zhuǎn)時(shí)間temp.next = arr;temp = arr; //進(jìn)行節(jié)點(diǎn)連接}this.tail = arr;System.out.println("Simulation allocation is successful!");}4.核心計(jì)算分析
(如果是第一個(gè)進(jìn)程) 完成時(shí)間 = 服務(wù)時(shí)間 + 到達(dá)時(shí)間
如果是n>1的進(jìn)程就要考慮前面進(jìn)程所花費(fèi)的時(shí)間和該進(jìn)程到達(dá)時(shí)間的長(zhǎng)短問(wèn)題。如果前面所花費(fèi)的完成時(shí)間大于該進(jìn)程的到達(dá)進(jìn)程,則(完成時(shí)間 = 服務(wù)時(shí)間+上一個(gè)進(jìn)程的完成時(shí)間)
反之則是 (完成時(shí)間= 服務(wù)時(shí)間+到達(dá)時(shí)間)
5.輸入到達(dá)時(shí)間和服務(wù)時(shí)間(模擬進(jìn)程到達(dá)和服務(wù))
public void keyBordInput(Queue arr, Scanner scanner) {arr.processName = scanner.next();arr.arrTime = scanner.nextInt();arr.serviceTime = scanner.nextInt();}6.出隊(duì)列(模擬完成所有進(jìn)程工作)
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;}}總結(jié)
以上是生活随笔為你收集整理的模拟FCFS调度算法(先来先服务)没错,是篇好文章!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: DTO数据传输对象详解
- 下一篇: springboot+maven实现模块