进程调度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 }

内容来源于网络如有侵权请私信删除

(1)、假定系统有五个进程,每一个进程用一个进程控制块PCB来代表。进程控制块的格式 (2)、每次运行你所设计的处理器调度程序之前,为每个进程任意确定它的“要求运行时间”。 把五个进程按顺序排成循环队列,用指针指出队列连接情况。另用一标志单元记录轮到运行的进程。 (3)、处理器调度总是选择标志单元指示的进程运行。由于本实验是模拟处理器调度的功能,所以,对被选中的进程并不实际启动运行 (4)、进程运行一次后,应把该进程的进程控制块中的指针值送到标志单元,以指示下一个轮到运行的进程。同时,应判断该进程的要求运行时间与已运行时间,若该进程要求运行时间≠已运行时间,则表示它尚未执行结束,应待到下一轮时再运行。若该进程的要求运行时间=已运行时间,则表示它已经执行结束,应把它的状态修改为“结束”(E)且退出队列。此时,应把该进程的进程控制块中的指针值送到前面一个进程的指针位置。 (5)、若“就绪”状态的进程队列不为空,则重复上面(4)和(5)的步骤,直到所有进程都成为“结束”状态。 (6)、在所设计的称序中应有显示或打印语句,能显示或打印每次被选中进程的进程名以及运行一次后进称对列的变化。 (7)、为五个进程任意确定一组“要求运行时间”,启动所设计的处理器调度程序,显示或打印逐次被选中进程的进程名以及进程控制块的动态变化过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值