java实现动态优先权算法
来源:互联网 发布:网络数据分析工具解析 编辑:程序博客网 时间:2024/06/11 18:48
本模拟程序实现对N个进程根据优先权的高低调度的模拟,创建进程描述符PCB,进程的优先权在运行过程中动态改变,每个时间片结束后显示当前各进程的状态。具体要求如下:
1) 用JAVA语言来实现对N个进程采用动态优先权算法的进程调度;
2) 每个用来标识进程的进程控制块PCB包括以下字段:
n 进程标识数 ID。
n 进程优先数 PRIORITY,并规定优先数越大的进程,其优先权越高;
n 进程已占用的CPU时间CPUTIME。
n 进程还需占用的CPU时间NEEDTIME,当进程运行完毕时,NEEDTIME变为0;
n 进程的阻塞时间STARTBLOCK,表示当进程再运行STARTBLOCK个时间片后,将进入阻塞状态;
n 进程被阻塞的时间BLOCKTIME,表示已阻塞的进程再等待BLOCKTIME个时间片后,将转换成就绪状态;
n 进程状态STATE;
n 队列指针NEXT,用来将PCB排成队列。
3) 优先数改变的原则:
进程在就绪队列中呆一个时间片,优先数加1;
进程每运行一个时间片,优先数减3;
4) 假设在调度前,系统中有5个进程,它们的初始状态如下:
ID
0
1
2
3
4
PRIORITY
9
38
30
29
0
CPUTIME
0
0
0
0
0
ALLTIME
3
3
6
3
4
STARTBLOCK
2
-1
-1
-1
-1
BLOCKTIME
3
0
0
0
0
STATE
READY
READY
READY
READY
READY
5) 为了清楚的观察各进程的调度过程,程序应将每个时间片内的情况显示出来,参照的具体格式如下:
RUNNING PROG:i
READY-QUEUE:-〉id1-〉id2
BLOCK-QUEUE:-〉id3-〉id4
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = == = =
ID 0 1 2 3 4
PRIORITY P0 P1 P2 P3 P4
CUPTIME C0 C1 C2 C3 C4
ALLTIME A0 A1 A2 A3 A4
STARTBLOCK T0 T1 T2 T3 T4
BLOCKTIME B0 B1 B2 B3 B4
STATE S0 S1 S2 S3 S4
package com.os;import java.util.Queue;import java.util.LinkedList;//队列类 import java.util.Scanner;class PCB {public int id; // 进程IDpublic int pri; // 进程优先级public int cput; // 进程已占用的时间片public int allt; // 进程还需占用的时间片public int startblock; // 进程开始时间片public int endblock; // 进程阻塞时间片public String state; // 进程状态public PCB() // 构造方法{}}// 进程控制块public class DP {public PCB rpcb = null; // 正在运行的进程public LinkedList<PCB> readyqueue = null; // 就绪队列public LinkedList<PCB> blockqueue = null; // 阻塞队列public LinkedList<PCB> tempqueue = null; // 临时队列,存储初始队列;public LinkedList<PCB> overqueue = null; // 执行完成的队列public int[] ids = { 0, 1, 2, 3, 4 };public int[] pris = { 9, 38, 30, 29, 0 };public int[] cputs = { 0, 0, 0, 0, 0 };public int[] allts = { 3, 3, 6, 3, 4 };public int[] sts = { 2, -1, -1, -1, -1 };public int[] ens = { 3, 0, 0, 0, 0 };public String[] states = { "READY", "READY", "READY", "READY", "READY" };public DP() {readyqueue = new LinkedList<PCB>();blockqueue = new LinkedList<PCB>();tempqueue = new LinkedList<PCB>();overqueue = new LinkedList<PCB>();}public void readyqueueSort(LinkedList<PCB> queue) // 就绪队列优先权排序{PCB temppcb1, temppcb2, temppcb3;int count1 = queue.size();int count2 = queue.size() - 1;LinkedList<PCB> queueQ = new LinkedList<PCB>();while (count1 > 0) {temppcb3 = temppcb1 = queue.poll();count1--;while (count2 > 0) {temppcb2 = queue.poll();count2--;if (temppcb2.pri >= temppcb1.pri)// 优先权{queue.offer(temppcb2);} else {queue.offer(temppcb1);temppcb1 = new PCB();temppcb1 = temppcb2;}}queue.offer(temppcb1);count2 = queue.size() - 1;}}public void blockqueueSort(LinkedList<PCB> queue) // 初始化阻塞队列剩余时间片排序{PCB temppcb1, temppcb2;int count1 = queue.size();int count2 = queue.size() - 1;while (count1 > 0) {temppcb1 = queue.poll();count1--;while (count2 > 0) {temppcb2 = queue.poll();count2--;if (temppcb2.endblock < temppcb1.endblock)// 剩余时间片{queue.offer(temppcb2);} else {queue.offer(temppcb1);temppcb1 = new PCB();temppcb1 = temppcb2;}}queue.offer(temppcb1);count2 = queue.size() - 1;}}public void tempqueueInit(LinkedList<PCB> queue)// 临时队列的初始化{for (int i = 0; i <= 4; i++) {PCB temPcb = new PCB();temPcb.id = ids[i];temPcb.pri = pris[i];temPcb.cput = cputs[i];temPcb.allt = allts[i];temPcb.startblock = sts[i];temPcb.endblock = ens[i];temPcb.state = states[i];queue.offer(temPcb);}System.out.println("临时队列初始化成功");}public String initqueue(LinkedList<PCB> queue0, LinkedList<PCB> queue1, LinkedList<PCB> queue2) {PCB temppcb;while ((temppcb = queue0.poll()) != null) {if (temppcb.state.equals("READY"))// 就绪队列初始化{queue1.offer(temppcb);} else // 阻塞队列初始化{queue2.offer(temppcb);}}readyqueueSort(queue1);blockqueueSort(queue2);return "队列初始化成功!";}public void inputQueue(LinkedList<PCB> queue) // 队列输出{LinkedList<PCB> tempqueue = new LinkedList<PCB>(queue);PCB temppcb;while ((temppcb = tempqueue.poll()) != null) {System.out.print(" " + temppcb.id + " ");System.out.print(temppcb.pri + " ");System.out.print(temppcb.cput + " ");System.out.print(temppcb.allt + " ");System.out.print(temppcb.startblock + " ");System.out.print(temppcb.endblock + " ");System.out.print(temppcb.state);System.out.println();}}public void input(DP sDp) {System.out.println("--------------------------------");System.out.println("RUNNING_PROCESS:");if (sDp.rpcb != null) {System.out.println(sDp.rpcb.id + " " + sDp.rpcb.pri + " " + sDp.rpcb.allt);} else {System.out.println("无进程在执行");}System.out.println("READY_QUEUE:");sDp.inputQueue(sDp.readyqueue);System.out.println("BLOCK_QUEUE:");sDp.inputQueue(sDp.blockqueue);System.out.println("OVER_QUEUE:");sDp.inputQueue(sDp.overqueue);}public boolean judgePri(LinkedList<PCB> queue)// 判断是否后来优先权更大{LinkedList<PCB> tempqueue = new LinkedList<PCB>(queue);PCB tempPcb = tempqueue.poll();if (tempPcb != null) {if (tempPcb.pri > rpcb.pri) {return true;} else {return false;}} else {return false;}}public void readyqueueUpdate(LinkedList<PCB> queue)// 就绪队列更新{int count = queue.size();PCB tempPcb;while (count > 0) {count--;tempPcb = queue.poll();tempPcb.pri++;//待在就绪队列优先数加一queue.offer(tempPcb);}readyqueueSort(readyqueue);}public void blockqueueUpdate(LinkedList<PCB> queue)// 阻塞队列更新{int count = queue.size();PCB tempPcb;while (count > 0) {count--;tempPcb = queue.poll();tempPcb.endblock--;if (tempPcb.endblock == 0) {tempPcb.endblock = 3;tempPcb.state = "READY";readyqueue.offer(tempPcb);readyqueueSort(readyqueue);// 使得刚转移过来的进程参与优先权竞选} else {queue.offer(tempPcb);}}blockqueueSort(blockqueue);}public void stepupEnd() {if (rpcb.allt == 0) {rpcb.state = "OVER";overqueue.offer(rpcb);rpcb = readyqueue.poll();rpcb.state = "EXECUTE";rpcb.allt--;rpcb.cput++;rpcb.pri = rpcb.pri - 3;} else {rpcb.allt--;rpcb.cput++;rpcb.pri = rpcb.pri - 3;}}public void stepup() // 运行一个时间片(优先数减3){if (rpcb != null) {if (rpcb.allt == 0) {rpcb.state = "OVER";overqueue.offer(rpcb);rpcb = readyqueue.poll();rpcb.state = "EXECUTE";rpcb.allt--;rpcb.cput++;rpcb.pri = rpcb.pri - 3;} else {if (judgePri(readyqueue)) {rpcb.state = "BLOCK";blockqueue.offer(rpcb);rpcb = readyqueue.poll();rpcb.state = "EXECUTE";}rpcb.allt--;rpcb.cput++;rpcb.pri = rpcb.pri - 3;}readyqueueUpdate(readyqueue);blockqueueUpdate(blockqueue);} else {rpcb = readyqueue.poll();rpcb.allt--;rpcb.cput++;rpcb.pri = rpcb.pri - 3;rpcb.state = "EXECUTE";System.out.println("哈哈");readyqueueUpdate(readyqueue);blockqueueUpdate(blockqueue);}}public static void main(String[] args) {// TODO Auto-generated method stubDP sDp = new DP();sDp.tempqueueInit(sDp.tempqueue);System.out.println(sDp.initqueue(sDp.tempqueue, sDp.readyqueue, sDp.blockqueue));System.out.println("READY_QUEUE:");sDp.inputQueue(sDp.readyqueue);System.out.println("BLOCK_QUEUE:");sDp.inputQueue(sDp.blockqueue);System.out.println("OVER_QUEUE:");sDp.inputQueue(sDp.overqueue);PCB temPcb;LinkedList<PCB> queue = new LinkedList<PCB>(sDp.readyqueue);int count = 0;while ((temPcb = queue.poll()) != null) {sDp.stepup();count++;System.out.println("第" + count + "个时间片后:");sDp.input(sDp);System.out.println();queue = new LinkedList<PCB>(sDp.readyqueue);}while (sDp.rpcb.allt != 0) {sDp.stepupEnd();count++;System.out.println("第" + count + "个时间片后:");sDp.input(sDp);System.out.println();}sDp.rpcb.state = "OVER";sDp.overqueue.offer(sDp.rpcb);sDp.rpcb = null;count++;System.out.println("第" + count + "个时间片后:");sDp.input(sDp);}}
- java实现动态优先权算法
- 操作系统--动态高优先权算法的Java实现
- java按优先权调度进程算法实现
- java按优先权调度进程算法实现
- 动态优先权进程调度算法
- 静态优先权和动态优先权
- 使用动态优先权的进程调度算法的模拟
- 使用动态优先权的进程调度算法的模拟
- 优先权调度算法
- 优先权队列的实现
- 操作系统——动态高优先权优先调度算法(无到达时间)
- 操作系统——使用动态优先权的进程调度算法的模拟
- 操作系统-使用动态优先权的进程的调度算法的模拟
- 《C++实现数据结构》:优先权队列
- java最小堆实现优先权队列和求最大的n个数问题
- OS 使用动态优先权的进程调度
- 处理机调度模拟(优先权调度算法)
- 基于优先权的时间片轮转算法
- 任务管理
- 在Spring Boot中输出REST资源
- 苹果手机网页上点击数字可能拨打电话的解决办法
- spring mvc:expected at least 1 bean which qualifies as autowire candidate for this depende
- “浅拷贝”与“深拷贝”
- java实现动态优先权算法
- 基于TINY4412的Andorid开发-------简单的LED灯控制
- kmalloc&vmalloc&malloc
- android studio生成aar包并在其他工程引用aar包
- 【sqlserver】获取指定日期的上个季度的第一天和最后一天
- 油田大数据与创新之路的探究
- [00013] 通过某个关键字,排序一个字典列表
- easygui系列之-打开文件显示内容
- void (*signal(int signo, void (*func) (int))) (int) 理解