進程調度rr算法java實現_Java實現進程調度算法(二) RR(時間片輪轉)

一、概述

因為這次os作業對用戶在控制臺的輸入輸出有要求,所以我花了挺多的代碼來完善控制臺的顯示。

也因為我這次要實現多個類似算法,所以將一些共性單獨提取出來作為一個類。

如果只想要和算法有關的核心代碼,看RR類的calc()即可。

實現思路:每運行一個進程,則將所有進程的remainServiceTime減去一個時間片的長度。

二、運行結果

1. 測試數據:

d3432967340c86fa21e4f27cfed6d710.png

2. 運行結果:

399cb03406a9fd1ffb8430275cb5e3ab.png

21fa4223ce78ed53e42b5532a42c13cc.png

三、流程圖

b62340665ef15a0f2df34ca77df97bb3.png

四、實現代碼

1. RR類(主類)

只有calc()中涉及了算法,init()和printResult()只有簡單的輸入輸出操作。

1 packagexqy.algorithm;2

3 import java.util.*;4

5 importxqy.Util.Tools;6 importxqy.been.Process;7

8 /**

9 *@authorxqy10 * @date 2018年12月19日19:14:4911 */

12 public classRR {13 private intprocessNumber;14 private ArrayListprocessList;15 private inttimeSlice;16

17 publicRR() {18 init();19 calc();20 Tools.printResult(processList);21 }22

23 private voidinit() {24 Scanner sc = newScanner(System.in);25

26 System.out.print(" Please enter the slice time:");27 timeSlice =sc.nextInt();28 System.out.print(" please enter the process num:");29 processNumber =sc.nextInt();30

31 processList = new ArrayList();32 for (int i = 0; i < processNumber; i++) {33 processList.add(newProcess());34 }35

36 System.out.println(" Please enter each process arrival time:");37 for (int i = 0; i < processNumber; i++) {38 System.out.print(" Process" + (i + 1) + ":");39 processList.get(i).setArrivalTime(sc.nextInt());40 }41

42 System.out.println(" Please enter each process service time:");43 for (int i = 0; i < processNumber; i++) {44 System.out.print(" Process" + (i + 1) + ":");45 int servicesTime =sc.nextInt();46

47 processList.get(i).setServicesTime(servicesTime);48 processList.get(i).setRemainServiceTime(servicesTime);49 }50 }51

52 private voidcalc() {53 int timeNow = 0;54 int processRemain =processNumber;55 booleannoProcessRunInThisTurn;56 Process opProcess;57

58 while (processRemain != 0) {59 noProcessRunInThisTurn = true;60

61 for (int i = 0; i < processNumber; i++) {62 opProcess =processList.get(i);63

64 if ((opProcess.getRemainServiceTime() > 0)65 && (timeNow >=opProcess.getArrivalTime())) {66 //First time

67 if (opProcess.getServicesTime() ==opProcess68 .getRemainServiceTime()) {69 int waitTime = timeNow -opProcess.getArrivalTime();70

71 opProcess.setStartTime(timeNow);72 opProcess.setWaitTime(waitTime);73 }74

75 //Calculating remain service time

76 int remainServiceTime =opProcess.getRemainServiceTime()77 -timeSlice;78 opProcess.setRemainServiceTime(remainServiceTime);79

80 //Last time

81 if (remainServiceTime <= 0) {82 int completionTime = timeNow + timeSlice; //The process ends when the current slice is completed.

83 int turnAroundTime =completionTime84 -opProcess.getArrivalTime();85 double turnAroundTimeWithWeight = 1.0 *turnAroundTime86 /opProcess.getServicesTime();87

88 opProcess.setCompletionTime(completionTime);89 opProcess.setTurnAroundTime(turnAroundTime);90 opProcess91 .setTurnAroundTimeWithWeight(turnAroundTimeWithWeight);92 processRemain--;93 }94

95 timeNow +=timeSlice;96 noProcessRunInThisTurn = false;97

98 System.out.println(" #STEP# Process" + (i + 1)99 + " remain service time:"

100 +opProcess.getRemainServiceTime()101 + " , timeBefore:" + (timeNow - 1) + ", timeNow:"

102 +timeNow103 + ((remainServiceTime <= 0) ? " Finish" : ""));104 } else{105 //do noting, because this process has been completed or hasn`t arrived.

106 }107 }108

109 //Means no process could run, because they have arrived.

110 if ((processRemain > 0) &&noProcessRunInThisTurn) {111 timeNow +=timeSlice;112 }113 }114 }115 }

2. Process類

模擬了進程,對屬性進行了封裝。

1 package xqy.been;

2

