- 处理机调度算法:Priority scheduling algorithm
优先级调度算法(抢占式)
---------------------java代码------------------------
package operate;
import java.util.*;
public class PSA2 {
static Scanner cin = new Scanner(System.in);
static class PCB implements Comparable<PCB>{
int id;
int arriveTime;
int runTime;
int hasRanTime = 0;
int PRI;
double responseRatio;
int responseTime;
int turnAroundTime;
int waitTime;
public PCB(int id, int arriveTime, int runTime, int PRI) {
this.id = id;
this.arriveTime = arriveTime;
this.runTime = runTime;
this.PRI = PRI;
}
@Override
public int compareTo(PCB o) {
return this.arriveTime - o.arriveTime;
}
}
static PCB[] pcbs;
static Queue<PCB> queue = new PriorityQueue<>();
static void calculateResponseRatio(PCB pcb, int currentTime) {
pcb.waitTime = currentTime+1 - pcb.arriveTime;
pcb.responseRatio = (pcb.waitTime+pcb.runTime)*1.0/pcb.runTime;
}
static void initPCB(){
System.out.print("输入进程数: ");
int num = cin.nextInt();
pcbs = new PCB[num+1];
System.out.println("输入 到达时间, 运行时间, 优先级");
for(int i = 1; i <= num; i++) {
System.out.print("进程" + i + ":");
pcbs[i] = new PCB(i, cin.nextInt(), cin.nextInt(), cin.nextInt());
queue.offer(pcbs[i]);
}
}
static boolean judge(Queue<PCB> readyQueue, int currentTime){
boolean flag = false;
while (true){
PCB pcb = queue.peek();
if (pcb == null){
break;
}else if(pcb.arriveTime <= currentTime){
PCB runPCB = queue.poll();
readyQueue.offer(runPCB);
flag = true;
}else {
break;
}
}
return flag;
}
static boolean processRun(PCB pcb, int currentTime){
if(pcb.hasRanTime == 0){
pcb.responseTime = currentTime;
}
pcb.hasRanTime++;
System.out.printf(" %d ", pcb.id);
if(pcb.hasRanTime == pcb.runTime){
pcb.turnAroundTime = currentTime+1 - pcb.arriveTime;
pcb.waitTime = pcb.turnAroundTime - pcb.runTime;
pcb.responseTime -= pcb.arriveTime;
return true;
}else {
System.out.println();
return false;
}
}
static void printReadyProcess(Queue<PCB> queue, int currentTime){
Iterator<PCB> iterator = queue.iterator();
System.out.print(" 就绪队列 >> ");
while (iterator.hasNext()) {
PCB pcb = iterator.next();
calculateResponseRatio(pcb, currentTime);
System.out.printf("进程%d 优先级%d; ", pcb.id, pcb.PRI);
}
System.out.println();
}
static void run() {
int currentTime = 0;
if(!queue.isEmpty()){
currentTime = queue.peek().arriveTime;
}
Queue<PCB> readyQueue = new PriorityQueue<PCB>(new Comparator<PCB>() {
@Override
public int compare(PCB o1, PCB o2) {
if(o1.PRI != o2.PRI){
return (o2.PRI - o1.PRI) > 0 ? 1 : -1;
}else {
return o1.arriveTime - o2.arriveTime;
}
}
});
PCB runPcb = null;
System.out.println("now 正在运行的进程");
while (true) {
System.out.printf("%d\t ", currentTime);
if(queue.isEmpty() && readyQueue.isEmpty() && runPcb == null){
System.out.println("当前所有进程运行结束");
break;
}else{
if(judge(readyQueue, currentTime) == true) {
if(runPcb != null) {
readyQueue.offer(runPcb);
runPcb = null;
}
}
if(runPcb != null){
if(processRun(runPcb, currentTime) == true){
printReadyProcess(readyQueue, currentTime);
runPcb = null;
}
}else {
runPcb = readyQueue.poll();
if(runPcb == null){
currentTime++;
System.out.printf(" 处理机空闲,\n");
continue;
}else{
if(processRun(runPcb, currentTime) == true){
printReadyProcess(readyQueue, currentTime);
runPcb = null;
}
}
}
currentTime++;
}
}
}
public static void main(String[] args) {
initPCB();
System.out.println("-----处理机开始运行-----");
run();
System.out.println("-----处理机运行结束-----");
showTurnAroundTime();
}
private static void showTurnAroundTime() {
double averageT = 0;
double averageWTAT = 0;
double averageWT = 0;
System.out.println("进程\t 周转时间\t 带权周转时间\t 等待时间\t");
for (int i = 1; i < pcbs.length; i ++) {
int turnAroundTime = pcbs[i].turnAroundTime;
double weightTurnAroundTime = turnAroundTime*1.0/pcbs[i].runTime;
int waitTime = pcbs[i].waitTime;
System.out.printf("%d\t %d\t\t\t %.2f\t\t\t %d\n" ,i , turnAroundTime, weightTurnAroundTime, waitTime);
averageT += turnAroundTime;
averageWTAT += weightTurnAroundTime;
averageWT += waitTime;
}
averageT /= pcbs.length-1;
averageWTAT /= pcbs.length-1;
averageWT /= pcbs.length-1;
System.out.println("平均周转时间:" + averageT);
System.out.println("平均带权周转时间:" + averageWTAT);
System.out.println("平均等待时间:" + averageWT);
}
}