CPU进程调度简单模拟(PriorityFirst)--Java
程序员文章站
2022-04-02 11:52:01
...
这是我的作业,要求的输出结果是:各进程的等待时间、周转时间及平均等待、周转时间
进程的基本信息我直接写在了程序里(懒得运行时一一输入)。写了两个Class,一个储存进程的基本信息(Work),一个模拟进程(Test)
Work Class:
/*
* Student name: Xin
* Program name: Work
* Program purpose: Test the scheduling algorithm sumulation (PriorityFirst)
*/
import java.util.*;
public class Work implements Comparable {
// 用 ComparaTo 在 jobQueue中对进程第一次排序
@Override
public int compareTo(Object o){
//如果进程的到达时间相同,则以优先级排序。否则按到达时间排序
Work p = (Work)o;
if(this.arrivalTime == p.arrivalTime)
return this.priority < p.priority ? -1:1;
else
return this.arrivalTime < p.arrivalTime ? -1:1;
}
// 变量:到达时间,运行时间,优先级,进程名
private int arrivalTime;
private int burstTime;
private int priority;
private String name;
// 变量:平均等待时间,平均周转时间
private int turnAroundTime;
private int waitingTime;
// 获取变量值
public int getArrivalTime() {return arrivalTime;}
public int getPriority() {return priority;}
public int getBurstTime() {return burstTime;}
public String getName() {return name;}
public int getWaitingTime() {return waitingTime;}
public int getTurnAroundTime() {return turnAroundTime;}
// 设置变量值
public void setArrivalTime(int arrivalTime) {this.arrivalTime = arrivalTime;}
public void setBurstTime(int burstTime) {this.burstTime = burstTime;}
public void setPriority(int priority) {this.priority = priority;}
public void setName(String name) {this.name = name;}
public void setWaitingTime(int waitingTime) {this.waitingTime = waitingTime;}
public void setTurnAroundTime(int turnAroundTime) {this.turnAroundTime = turnAroundTime;}
// 第一组数据(进程同时到达)
public static PriorityQueue<Work> generateProcess1(){
String[] name1 = {"A","B","C","D","E"};
int[] at1 = {0,0,0,0,0};
int[] bt1 = {7,3,9,5,1};
int[] pri1 = {3,5,2,1,4};
PriorityQueue<Work> runQueue1= new PriorityQueue<>();
for(int i = 0; i<5;i++){
Work process = new Work();
process.setName(name1[i]);
process.setArrivalTime(at1[i]);
process.setBurstTime(bt1[i]);
process.setPriority(pri1[i]);
runQueue1.add(process);
}
return runQueue1;
}
// 第二组数据(到达时间不同)
public static PriorityQueue<Work> generateProcess2(){
String[] name2 = {"A","B","C","D","E"};
int[] at2 = {0,2,4,6,8};
int[] bt2 = {7,3,9,5,1};
int[] pri2 = {3,5,2,1,4};
PriorityQueue<Work> runQueue2= new PriorityQueue<>();
for(int i = 0; i<5;i++){
Work process = new Work();
process.setName(name2[i]);
process.setArrivalTime(at2[i]);
process.setBurstTime(bt2[i]);
process.setPriority(pri2[i]);
runQueue2.add(process);
}
return runQueue2;
}
}
Test Class:
/*
* Student name: Xin
* Program name: Test (Program 3)
* Program purpose: Test the scheduling algorithm sumulation (PriorityFirst)
*/
import java.util.*;
/*
* 思路:要实现优先级算法模拟,考虑优先级外也要考虑进程的到达时间(如果进程先到达,即使它的优先级低,也要先运行)。因此我在原始的JobQUEUE和后来的readyQUEUE分别对进程进行排序。在jobQUEUE里,如果进程的到达时间不同,则按到达时间先后排序;在readyQUEUE里,则按优先级排序。这样可以确保每一个进程进入readyQUEUE时,都是先到达的进程
*/
public class Test {
public static int numberOfProcess;
// 用 Comparator 对readyQueue里的进程进行排序,根据优先级
public static Comparator<Work> theComparator = new Comparator<Work>(){
public int compare(Work work1, Work work2){
return work1.getPriority() < work2.getPriority()? -1:1;
}};
// Main method
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
// 用户输入进程数
System.out.println("Enter the # of processes.");
numberOfProcess = userInput.nextInt();
// 创建两个 PriorityQueue q1,q2
PriorityQueue<Work> q1 = new PriorityQueue<>();
PriorityQueue<Work> q2 = new PriorityQueue<>();
// generateProcess method 对进程进行排序
q1 = Work.generateProcess1();
q2 = Work.generateProcess2();
System.out.println("\nThe running order and details of data set 1: ");
priorityFirst(q1);
System.out.println("\nThe running order and details of data set 2: ");
priorityFirst(q2);
}
// The priorityFirst method
public static Queue<Work> priorityFirst(PriorityQueue<Work> queue)
{
// 设置变量
double averageWaitTime = 0.00;
double averageTurnAroundTime = 0.00;
int startTime = 0;
int finishTime = 0;
int queueSize = queue.size();
Work process;
// 创建readyQueue(大小为进程的数量,用Comparetor排序进程)
PriorityQueue<Work> readyQueue = new PriorityQueue<Work>(numberOfProcess, theComparator);
// (这个数字是我随便设的-。- 我也不太确定要loop多少次)
for (int i = 0; i<10;i++){
// 当queue和readyQueue其中有不为空时(即还有进程没有运行)
while(!queue.isEmpty() || !readyQueue.isEmpty()){
// 看readyQueue是否为空,若为空,则意味着所有READY state 的进程都完成运行了。此时从queue中取下一个进程
process=readyQueue.isEmpty()? queue.poll():readyQueue.poll();
// 计算开始时间,结束时间
startTime = finishTime;
finishTime = startTime + process.getBurstTime();
// 如果queue里第一个进程的到达时间小于刚刚进入readyQueue的进程的结束时间(即意味着当刚刚进入的那个进程,完成运行,下一个进程已会到达),则queue里的进程可以继续被加入到readyQueue
while(queue.peek()!=null && queue.peek().getArrivalTime()<=finishTime)
readyQueue.add(queue.poll());
// 计算等待时间(开始时间-到达时间)
process.setWaitingTime(startTime-process.getArrivalTime());
// 输入等待时间
System.out.println(process.getName()+" Process's waiting time:"+process.getWaitingTime());
// 计算周转时间(结束时间-到达时间)
process.setTurnAroundTime(finishTime-process.getArrivalTime());
// 输出到达时间
System.out.println(process.getName()+" Process's turn around time:"+process.getTurnAroundTime());
// 计算平均值
averageWaitTime = averageWaitTime + process.getWaitingTime();
averageTurnAroundTime = averageTurnAroundTime + process.getTurnAroundTime();
}}
// 计算平均值
averageTurnAroundTime /= queueSize;
averageWaitTime /= queueSize;
// 输出平均值
System.out.println("\nAverage Wait time: "+ averageWaitTime);
System.out.println("Average Turn Around time: "+ averageTurnAroundTime);
return readyQueue;
}
}
最终输出结果:
上一篇: 求平方根算法