3 public class Process {

4 private int arrivalTime;

5 private int servicesTime;

6 private int remainServiceTime;

7 private int startTime;

8 private int waitTime;

9 private int completionTime;

10

11 /**

12 * turnAroundTime = completionTime - arrivalTime

13 */

14 private int turnAroundTime;

15

16 /**

17 * turnAroundTimeWithWeight = turnAroundTime / servicesTime

18 */

19 private double turnAroundTimeWithWeight;

20

21 public Process() {

22 ;

23 }

24

25 public int getArrivalTime() {

26 return arrivalTime;

27 }

28

29 public void setArrivalTime(int arrivalTime) {

30 this.arrivalTime = arrivalTime;

31 }

32

33 public int getServicesTime() {

34 return servicesTime;

35 }

36

37 public void setServicesTime(int servicesTime) {

38 this.servicesTime = servicesTime;

39 }

40

41 public int getRemainServiceTime() {

42 return remainServiceTime;

43 }

44

45 public void setRemainServiceTime(int remainServiceTime) {

46 this.remainServiceTime = remainServiceTime;

47 }

48

49 public int getStartTime() {

50 return startTime;

51 }

52

53 public void setStartTime(int startTime) {

54 this.startTime = startTime;

55 }

56

57 public int getWaitTime() {

58 return waitTime;

59 }

60

61 public void setWaitTime(int waitTime) {

62 this.waitTime = waitTime;

63 }

64

65 public int getCompletionTime() {

66 return completionTime;

67 }

68

69 public void setCompletionTime(int completionTime) {

70 this.completionTime = completionTime;

71 }

72

73 public int getTurnAroundTime() {

74 return turnAroundTime;

75 }

76

77 public void setTurnAroundTime(int turnAroundTime) {

78 this.turnAroundTime = turnAroundTime;

79 }

80

81 public double getTurnAroundTimeWithWeight() {

82 return turnAroundTimeWithWeight;

83 }

84

85 public void setTurnAroundTimeWithWeight(double turnAroundTimeWithWeight) {

86 this.turnAroundTimeWithWeight = turnAroundTimeWithWeight;

87 }

88

89 @Override

90 public String toString() {

91 return "Process [arrivalTime=" + arrivalTime + ", servicesTime="

92 + servicesTime + ", remainServiceTime=" + remainServiceTime

93 + ", startTime=" + startTime + ", waitTime=" + waitTime

94 + ", completionTime=" + completionTime + ", turnAroundTime="

95 + turnAroundTime + ", turnAroundTimeWithWeight="

96 + turnAroundTimeWithWeight + "]";

97 }

98 }

3. Tools類

因為這次要實現幾個類似的算法,所以我把每個算法中都要用到的方法都提取出來作為單獨的工具類。

也可以將這些工具方法都放入FCFS類中。

1 package xqy.Util;

2

3 import java.util.ArrayList;

4

5 import xqy.been.Process;

6

7 public class Tools {

8

9 public static double calcAverageTurnAroundTime(

10 ArrayList processList) {

11 double sum = 0;

12 for (int i = 0; i < processList.size(); i++) {

13 sum += processList.get(i).getTurnAroundTime();

14 }

15 return Math.round(sum / processList.size() * 100) / 100.0;

16 }

17

18 public static double calcAverageTurnAroundTimeWithWeight(

19 ArrayList processList) {

20 double sum = 0;

21 for (int i = 0; i < processList.size(); i++) {

22 sum += processList.get(i).getTurnAroundTimeWithWeight();

23 }

24 return Math.round(sum / processList.size() * 100) / 100.0;

25 }

26

27 public static void printResult(ArrayList processList) {

28 System.out.println("n #RESULT#");

29

30 System.out.print("tArrive:tt");

31 for (int i = 0; i < processList.size(); i++) {

32 System.out.print(processList.get(i).getArrivalTime() + "t");

33 }

34 System.out.println();

35

36 System.out.print("tService:t");

37 for (int i = 0; i < processList.size(); i++) {

38 System.out.print(processList.get(i).getServicesTime() + "t");

39 }

40 System.out.println();

41

42 System.out.print("tStart:tt");

43 for (int i = 0; i < processList.size(); i++) {

44 System.out.print(processList.get(i).getStartTime() + "t");

45 }

46 System.out.println();

47

48 System.out.print("tWait:tt");

49 for (int i = 0; i < processList.size(); i++) {

50 System.out.print(processList.get(i).getWaitTime() + "t");

51 }

52 System.out.println();

53

54 System.out.print("tFinish:tt");

55 for (int i = 0; i < processList.size(); i++) {

56 System.out.print(processList.get(i).getCompletionTime() + "t");

57 }

58 System.out.println();

59

60 System.out.print("tTurn around:t");

61 for (int i = 0; i < processList.size(); i++) {

62 System.out.print(processList.get(i).getTurnAroundTime() + "t");

63 }

64 System.out.println();

65

66 System.out.print("tTA wight:t");

67 for (int i = 0; i < processList.size(); i++) {

68 System.out.print(Math.round(processList.get(i)

69 .getTurnAroundTimeWithWeight() * 100) / 100.0 + "t");

70 }

71 System.out.println();

72

73 System.out.println("tAverage turn around time:"

74 + Tools.calcAverageTurnAroundTime(processList) + "t");

75 System.out.println("tAverage turn around time with wight:"

76 + Tools.calcAverageTurnAroundTimeWithWeight(processList));

77

78 System.out.println();

79 }

80 }

