java 实现进程调度




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<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={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<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)//临时队列的初始化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("就绪队列:");  
        sDp.inputQueue(sDp.readyqueue);  
        System.out.println("阻塞队列:");  
        sDp.inputQueue(sDp.blockqueue);  
        System.out.println("完成的队列:");  
        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";  
              
            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<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);  
    }  
}

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现了如下四种调度算法的模拟: (1)时间片轮转调度 (2)优先数调度 (3)最短进程优先 (4)最短剩余时间优先 模拟过程使用了JProgressBar作为进程状态条,更为直观地观察到每个进程的执行状态。 程序用户说明: 1、在上图标号1处输入要创建随机进程的个数,仅可输入正数,非正数会有相关提示。然后点击标号2处的“创建进程”按钮,随进创建的进程显示在程序界面的中央窗口,如标号3所示。 2、创建好随机进程后,在标号4的单选框选择将要模拟执行的调度算法,然后点击标号5处的“开始模拟”,程序开始执行。标号3的列表会显示相应的调度变化。 3、模拟过程中,可以继续添加新的进程,操作同上。 4、 一个算法模拟执行完毕之后,可以点击标号6的“复位”按钮,可以重置列表的内容为程序模拟运行前的内容。复位成功后,可以继续选择其他调度算法进行模拟。 5、标号7显示为程序模拟过程中的时间,从1秒开始累计。 6、点击标号8的“清空”按钮,可以清空类别的进程,以便程序的下次执行。 题目要求: 题目四 单处理系统的进程调度 一 、 课 程 设 计 目 的 1. 加深对进程概念的理解, 明确进程和程序的区别。 2. 深入了解系统如何组织进程、 创建进程。 3. 进一步认识如何实现处理调度。 二 、 课 程 设 计 内 容 编写程序完成单处理系统中的进程调度, 要求实现时间片轮转、 优先数、 最短进程优 先和最短剩余时间优先四种调度算法。 实验具体包括: 首先确定进程控制块的内容, 进程控 制块的组成方式; 然后完成进程创建原语和进程调度原语; 最后编写主函数对所作工作进行 测试。 模拟程序只对你所设置的“ 虚拟 PCB” 进行相应的调度模拟操作, 即每发生“ 调度” 时, 显示出当前运行进程的“ 进程标识符”、“ 优先数”、“ 剩余运行时间” 等, 而不需要对系 统中真正的 PCB 等数据进行修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值