Java 操作系统 优先数算法

学校布置的作业 大家可以看来参考一下 代码量还是好多 一点都不简洁

欢迎评论......

Main函数

public class Main {
    public static void main(String[] args) {
        Cal pri = new Cal();
        pri.input();
    }
}

public class pcb1 {
    private String name ;
    private int cuptime = 0;//刚开始cpu占用时间为0
    private int needtime;
    private int priority = 50;//优先数 提前定义为50
    private String state;

    private int cputime;

    private int count = 0;

    public pcb1() {
    }

    public int getCputime() {
        return cputime;
    }

    public void setCputime(int cputime) {
        this.cputime = cputime;
    }


    public pcb1(String name, int needtime) {
        this.name = name;
        this.needtime = needtime;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCuptime() {
        return cuptime;
    }

    public void setCuptime(int cuptime) {
        this.cuptime = cuptime;
    }

    public int getNeedtime() {
        return needtime;
    }

    public void setNeedtime(int needtime) {
        this.needtime = needtime;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }


}

方法

public class Cal {

    public static void input() {
        Scanner sc = new Scanner(System.in);
        System.out.println("please input the number of processes:");
        int input = sc.nextInt();
        pcb1[] pcb = new pcb1[input];//创建pcb1数组

        List Wait = new ArrayList<String>();     //创建等待队列

        List Ready = new ArrayList<String>();     //创建就绪队列

        List Done = new ArrayList<String>(); //创建完成队列

        List Sout = new ArrayList<>();  //定义一个列表 用于排序

        System.out.println("input name and needtime:");
        int round = 2;//创建时间片大小
        int i;
        for (i = 0; i < input; i++) {
            String a = sc.next();//输入名字
            int b = sc.nextInt();//输入--需要的时间
            pcb[i] = new pcb1(a, b);//创建数组  按照input的数量
            //刚开始就给进程设置pri 方便下面运算
            pcb[i].setPriority(pcb[i].getPriority() - pcb[i].getNeedtime());
        }


        // 读取priority数值最大的对象进cup调度 先排序
        pcb1 temp = new pcb1();  //创建一个pcb对象
        int j;
        for (i = 0; i < input-1; i++) {    //排列,按照pri 大的排在前面
            for (j = 0; j < input-i-1 ; j++) {
                    if (pcb[j].getPriority() < pcb[j + 1].getPriority())//冒泡排序
                    {
                        temp = pcb[j];
                        pcb[j] = pcb[j + 1];
                        pcb[j + 1] = temp;
                    }

            }
        }

        for (int k = 0; k < input; k++) {
            Sout.add(pcb[k].getPriority());//排序好之后 往sout添加优先级
        }



        //往就绪队列中进行操作

        for (i = 0; i < input; i++) {

            Ready.add(pcb[i].getName());  //往就绪队列中添加名字 大家一开始就已经就绪了
            pcb[i].setState("W");      //就绪队列的状态设为 W
            //否则进入等待队列
            Wait.add(pcb[i].getName());   //往等待队列中添加名字 String类型
            pcb[i].setState("W");//等待队列的W
        }

        System.out.println("Name  cputime  needtime  count     pri     state");   //输出排序后的进程
        for (j = 0; j < input; j++) {
           //遍历一下 取出与排列数组索引对应的程序块
                    System.out.print("  " + pcb[j].getName() + "     ");//获取名字
                    System.out.print(pcb[j].getCputime() + "      ");//获取占用cpu的时间
                    System.out.print(pcb[j].getNeedtime() + "        ");//获取需要使用的时间
                    System.out.print(pcb[j].getCount() + "         ");//获取使用cpu的轮数
                    System.out.print(pcb[j].getPriority() + "         ");//输出时间片
                    System.out.println(pcb[j].getState());

        }
        System.out.println("就绪队列:" + Ready);
        System.out.println("完成队列:" + Done);

        //运行的次数等于?
        //当大家需要的时间都为0的时候,结束循环.
        while(true){
            check(input,Ready,Done,Sout,pcb,round);
            int c = 0;//这是一个伟大的算法 先定义一个变量用于计数
            for (j = 0; j < input; j++) {
                if(pcb[j].getNeedtime()==0){
                    c+=1;//如果所有的时间块的needtime都为0了,就可以结束了
                }//顶级理解
            }

            if(c==input){
                break;
            }
        }


    }