內容來源于網絡如有侵權請私信刪除

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/534851.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/534851.shtml
英文地址,請注明出處:http://en.pswp.cn/news/534851.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python全局變量global線程安全_對python多線程與global變量詳解

今天早上起來寫爬蟲&#xff0c;基本框架已經搭好&#xff0c;添加多線程爬取功能時&#xff0c;發現出錯&#xff1a;比如在下載文件的url列表中加入200個url&#xff0c;開啟50個線程。我的爬蟲…竟然將50個url爬取并全部命名為0.html&#xff0c;也就是說&#xff0c;最后的…

python123第五章_python 3.5學習筆記(第五章)

本章內容1、什么是模塊2、模塊的導入方法3、搜索路徑4、重要標準庫一、什么是模塊1、模塊本質上是一個以.py 結尾的python文件&#xff0c;包含了python對象定義和python語句。2、模塊是用來從邏輯上組織python代碼(定義變量、函數、類、邏輯等)以實現某種功能3、包&#xff1a…

string 長度_String源碼解析

本章源碼分析基于JDK1.7實現的接口String類被final修飾詞修飾&#xff0c;代表不可修改的特性&#xff0c;它實現了三個接口&#xff0c;Serializable是序列化接口&#xff0c;Compareble是排序接口&#xff0c;Char是字符序列接口。主要成員變量char[]&#xff1a;String通過c…

將你一張表的值覆蓋_山西聯通攜手華為完成長風商務區宏微協同,立體覆蓋,打造5G精品網絡...

近日&#xff0c;中國聯通山西分公司(以下簡稱“山西聯通”)在太原長風商務區繼5G CA超高速率升級之后&#xff0c;又針對長風商務區兩層活動區域進行了5G宏微協同的立體覆蓋&#xff0c;實現了該區域5G網絡的連續部署。長風商務區建筑結構設計新穎&#xff0c;占地面積3.06平方…

16速 java_不停歇的 Java 即將發布 JDK 16,新特性速覽!

之前在 JDK 15 中預覽的密封類和接口限制其余類和接口能夠擴展或實現它們。該計劃的目標包括&#xff0c;容許類或接口的做者控制負責實現它的代碼&#xff0c;提供比訪問修飾符更聲明性的方式來限制超類的使用&#xff0c;以及經過提供模式分析的基礎來支持模式匹配的將來方向…

局域網內文件傳輸速度_詳解蒲公英路由器組網 實現文件共享

蒲公英路由器&#xff0c;除了具備普通路由器的功能之外&#xff0c;如圖&#xff1a;最大的特色是可以實現智能組網&#xff1a;最大的特色是可以實現智能組網&#xff1a;采用全新自主研發的Cloud VPN技術替代傳統VPN&#xff0c;基于SD-WAN智能組網方案&#xff0c;快速組建…

java emoji顯示亂碼_Java 解決Emoji表情過濾問題

Emoji表情從三方數據中獲取沒有過濾&#xff0c;導致存入DB的時候報錯。原因&#xff1a;UTF-8編碼有可能是兩個、三個、四個字節。Emoji表情是4個字節&#xff0c;而Mysql的utf8編碼最多3個字節&#xff0c;所以數據插不進去。方法1.將已經建好的表也轉換成utf8mb42&#xff0…

mongotemplate中save拋出異常_異常處理的三個好習慣 | Python 工匠

