Java实现进程调度算法(二) RR(时间片轮转)
一、概述
因为这次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(时间片轮转)的更多相关文章
- Java实现进程调度算法(一) FCFS(先来先服务)
一.概述 因为这次os作业对用户在控制台的输入输出有要求,所以我花了挺多的代码来完善控制台的显示. 也因为我这次要实现多个类似算法,所以将一些共性单独提取出来作为一个类. 如果只想要和算法有关的核心代 ...
- 《操作系统_时间片轮转RR进程调度算法》
转自:https://blog.csdn.net/houchaoqun_xmu/article/details/55540250 时间片轮转RR进程调度算法 一.概念介绍和案例解析时间片轮转法 - 基 ...
- 进程调度算法spf,fpf,时间片轮转算法实现
调度的基本概念:从就绪队列中按照一定的算法选择一个进程并将处理机分配给它运行,以实现进程并发地执行. 进程信息 struct node { string name;//进程名称 int id;//进程 ...
- Linux常见的进程调度算法
进程调度:在操作系统中调度是指一种资源分配. 调度算法是指: 根据系统的资源分配策略所规定的资源分配算法. 操作系统管理了系统的有限资源,当有多个进程(或多个进程发出的请求)要使用这些资源时,因为资源 ...
- 进程调度算法Linux进程调度算法
这次介绍一下操作系统的进程调度算法 操作系统的调度分为三种:1.远程调度(创建新进程):2.中程调度(交换功能的一部分):3.短程调度(下次执行哪个进程) 这次讲述的就是短程调度,可以简单的看作咱们平 ...
- 分析Linux内核中进程的调度(时间片轮转)-《Linux内核分析》Week2作业
1.环境的搭建: 这个可以参考孟宁老师的github:mykernel,这里不再进行赘述.主要是就是下载Linux3.9的代码,然后安装孟宁老师编写的patch,最后进行编译. 2.代码的解读 课上的 ...
- Java NIO入门(二):缓冲区内部细节
Java NIO 入门(二)缓冲区内部细节 概述 本文将介绍 NIO 中两个重要的缓冲区组件:状态变量和访问方法 (accessor). 状态变量是前一文中提到的"内部统计机制"的 ...
- Linux 常见的进程调度算法
1.在介绍进程调度之前,先对进程的状态的概念应该有所了解,下面是关于进程状态的一些基本概念:进程的状态分为三种,分别为: 1).运行态:该状态表明进程在实际占用CPU 2).就绪态: 该状态下进程可以 ...
- [Java并发编程(二)] 线程池 FixedThreadPool、CachedThreadPool、ForkJoinPool?为后台任务选择合适的 Java executors
[Java并发编程(二)] 线程池 FixedThreadPool.CachedThreadPool.ForkJoinPool?为后台任务选择合适的 Java executors ... 摘要 Jav ...
随机推荐
- Visual Studio code安装步骤
1.官方下载:https://code.visualstudio.com/,本人电脑是window系统 下载之后,双击安装,安装完之后左侧栏那边是英文,如何变为中文: 按快捷键ctrl+shift+ ...
- Windows文件共享,报错"该用户已禁用"解决方案
文章背景:学校机器有俩个用户think和adminsitrator.默认administrator禁用,think属于administrators组. 经排错得问题出现情况.因为同学们都是Think用 ...
- Unity 下集成第三方原生 SDK,以极光厂商通道为例
Unity中集成三方SDK有两种方式: Unity 项目开发中时常有集成 Android 第三方 SDK 的需求,比如接入第三方推送,分享等功能.而第三方 SDK 的集成文档提到的往往是基于原生 An ...
- python中type、class、object的区别
type 一. type可以用来返回一个对象的类型 例如: 二. 由于Python中一切皆对象,也就是说Python中的任何变量类型都是可以被修改的,这也是Python等动态编程语言的特点.type的 ...
- QuantLib 金融计算——随机过程之 Heston 过程
目录 QuantLib 金融计算--随机过程之 Heston 过程 Heston 过程 参考文献 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib 金融计算--随机过程之 H ...
- Windows下部署安装Docker
好长时间没用Docker,最近准备部署一下,做个记录,今天早上去官网下载,发现Docker开始区分Docker Community Edition(社区版)和Docker Enterprise Edi ...
- Apache Maven的入门使用之项目的基本构建(1)
前言 最近在研究java框架struts2的相关漏洞,然后就去看了官方给出的文档.在看文档的过程中发现使用到了Apache Maven这个项目管理工具,我在网上搜索了一下,大多数文章都写得不是很系统, ...
- oauth2.0的授权流程详解
授权模式 1)oauth2.0 提供了四种授权模式,开发者可以根据自己的业务情况自由选择. 授权码授权模式(Authorization Code Grant) 隐式授权模式(简化模式)(Implici ...
- linux通过源码安装nodejs
1.下载nodejs源码,下载地址:https://nodejs.org/en/download/ 2.下载回来后,解压缩,ubuntu系统ctrl + alt +T 打开终端 假如tar.gz压缩包 ...
- 四大组件之Activity——生命周期
1.完整的Activity生命周期 包括onCreate() -> onStart() -> onResume() -> onPause() -> onStop -> o ...