一、概述

  因为这次os作业对用户在控制台的输入输出有要求,所以我花了挺多的代码来完善控制台的显示。

  也因为我这次要实现多个类似算法,所以将一些共性单独提取出来作为一个类。  

  如果只想要和算法有关的核心代码,看RR类的calc()即可

  实现思路:每运行一个进程,则将所有进程的remainServiceTime减去一个时间片的长度。

二、运行结果

  1. 测试数据:

  2. 运行结果:

三、流程图

四、实现代码

  1. RR类(主类)

只有calc()中涉及了算法,init()和printResult()只有简单的输入输出操作。

 package xqy.algorithm;

 import java.util.*;

 import xqy.Util.Tools;
import xqy.been.Process; /**
* @author xqy
* @date 2018年12月19日19:14:49
*/
public class RR {
private int processNumber;
private ArrayList<Process> processList;
private int timeSlice; public RR() {
init();
calc();
Tools.printResult(processList);
} private void init() {
Scanner sc = new Scanner(System.in); System.out.print("<RR> Please enter the slice time:");
timeSlice = sc.nextInt();
System.out.print("<RR> please enter the process num:");
processNumber = sc.nextInt(); processList = new ArrayList<Process>();
for (int i = 0; i < processNumber; i++) {
processList.add(new Process());
} System.out.println("<RR> Please enter each process arrival time:");
for (int i = 0; i < processNumber; i++) {
System.out.print(" Process" + (i + 1) + ":");
processList.get(i).setArrivalTime(sc.nextInt());
} System.out.println("<RR> Please enter each process service time:");
for (int i = 0; i < processNumber; i++) {
System.out.print(" Process" + (i + 1) + ":");
int servicesTime = sc.nextInt(); processList.get(i).setServicesTime(servicesTime);
processList.get(i).setRemainServiceTime(servicesTime);
}
} private void calc() {
int timeNow = 0;
int processRemain = processNumber;
boolean noProcessRunInThisTurn;
Process opProcess; while (processRemain != 0) {
noProcessRunInThisTurn = true; for (int i = 0; i < processNumber; i++) {
opProcess = processList.get(i); if ((opProcess.getRemainServiceTime() > 0)
&& (timeNow >= opProcess.getArrivalTime())) {
// First time
if (opProcess.getServicesTime() == opProcess
.getRemainServiceTime()) {
int waitTime = timeNow - opProcess.getArrivalTime(); opProcess.setStartTime(timeNow);
opProcess.setWaitTime(waitTime);
} // Calculating remain service time
int remainServiceTime = opProcess.getRemainServiceTime()
- timeSlice;
opProcess.setRemainServiceTime(remainServiceTime); // Last time
if (remainServiceTime <= 0) {
int completionTime = timeNow + timeSlice; // The process ends when the current slice is completed.
int turnAroundTime = completionTime
- opProcess.getArrivalTime();
double turnAroundTimeWithWeight = 1.0 * turnAroundTime
/ opProcess.getServicesTime(); opProcess.setCompletionTime(completionTime);
opProcess.setTurnAroundTime(turnAroundTime);
opProcess
.setTurnAroundTimeWithWeight(turnAroundTimeWithWeight);
processRemain--;
} timeNow += timeSlice;
noProcessRunInThisTurn = false; System.out.println(" #STEP# Process" + (i + 1)
+ " remain service time:"
+ opProcess.getRemainServiceTime()
+ " , timeBefore:" + (timeNow - 1) + ", timeNow:"
+ timeNow
+ ((remainServiceTime <= 0) ? " Finish" : ""));
} else {
// do noting, because this process has been completed or hasn`t arrived.
}
} // Means no process could run, because they have arrived.
if ((processRemain > 0) && noProcessRunInThisTurn) {
timeNow += timeSlice;
}
}
}
}

  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<Process> 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<Process> 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<Process> processList) {
28 System.out.println("\n #RESULT#");
29
30 System.out.print("\tArrive:\t\t");
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:\t\t");
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:\t\t");
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:\t\t");
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 }

