ラウンドロビンスケジューリングアルゴリズムのJavaバージョン



Round Robin Scheduling Algorithm Java Version



ラウンドロビンスケジューリングアルゴリズム

In the time-sharing system, the simplest and most commonly used is the round robin (RR) scheduling algorithm based on time slices. The algorithm uses a very fair processor allocation method, that is, each process on the ready queue runs only one time slice at a time. If there are n processes on the ready queue, each process can get approximately 1/n processor time each time.
  • 回転が基本原理です

    ラウンドロビン(RR)方式では、システムはすべての準備完了プロセスをFCFS戦略に従って準備完了キューに配置し、特定の時間間隔(30ミリ秒など)ごとに割り込みを生成し、システムのプロセススケジューラをアクティブ化するように設定できます。 、そしてスケジューリングが完了したらそれを完了し、CPUをキューの先頭に割り当てて実行します。プロセスのタイムスライスが使い果たされるか実行されると、システムはCPUをキューの先頭に再度割り当てます(または新しく到着した緊急プロセス)。その結果、レディキュー内のすべてのプロセスが一定期間内に1回CPUによって実行されることが保証されます。



アルゴリズムは次のとおりです。

処理する

package com.RouletteSchedulingAlgorithm /** * Process * @ClassName: Process */ public class Process { private int arriveTime//Time of arrival private int serverTime//Service time, can be modified private int finishTime//Complete time private int turnoverTime//Turnaround time private double quanTurnoverTime//Weighted turnaround time public int getRealServerTime() { return realServerTime } public void setRealServerTime(int realServerTime) { this.realServerTime = realServerTime } private int realServerTime //The real service time cannot be modified public Process() { } public Process(int arriveTime, int serverTime) { super() this.arriveTime = arriveTime this.serverTime = serverTime } public int getArriveTime() { return arriveTime } public void setArriveTime(int arriveTime) { this.arriveTime = arriveTime } public int getServerTime() { return serverTime } public void setServerTime(int serverTime) { this.serverTime = serverTime } public int getFinishTime() { return finishTime } public void setFinishTime(int finishTime) { this.finishTime = finishTime } public int getTurnoverTime() { return turnoverTime } public void setTurnoverTime(int turnoverTime) { this.turnoverTime = turnoverTime } public double getQuanTurnoverTime() { return quanTurnoverTime } public void setQuanTurnoverTime(double quanTurnoverTime) { this.quanTurnoverTime = quanTurnoverTime } }

特定の実装クラスRouletteSchedule



package com.RouletteSchedulingAlgorithm import java.text.DecimalFormat import java.util.LinkedList import java.util.List import java.util.Scanner /** * Round-robin scheduling algorithm * * @ClassName: RouletteSchedule */ public class RouletteSchedule { private static int finishTime = 0 public static void main(String[] args) { System.out.println('Please enter the time segment RR, an integer value') @SuppressWarnings('resource') Scanner s = new Scanner(System.in) int q = s.nextInt() // Time slice RR Process A = new Process(0,4) Process B = new Process(1,3) Process C = new Process(2,4) Process D = new Process(3,2) Process E = new Process(4,4) //Pass to the real service time A.setRealServerTime(A.getServerTime()) B.setRealServerTime(B.getServerTime()) C.setRealServerTime(C.getServerTime()) D.setRealServerTime(D.getServerTime()) E.setRealServerTime(E.getServerTime()) List list = new LinkedList() list.add(A) list.add(B) list.add(C) list.add(D) list.add(E) //Get and remove the head of the queue Process p while((p = (Process)((LinkedList) list).poll()) !=null ) { //The time of the process service is less than or equal to the time segment p if(p.getServerTime() <= q) { p.setFinishTime(finishTime + p.getServerTime()) finishTime += p.getServerTime() p.setTurnoverTime(p.getFinishTime() - p.getArriveTime()) p.setQuanTurnoverTime((double) p.getTurnoverTime() / (double)p.getRealServerTime()) }else if(p.getServerTime() > q){ //more than the p.setFinishTime(finishTime + q) finishTime += q p.setServerTime(p.getServerTime() - q) list.add(p)//Transplant the process to the end of the team } } print(A) print(B) print(C) print(D) print(E) //Print average turnaround time avgTurnoverTime(A,B,C,D,E) //Print weighted turnaround time avgQuanTurnoverTime(A,B,C,D,E) } /** * Print out the result * @Title: print * @return void */ //Format decimals, keep two decimal places static DecimalFormat df = new DecimalFormat('0.00') private static int i = 1 public static void print(Process p) { System.out.println(' '+(i++)+'Process completion time:'+p.getFinishTime()+' Turnaround time: '+ p.getTurnoverTime()+' Turnaround time with rights:'+df.format(p.getQuanTurnoverTime())) } /** * Average turnaround time */ public static void avgTurnoverTime(Process...process) { int i = 0 double sum = 0 for (Process p : process) { sum += p.getTurnoverTime() i++ } System.out.println('The average turnaround time is:'+sum/i) } /** * Average weighted turnaround time */ public static void avgQuanTurnoverTime(Process...process) { int i = 0 double sum = 0 for (Process p : process) { sum += p.getQuanTurnoverTime() i++ } System.out.println('The average weighted turnaround time is:'+df.format(sum/i)) } }