java 实现进程调度
2019-07-14 12:42发布
生成海报
import java.util.LinkedList;
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 readyqueue=null; //就绪队列
public LinkedList blockqueue=null; //阻塞队列
public LinkedList tempqueue=null; //临时队列,存储初始队列;
public LinkedList overqueue=null; //执行完成的队列
public int[] ids={0,1,2,3,4};
public int[] pris={19,18,12,17,11};
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();
blockqueue=new LinkedList();
tempqueue=new LinkedList();
overqueue=new LinkedList();
}
public void readyqueueSort(LinkedList queue) //就绪队列优先权排序
{
PCB temppcb1,temppcb2,temppcb3;
int count1=queue.size();
int count2=queue.size()-1;
LinkedList queueQ=new LinkedList();
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 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 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 queue0,LinkedList queue1,LinkedList 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 queue) //队列输出
{
LinkedList tempqueue=new LinkedList(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("就绪队列:");
sDp.inputQueue(sDp.readyqueue);
System.out.println("阻塞队列:");
sDp.inputQueue(sDp.blockqueue);
System.out.println("完成的队列:");
sDp.inputQueue(sDp.overqueue);
}
public boolean judgePri(LinkedList queue)//判断是否后来优先权更大
{
LinkedList tempqueue=new LinkedList(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 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 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";
readyqueueUpdate(readyqueue);
blockqueueUpdate(blockqueue);
}
}
public static void main(String[] args) {
DP sDp=new DP();
sDp.tempqueueInit(sDp.tempqueue);
System.out.println(sDp.initqueue(sDp.tempqueue, sDp.readyqueue, sDp.blockqueue));
System.out.println("就绪队列:");
sDp.inputQueue(sDp.readyqueue);
System.out.println("阻塞队列:");
sDp.inputQueue(sDp.blockqueue);
System.out.println("完成的队列:");
sDp.inputQueue(sDp.overqueue);
PCB temPcb;
LinkedList queue=new LinkedList(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(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);
}
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