实验要求: 模拟进程调度的各种算法:
总的来说,建立一个Pcb进程信息类,完成初始化操作,把多个进程对象放入队列中,使用的数据结构是LinkedList 把进程对象放入队列中之后, 就完成了初始化操作,而不同调度算法的不同之处是哪个进程先进入就绪队列呢? 如下图所示 理解了进程的调度过程,具体的实现如下:
- 定义Pcb类:
public class Pcb {
private String name;
private String id;
private int priorityNum;
private int arriveTime;
private int totalRuntime;
private int useCpuTime;
private int finishTime;
private int waitTime;
private double response;
...
...
}
- 定义一个实现各种算法调度的类:PcbMenu:
- 初始化进程
public class ProcessMenu {
LinkedList<Pcb> pcb;
LinkedList<Pcb> readyLink;
Pcb runLink;
LinkedList<Pcb> RQ1 = new LinkedList<>();
LinkedList<Pcb> RQ2 = new LinkedList<>();
LinkedList<Pcb> RQ3 = new LinkedList<>();
public ProcessMenu() {
}
Transform transform = new TransformImp();
public LinkedList<Pcb> init(Pcb newProcess) {
pcb = new LinkedList<>();
readyLink = new LinkedList<Pcb>();
Pcb p1 = new Pcb("进程1", "001", 3, 0, 3);
Pcb p2 = new Pcb("进程2", "002", 2, 2, 6);
Pcb p3 = new Pcb("进程3", "003", 3, 4, 4);
Pcb p4 = new Pcb("进程4", "004", 1, 6, 5);
Pcb p5 = new Pcb("进程5", "005", 5, 8, 2);
pcb.add(p1);
pcb.add(p2);
pcb.add(p3);
pcb.add(p4);
pcb.add(p5);
if(newProcess!=null){
if(pcb.add(newProcess)){
System.out.println("插入成功!");
}
}
Collections.sort(pcb, new compareAt_ServerTime());
readyLink = pcb;
runLink = null;
return readyLink;
}
}
2. 创建实现各种算法的比较器
class compareAt_ServerTime implements Comparator<Pcb> {
@Override
public int compare(Pcb o1, Pcb o2) {
int a = o1.getArriveTime() - o2.getArriveTime();
if (a > 0) {
return 1;
} else if (a == 0) {
return o1.getTotalRuntime() > o2.getTotalRuntime() ? 1 : -1;
} else
return -1;
}
}
class compareAt_Priority implements Comparator<Pcb> {
@Override
public int compare(Pcb o1, Pcb o2) {
int a = o1.getPriorityNum() - o2.getPriorityNum();
if(a>0){
return 1;
}else if(a == 0){
return o1.getArriveTime() > o2.getArriveTime() ? 1: -1;
}else {
return -1;
}
}
}
class compareAt_Response implements Comparator<Pcb> {
@Override
public int compare(Pcb o1, Pcb o2) {
double a = o1.getResponse() - o2.getResponse();
if(a<0.0){
return 1;
}
else if(a == 0.0){
return o1.getArriveTime() > o2.getArriveTime() ? 1: -1;
}else{
return -1;
}
}
}
3. 各种算法的实现:
1. FCFS算法----先来先服务算法
public void FCFS(LinkedList<Pcb> readyLink) {
LinkedList<Pcb> finishLink = new LinkedList<>();
LinkedList<Pcb> waitLink = new LinkedList<>();
int size = readyLink.size();
int lastFinishTime = 0;
int waitTime = 0;
System.out.println("就绪队列的元素个数:"+readyLink.size());
for(int i = 1; i<=size;i++){
System.out.println("第"+i+"次进程调度情况");
Time.startTime();
if(i == 1){
runLink = transform.readyToRun(readyLink);
displayRun(runLink);
runLink.setFinishTime(runLink.getTotalRuntime());
waitTime = 0;
runLink.setWaitTime(waitTime);
}
else{
lastFinishTime = runLink.getFinishTime();
runLink = transform.readyToRun(readyLink);
displayRun(runLink);
runLink.setFinishTime(runLink.getTotalRuntime()+lastFinishTime);
runLink.setWaitTime(lastFinishTime-runLink.getArriveTime());
}
if(i==1){
System.out.println("需要阻塞当前进程吗?请输入Yes或No");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if("Yes".equals(input)){
waitLink = transform.runToWait(runLink);
displayWaitLink(waitLink);
System.out.println("当前进程已经阻塞,需要停止阻塞吗?请输入Yes或No");
Scanner scanner1 = new Scanner(System.in);
String flag = scanner1.nextLine();
if("Yes".equals(flag)){
transform.WaitToReady(waitLink,readyLink);
}
continue;
}
}
long midTime = runLink.getTotalRuntime();
WaitTimeUtil.waitTime(midTime);
Time.endTime();
displayReadyLink(readyLink);
finishLink = transform.runToFinish(runLink);
displayFinishLink(finishLink);
}
}
2. 时间片轮转调度算法
public void TRR(LinkedList<Pcb> readyLink,int time,boolean flag){
LinkedList<Pcb> finishLink = new LinkedList<>();
int size = readyLink.size();
int count = 1;
System.out.println("========处于就绪状态的进程数=======:"+size);
initUseTime(readyLink);
while(readyLink!=null && readyLink.size()!=0){
System.out.println("第"+count+"次进程调度情况");
Time.startTime();
int useTime = readyLink.getFirst().getUseCpuTime();
runLink = transform.readyToRun(readyLink);
displayRun(runLink);
long midTime = time;
WaitTimeUtil.waitTime(midTime);
Time.endTime();
useTime-=time;
runLink.setUseCpuTime(useTime);
if(runLink.getUseCpuTime()<=0){
finishLink = transform.runToFinish(runLink);
displayFinishLink(finishLink);
}
else{
if(flag == true){
int priorityNum = runLink.getPriorityNum();
runLink.setPriorityNum(priorityNum+1);
}
readyLink = transform.runToReady(runLink,readyLink);
}
if(flag == true){
Collections.sort(readyLink,new compareAt_Priority());
System.out.println("就绪队列中的进程信息\n"+readyLink.toString());
}
runLink = null;
count++;
}
}
3. 多级反馈队列算法
public void MQ(LinkedList<Pcb> RQ1){
int RQ1Time = 1;
int RQ2Time = 2;
int RQ3Time = 10;
LinkedList<Pcb> finishLink = new LinkedList<>();
initUseTime(RQ1);
System.out.println("第一级反馈队列调度--时间片为1");
System.out.println("RQ1的大小"+RQ1.size());
while(RQ1.size()>0 &&RQ1!=null){
runLink = transform.readyToRun(RQ1);
displayRun(runLink);
int useTime = runLink.getUseCpuTime();
useTime-=RQ1Time;
runLink.setUseCpuTime(useTime);
if(useTime>0){
RQ2.addFirst(runLink);
}else{
finishLink = transform.runToFinish(runLink);
displayReadyLink(finishLink);
}
Collections.sort(RQ1,new compareAt_ServerTime());
}
System.out.println("====第一级队列进程调度完之后已经完成的进程有:\n"+finishLink);
System.out.println("====第一级时间片用完也没有结束的进入第二级队列====\n"+RQ2.toString());
System.out.println("=====开始第二级队列调度======");
while(RQ2.size()>0&& RQ2!=null){
Collections.sort(RQ2,new compareAt_ServerTime());
runLink = transform.readyToRun(RQ2);
displayRun(runLink);
int useTime = runLink.getUseCpuTime();
useTime-=RQ2Time;
runLink.setUseCpuTime(useTime);
if(useTime>0){
RQ3.addFirst(runLink);
}else{
finishLink = transform.runToFinish(runLink);
displayReadyLink(finishLink);
}
}
System.out.println("第二级队列进程调度完之后已经完成的进程有:\n"+finishLink);
System.out.println("====第二级时间片用完也没有结束的进入第三级队列====\n"+RQ3.toString());
if(RQ3.size()>0&& RQ3!=null){
System.out.println("=====开始第三级队列调度======时间片为8====");
Collections.sort(RQ3,new compareAt_ServerTime());
TRR(RQ3,RQ3Time,false);
}
}
4. 动态优先级算法
public void JF(LinkedList<Pcb> readyLink){
while(readyLink.size()>0&& readyLink!=null){
System.out.println("当前就绪队列的进程数:"+readyLink.size());
Collections.sort(readyLink,new compareAt_Priority());
System.out.println("输出排序之后的队列-----\n"+readyLink);
TRR(readyLink,2,true);
}
}
5. 高响应比优先算法
public void HRRN(LinkedList<Pcb> readyLink){
LinkedList<Pcb> finishLink = new LinkedList<>();
int i = 0;
int lastFinishTime = 0;
int waitTime = 0;
while(readyLink.size()>0 && readyLink!=null){
displayReadyLink(readyLink);
System.out.println("第"+(i+1)+"次进程调度情况");
Time.startTime();
if(i == 0){
runLink = transform.readyToRun(readyLink);
displayRun(runLink);
long midTime = runLink.getTotalRuntime();
WaitTimeUtil.waitTime(midTime);
Time.endTime();
runLink.setFinishTime(runLink.getTotalRuntime());
waitTime = 0;
runLink.setWaitTime(waitTime);
}
else{
lastFinishTime = runLink.getFinishTime();
runLink = transform.readyToRun(readyLink);
displayRun(runLink);
long midTime = runLink.getTotalRuntime();
WaitTimeUtil.waitTime(midTime);
Time.endTime();
runLink.setFinishTime(runLink.getTotalRuntime()+lastFinishTime);
runLink.setWaitTime(lastFinishTime-runLink.getArriveTime());
}
finishLink = transform.runToFinish(runLink);
displayFinishLink(finishLink);
response(readyLink,runLink);
Collections.sort(readyLink, new compareAt_Response());
i++;
}
}
public void response(LinkedList<Pcb> readyLink,Pcb runLink){
double response = 0.0;
for(int i = 0 ;i< readyLink.size();i++){
int waitTime = runLink.getFinishTime() - readyLink.get(i).getArriveTime();
int serveTime = readyLink.get(i).getTotalRuntime();
DecimalFormat df = new DecimalFormat("0.00");
String y = df.format((float)waitTime/serveTime);
double s = Double.valueOf(y);
response = s +1.0;
readyLink.get(i).setResponse(response);
System.out.println(readyLink.get(i).getName()+"的响应比为"+readyLink.get(i).getResponse());
}
}
- 定义实现状态转换的接口和接口的实现类:
transform接口
```java
package com.gcl.process.service;
import com.gcl.process.model.Pcb;
import java.util.LinkedList;
public interface Transform {
//1. 就绪状态转化为运行状态
Pcb readyToRun(LinkedList<Pcb> readyLink);
//2. 运行状态转化为完成状态
LinkedList<Pcb> runToFinish(Pcb runLink);
//3.运行状态转化为就绪状态
LinkedList<Pcb> runToReady(Pcb runLink,LinkedList<Pcb> readyLink);
//3.运行状态转化为等待状态
LinkedList<Pcb> runToWait(Pcb runLink);
//4. 等待状态转化为就绪状态
void WaitToReady(LinkedList<Pcb> WaitLink,LinkedList<Pcb> readyLink);
//5. 新增一个进程
LinkedList<Pcb> addProcess(Pcb newProcess,LinkedList<Pcb> readyLink);
}
transform接口的实现类
package com.gcl.process.service;
import com.gcl.process.model.Pcb;
import java.util.LinkedList;
public class TransformImp implements Transform {
LinkedList<Pcb> finishLink =new LinkedList<>();
LinkedList<Pcb> waitLink = new LinkedList<>();
@Override
public Pcb readyToRun(LinkedList<Pcb> readyLink) {
if(readyLink.size()>0){
return readyLink.removeFirst();
}
return null;
}
@Override
public LinkedList<Pcb> runToFinish(Pcb runLink) {
finishLink.addLast(runLink);
return finishLink;
}
@Override
public LinkedList<Pcb> runToReady(Pcb runLink,LinkedList<Pcb> readyLink) {
readyLink.addLast(runLink);
return readyLink;
}
@Override
public LinkedList<Pcb> runToWait(Pcb runLink) {
waitLink.addLast(runLink);
runLink = null;
return waitLink;
}
@Override
public void WaitToReady(LinkedList<Pcb> WaitLink, LinkedList<Pcb> readyLink) {
for(int i = 0;i<WaitLink.size();i++){
Pcb element = WaitLink.remove(i);
readyLink.addLast(element);
}
}
@Override
public LinkedList<Pcb> addProcess(Pcb newProcess,LinkedList<Pcb> readyLink) {
if(newProcess!=null){
readyLink.addLast(newProcess);
return readyLink;
}
return readyLink;
}
}
|