进程调度优先级JAVA_进程调度-优先级算法(Java简单实现)

该博客介绍了如何在Java中实现优先级调度算法,其中优先级定义为50减去进程的运行时间,每次运行后优先级降低3。通过循环处理找出优先级最高的进程并调度执行,直到所有进程完成。
摘要由CSDN通过智能技术生成

在时间片轮转调度算法中,做了一个隐含的假设,即系统中所有进程的紧迫性是相同的。但实际情况并非如此,为了能满足实际情况的需要,在进程调度算法中引入优先级,而形成优先级调度算法。

本例中实现的优先级的定义为:

优先数 = 50 - 运行时间

每运行一次优先数减3,重新竞争。

实现效果:(只列出输入和最终输出,省略中间过程)

4176469ff1723a3d52d19e2f265c3b43.png

2fbb10305343d37153b78bc8f580c4cd.png

Java实现过程

PCB类在比起时间片轮转算法中新增一个priorityNumber(省略了各个变量的getter与setter)

public class PCB {

String name;

int priorityNumber=50;

int cpuTime=0;

int needTime;

char state='W';

public PCB(String name,int needTime){

this.name = name;

this.needTime = needTime;

this.priorityNumber = 50-needTime;

}

public void printInformation(){

System.out.println(this.getName() +"\t" + this.getCpuTime() + "\t" + this.getNeedTime() + "\t\t" + this.getState());

}

......

}

循环一下处理过程并输出

// 找出优先级最高的进程

int maxPriorityNumber = 0;

for (PCB tempPCB : currentQueue) {

if(tempPCB.getPriorityNumber()>maxPriorityNumber){

maxPriorityNumber=tempPCB.getPriorityNumber();

}

}

// 把优先级最高的进程调配到队首

while(currentQueue.peek().getPriorityNumber() < maxPriorityNumber){

currentQueue.offer(currentQueue.peek());

currentQueue.poll();

}进程调度处理

PCB processingPCB = currentQueue.poll();

if (processingPCB.state != 'F') {

waitQueue.poll();

processingPCB.setState('R');

if (processingPCB.getNeedTime() > round) {

processingPCB

.setCpuTime(processingPCB.getCpuTime() + round);

processingPCB.setNeedTime(processingPCB.getNeedTime()

- round);

processingPCB.setState('W');

} else {

processingPCB.setCpuTime(processingPCB.getCpuTime()

+ processingPCB.getNeedTime());

processingPCB.setNeedTime(0);

processingPCB.setState('F');

}

// 执行完毕竞争数减3

}

currentQueue.offer(processingPCB);遍历输出结果(略)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值