/*基于优先级的进程调度算法 */
public class PrivilegeProcess {
public static void main(String[] args) {
MyQueue myqueue = new MyQueue();//声明队列
PCB[] pcb = {new PCB(001,8,1),new PCB(002,7,9),new PCB(003,3,8),new PCB(004,1,7),new PCB(005,7,4)};
PCB para = new PCB();
for(int i=0;i<pcb.length;i++){//初始化后首先执行一次排序,这里使用的是选择排序,优先级高的先入队
for(int j=i;j<pcb.length;j++){
if(pcb[i].privilege < pcb[j].privilege){
para = pcb[i];
pcb[i] = pcb[j];
pcb[j] = para;
}
}
}
System.out.println("初次入队后各进程的顺序:");
for(int i=0;i<pcb.length;i++){
System.out.println("初次入队后 # processname : " + pcb[i].name + " totaltime : " + pcb[i].totaltime + " privilege :" + pcb[i].privilege);
}
System.out.println();
myqueue.start(pcb);
}
}
class MyQueue {
int index = 0;
PCB[] pc = new PCB[5];
PCB[] pc1 = new PCB[4];
PCB temp = new PCB();
public void enQueue(PCB process){//入队算法
if(index==5){
System.out.println("out of bounds !");
return;
}
pc[index] = process;
index++;
}
public PCB deQueue(){//出队算法
if(index==0)
return null;
for(int i=0;i<pc1.length;i++){
pc1[i] = pc[i+1];
}
index--;
temp = pc[0];
for(int i=0;i<pc1.length;i++){
pc[i] = pc1[i];
}
return temp;
}
public void start(PCB[] pc){//显示进程表算法
while(pc[0].isNotFinish==true||pc[1].isNotFinish==true||pc[2].isNotFinish==true||pc[3].isNotFinish==true||pc[4].isNotFinish==true){
//*注意:||运算符,所有表达式都为false结果才为false,否则为true
for(int i=0;i<pc.length;i++){
pc[i].run(this);
}
System.out.println();
for(int i=0;i<pc.length;i++){//所有进程每执行完一次时间片长度的运行就重新按优先级排列一次
for(int j=i;j<pc.length;j++){
if(pc[i].privilege < pc[j].privilege){
temp = pc[i];
pc[i] = pc[j];
pc[j] = temp;
}
}
}
}
}
}
class PCB {//声明进程类
int name,totaltime,runtime,privilege;
boolean isNotFinish;
public PCB(){
}
public PCB(int name, int totaltime, int privilege){
this.name = name;//进程名
this.totaltime = totaltime;//总时间
this.privilege = privilege;//优先级别
this.runtime = 2;//时间片,这里设值为2
this.isNotFinish = true;//是否执行完毕
System.out.println("初始值: processname : " + name + " totaltime : " + totaltime + " privilege :" + privilege );
System.out.println();
}
public void run (MyQueue mq){//进程的基于时间片的执行算法
if(totaltime>1){
totaltime-=runtime;//在总时间大于1的时候,总时间=总时间-时间片
privilege--;
System.out.println(" processname : " + name + " remaintime : " + totaltime + " privilege :" + privilege );
}else if(totaltime==1){
totaltime--;//在总时间为1时,执行时间为1
privilege--;
System.out.println(" processname : " + name + " remaintime : " + totaltime + " privilege :" + privilege );
}else{
isNotFinish = false;//总时间为0,将isNotFinish标记置为false
}
if(isNotFinish==true){
mq.deQueue();
mq.enQueue(this);
}
}
}
java进程调度PrivilegeProcess
需积分: 9 134 浏览量
2008-10-22
14:19:39
上传
评论 1
收藏 1KB RAR 举报
gjl1988
- 粉丝: 0
- 资源: 1
最新资源
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