实验目的
代码还有一些BUG,会慢慢改过来!也请大家指教!
通过动态优先权算法的模拟加深对进程概念和进程调度过程的理解。
实验内容
(1)用C语言来实现对N个进程采用动态优先算法的进程调度;(2)每个用来标识进程的进程控制块 PCB用结构来描述,包括以下字段:
进程标识符id
进程优先数priority,并规定优先数越大的进程,其优先权越高;
进程已占用的CPU时间cputime ;
进程还需占用的CPU时间alltime,当进程运行完毕时,alltime变为0;
进程的阻塞时间startblock,表示当进程再运行startblock个时间片后,进程将进入阻塞状态;
进程被阻塞的时间blocktime,表示已阻塞的进程再等待blocktime个时间片后,将转换成就绪态 进程状态state;
队列指针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
实现代码:
import java.util.Queue;
import java.util.LinkedList;//队列类
import java.util.Scanner;
class PCB{
public int id; //进程ID
public 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={0,0,0,0,0};
public int[] ens={3,3,3,3,3};
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)//临时队列的初始化1
// {
// String controlString="";
// Scanner inScanner=new Scanner(System.in);
//
// while(!controlString.equals("stop"))
// {
// PCB temPcb=new PCB();
// temPcb.id=inScanner.nextInt();
// temPcb.pri=inScanner.nextInt();
// temPcb.cput=inScanner.nextInt();
// temPcb.allt=inScanner.nextInt();
// temPcb.startblock=inScanner.nextInt();
// temPcb.endblock=inScanner.nextInt();
// temPcb.state=inScanner.nextLine();
// queue.offer(temPcb);
//
// System.out.println("请输入控制字段:");
// controlString=inScanner.nextLine();
//
// }
// System.out.println("临时队列初始化成功");
//
// }
public void tempqueueInit(LinkedList<PCB> queue)//临时队列的初始化2
{
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() //运行一个时间片
{
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 stub
DP 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);
}
}
代码还有一些BUG,会慢慢改过来!也请大家指教!