Java实现进程调度算法(二) RR(时间片轮转)的更多相关文章

  1. Java实现进程调度算法(一) FCFS(先来先服务)

    一.概述 因为这次os作业对用户在控制台的输入输出有要求,所以我花了挺多的代码来完善控制台的显示. 也因为我这次要实现多个类似算法,所以将一些共性单独提取出来作为一个类. 如果只想要和算法有关的核心代 ...

  2. 《操作系统_时间片轮转RR进程调度算法》

    转自:https://blog.csdn.net/houchaoqun_xmu/article/details/55540250 时间片轮转RR进程调度算法 一.概念介绍和案例解析时间片轮转法 - 基 ...

  3. 进程调度算法spf,fpf,时间片轮转算法实现

    调度的基本概念:从就绪队列中按照一定的算法选择一个进程并将处理机分配给它运行,以实现进程并发地执行. 进程信息 struct node { string name;//进程名称 int id;//进程 ...

  4. Linux常见的进程调度算法

    进程调度:在操作系统中调度是指一种资源分配. 调度算法是指: 根据系统的资源分配策略所规定的资源分配算法. 操作系统管理了系统的有限资源,当有多个进程(或多个进程发出的请求)要使用这些资源时,因为资源 ...

  5. 进程调度算法Linux进程调度算法

    这次介绍一下操作系统的进程调度算法 操作系统的调度分为三种:1.远程调度(创建新进程):2.中程调度(交换功能的一部分):3.短程调度(下次执行哪个进程) 这次讲述的就是短程调度,可以简单的看作咱们平 ...

  6. 分析Linux内核中进程的调度(时间片轮转)-《Linux内核分析》Week2作业

    1.环境的搭建: 这个可以参考孟宁老师的github:mykernel,这里不再进行赘述.主要是就是下载Linux3.9的代码,然后安装孟宁老师编写的patch,最后进行编译. 2.代码的解读 课上的 ...

  7. Java NIO入门(二):缓冲区内部细节

    Java NIO 入门(二)缓冲区内部细节 概述 本文将介绍 NIO 中两个重要的缓冲区组件:状态变量和访问方法 (accessor). 状态变量是前一文中提到的"内部统计机制"的 ...

  8. Linux 常见的进程调度算法

    1.在介绍进程调度之前,先对进程的状态的概念应该有所了解,下面是关于进程状态的一些基本概念:进程的状态分为三种,分别为: 1).运行态:该状态表明进程在实际占用CPU 2).就绪态: 该状态下进程可以 ...

  9. [Java并发编程(二)] 线程池 FixedThreadPool、CachedThreadPool、ForkJoinPool?为后台任务选择合适的 Java executors

    [Java并发编程(二)] 线程池 FixedThreadPool.CachedThreadPool.ForkJoinPool?为后台任务选择合适的 Java executors ... 摘要 Jav ...

随机推荐

  1. lambda 、 map 、filter 、reduce 及 reversed 常用函数

    lambda 匿名函数 什么是lambda? lambda 操作符(或 lambda 函数)通常用来创建小巧的,一次性的匿名函数对象.它的基本语法如下: lambda arguments : expr ...

  2. Wannafly挑战赛29题解

    这套题目非常有意思啊23333--话说为啥没有上条先生的呢-- 传送门 \(A\) 御坂美琴 蠢了--首先先判总共加起来等不等于\(n\),不是的话就不行 然后dfs记录\(n\)不断分下去能分成哪些 ...

  3. memcache面试题汇总

    1,memcached是原子的吗? 所有的被发送到memcached的单个命令是完全原子的.如果您针对同一份数据同时发送了一个set命令和一个get命令,它们不会影响对方.它们将被串行化.先后执行.即 ...

  4. Security-OAuth2.0 密码模式之客户端实现

    我的OAuth2.0 客户端项目目录 pom 的配置 <?xml version="1.0" encoding="UTF-8"?> <proj ...

  5. 2016级算法期末上机-I.难题·ModricWang's Fight with DDLs III

    1126 ModricWang's Fight with DDLs III 思路 由于题目中已经说明了时间经过了正无穷,因此初始位置是不重要的,并且每条边.每个点的地位是均等的.因此到达每个点的概率就 ...

  6. 禅道与bugfree区别

    bugfree更偏重bug管理, 禅道是以项目管理为目标, 所以出发点不一样. 我们bug追踪管理使用bugfree,项目管理计划使用禅道,配置管理使用svn bugfree很low,安装需要环境特别 ...

  7. C# 并发队列ConcurrentQueue

    测试函数 static async Task RunProgram() { var taskQueue = new ConcurrentQueue<CustomTask>(); var c ...

  8. 关于莫比乌斯函数的塞 : 莫比乌斯前n项和 , 莫比乌斯函数绝对值的前n项和

    https://ac.nowcoder.com/acm/contest/874#submit/{%22problemIdFilter%22%3A25455%2C%22statusTypeFilter% ...

  9. CentOS7 安装 PostGIS方法(适合国内网络)

    安装Postgresql 9.4 yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-1. ...

  10. po'j2559 Largest Rectangle in a Histogram 单调栈(递增)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29498 ...