    //定义一个方法 用于真正的计算
    public static void check ( int input, List Ready, List Done, List Sout, pcb1[]pcb,int round){

        //先排序一次 按照pri把顺序由大到小排出来
        int j;//当状态为F的时候,就不用排他的序号了
        //这里运用的算法:排序方法为比较器的sort方法,原理是把pcb数列中各个状态为F的滞后,之后在比较pri的值

        List<pcb1> pcb1List = List.of(pcb);//创建一个pcb1的集合 不能被修改 把pcb数列排序进去
        ArrayList<pcb1> pcb2List = new ArrayList<>();//再创建一个pcb1的集合
        pcb2List.addAll(pcb1List);//把pcb1list里面所有的参数加入进去

        Comparator<pcb1> tComparator = (o1, o2) -> {//定义一个比较器 用于比较前后两者 两两比较 o2>o1
            if (o1.getState().equals("F")) {
                return 1;                        //如果返回正数 升序
            } else if (o2.getState().equals("F")) {
                return -1;                      //返回负数   倒叙
            }
            return o2.getPriority() - o1.getPriority();//else:相减 看返回值
        };
        Collections.sort(pcb2List, tComparator); //两两比较 pcb2List集合里面的东西
        pcb=pcb2List.toArray(pcb);


        //把第一个设置为就绪R 表示这个环节要用到他
            pcb[0].setState("R");



        System.out.println("Name  cputime  needtime  count     pri     state");   //输出排序后的进程
        for (j = 0; j < input; j++) {
            //遍历一下 取出与排列数组索引对应的程序块
            System.out.print("  " + pcb[j].getName() + "     ");//获取名字
            System.out.print(pcb[j].getCputime() + "      ");//获取占用cpu的时间
            System.out.print(pcb[j].getNeedtime() + "        ");//获取需要使用的时间
            System.out.print(pcb[j].getCount() + "         ");//获取使用cpu的轮数
            System.out.print(pcb[j].getPriority() + "         ");//输出时间片
            System.out.println(pcb[j].getState());

        }

        Ready.remove(pcb[0].getName());//在就绪队列删除掉 这个环节使用的pcb

        System.out.println("就绪队列:" + Ready);
        System.out.println("完成队列:" + Done);





        Integer realtime = 0;//定义一个变量 用于判断如果时间片没用完进程块就结束的情况 实际占用cpu的时间
        if (pcb[0].getNeedtime() - round >= 0) {
            realtime = round;
        } else if (pcb[0].getNeedtime() - round < 0) {
            realtime = pcb[0].getNeedtime();
        }
        //设置还需要的时间
        pcb[0].setNeedtime(pcb[0].getNeedtime() - realtime);//设置还需要的时间 减去时间片

        if (realtime == round) {
            pcb[0].setCputime(pcb[0].getCputime() + round);

        } else if (realtime == 1) {                //设置Cpu占用时间
            pcb[0].setCputime(pcb[0].getCputime() + realtime);
        }

        //每使用一次cpu就要pri - 3
        pcb[0].setPriority(pcb[0].getPriority()-3);

        if (pcb[0].getNeedtime() > 0) {
            Ready.add(pcb[0].getName());//如果需要时间仍然大于0 就重新添加回就绪队伍

            pcb[0].setState("W");
        } else {
            pcb[0].setState("F");//把状态设置为F 表示状态已经结束
            Ready.remove(pcb[0].getName());
            Done.add(pcb[0].getName());//如果时间=0了 那么就移出就绪队伍进入完成队伍
        }

        pcb[0].setCount(pcb[0].getCount()+1);//统计使用cpu的次数 加一

        if (Ready.size() == 0) {

            System.out.println("\n");
            System.out.println("最终结果:");
            System.out.println("Name  cputime  needtime  count     pri     state");   //输出排序后的进程
            for (j = 0; j < input; j++) {
                //遍历一下 取出与排列数组索引对应的程序块
                System.out.print("  " + pcb[j].getName() + "     ");//获取名字
                System.out.print(pcb[j].getCputime() + "      ");//获取占用cpu的时间
                System.out.print(pcb[j].getNeedtime() + "        ");//获取需要使用的时间
                System.out.print(pcb[j].getCount() + "         ");//获取使用cpu的轮数
                System.out.print(pcb[j].getPriority() + "         ");//输出时间片
                System.out.println(pcb[j].getState());

            }


            System.out.println("就绪队列:" + Ready);
            System.out.println("完成队列:" + Done);

        }


    }


    }

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值