文 | piglei 編輯 | EarlGrey推薦 | 編程派(微信ID&#xff1a;codingpy)前言如果你用 Python 編程&#xff0c;那么你就無法避開異常&#xff0c;因為異常在這門語言里無處不在。打個比方&#xff0c;當你在腳本執行時按 ctrlc 退出&#xff0c;解釋器就會產生一個 KeyboardI…

java 百度網盤上傳_使用pcs api往免費的百度網盤上傳下載文件的方法

百度個人云盤空間大&#xff0c;完全免費&#xff0c;而且提供了pcs api供調用操作文件&#xff0c;在平時的項目里往里面保存一些文件是很實用的。環境準備&#xff1a;開通讀寫網盤的權限及獲取access_token:http://blog.csdn.net/langyuezhang/article/details/47206621百度…

python縮進教學_Python縮進和選擇學習

縮進Python最具特色的是用縮進來標明成塊的代碼。我下面以if選擇結構來舉例。if后面跟隨條件&#xff0c;如果條件成立&#xff0c;則執行歸屬于if的一個代碼塊。先看C語言的表達方式(注意&#xff0c;這是C&#xff0c;不是Python!)if ( i > 0 ){ x 1; y 2;}如果i …

php如何新建xml文件,PHP中的生成XML文件的4種方法分享

生成如下XML串Xml代碼title1content12009-10-11title2content22009-11-11方法I.【直接生成字符串】使用純粹的PHP代碼生成字符串&#xff0c;并把這個字符串寫入一個以XML為后綴的文件。這是最原始的生成XML的方法&#xff0c;不過有效&#xff01;$data_array array(array(ti…

組態王能直接讀取儀表數據嗎_液晶多功能網絡電力儀表PD800H

液晶多功能網絡電力儀表PD800H-H44三相三線多功用電力表面&#xff0c;一般也被稱作網絡電力表面&#xff0c;它是一種數字化的監控設備&#xff0c;其功用集成了電量測量&#xff0c;情況監控&#xff0c;遠程通訊為一體&#xff0c;作業原理上選用了現代核算機技術和數字信號…

python程序顯示自己的版權_手把手教你Pycharm皮膚主題及個性化設置,python程序員必備-Go語言中文社區...

1.設置IDE皮膚主題File -> Settings -> Appearance -> Theme -> 選擇“Alloy.IDEA Theme”根據自己的喜好設置字體大小&#xff0c;以及樣式。2.修改字體大小File -> Settings > Editor -> Colors & Fonts -> Font -> Size -> 設置為“14”3…

java多線程activemq,多線程JMS客戶端ActiveMQ

我正在使用以下代碼創建多個JMS會話&#xff0c;以供多個使用者使用消息。我的問題是代碼以單線程方式運行。即使消息存在于隊列中&#xff0c;第二個線程也無法接收任何內容&#xff0c;而是繼續輪詢。同時&#xff0c;第一個線程完成對第一批的處理&#xff0c;然后返回并使用…

python cnn 實例_基于CNN的紋理合成實踐【附python實現】

Q0: Preliminary knowledge of Texture SynthesisBaseline請見此處&#xff0c;下文所有的代碼修改均建立此代碼基礎之上。1. 紋理合成簡述?紋理合成(Texture Systhesis)技術主要應用于計算機圖形學等領域&#xff0c;被用于模擬幾何模型的表面細節、增強繪制模型的真實感。不…

php使用jasperreport,php-報表引擎指南(Pentaho,JasperReports,BIRT)

我在各種論壇和他們的網站上花費了大約4-5個小時,研究可以幫助我發展的報告工具.我是使用這種工具的新手,可以使用一些特定的指導.我正在開發一個Web應用程序,該應用程序將托管在一臺服務器上,但是多個用戶可以通過登錄進行訪問.每個用戶將擁有自己的帳戶,并且只能訪問僅與與其…

python中dlib庫_python 基于dlib庫的人臉檢測的實現

本周暫時比較清閑&#xff0c;可以保持每日一更的速度。國外身份證項目新增需求&#xff0c;檢測出身份證正面的人臉。最開始考慮mobilenet-ssd&#xff0c;經同事提醒&#xff0c;有現成的人臉庫dlib&#xff0c;那就用傳統方法嘗試一下。dlib安裝dlib的安裝小費一波周折&…

php養老院管理系統,XYCMS養老院建站系統 v3.8

XYCMS養老院建站系統是一個專為養老院而設計的養老院建筑系統。中心信息管理&#xff1a;包括基本信息管理&#xff0c;添加&#xff0c;問答中心信息管理新聞動態管理&#xff1a;管理新聞信息內容&#xff0c;管理相關分類&#xff0c;添加或者刪除生活環境內容管理&#xff…

php 修改文件訪問時間,PHP中獲取文件創建日期、修改日期、訪問時間的方法

php獲取文件創建時間、修改時間常用代碼filemtime ( string filename )返回文件上次被修改的時間&#xff0c;出錯時返回 FALSE。時間以 Unix 時間戳的方式返回&#xff0c;可用于 date()。例如&#xff1a;$afilemtime("log.txt");echo "修改時間&#xff1a;&…

超過響應緩沖區限制_Nginx如何限制并發連接數和連接請求數?

全網最全1500份Java學習資料、500份BAT面試真題&#xff1a;關注公眾號&#xff0c;輸入“面試題”&#xff0c;獲取提取碼&#xff01;首先講解兩個算發&#xff1a;算法思想是&#xff1a;令牌以固定速率產生&#xff0c;并緩存到令牌桶中&#xff1b;令牌桶放滿時&#xff0…