操作系统程序调度算法——先来先服务(FCFS),时间片(RR),短作业(SJF),高响应比(HRRN)java实现

操作系统实验——程序调度

操作系统实验要求写程序调度,百度查了一下,借鉴了一位老哥的先来先服务,自己写了后面的

先来先服务

只要写一个循环,把process集合中的每个process取出,把process结束时间置为当前时间加上服务时间,
然后当前时间加上服务时间即可。

    private void calc() {
        int timeNow = 0;
        Process opProcess;

        for (int i = 0; i < processNumber; i++) {
            opProcess = processList.get(i);

            int waitTime = timeNow - opProcess.getArrivalTime();
            int completionTime = timeNow + opProcess.getServicesTime();
            int turnAroundTime = completionTime
                    - opProcess.getArrivalTime();
            double turnAroundTimeWithWeight = (double) turnAroundTime
                    / opProcess.getServicesTime();

            opProcess.setStartTime(timeNow);
            opProcess.setWaitTime(waitTime);
            opProcess.setCompletionTime(completionTime);
            opProcess.setTurnAroundTime(turnAroundTime);
            opProcess.setTurnAroundTimeWithWeight(
                    turnAroundTimeWithWeight);

            timeNow += opProcess.getServicesTime();
        }
    }

这是那老哥博客
[link]https://www.cnblogs.com/daheww/p/10154455.html

时间片轮转法

运用一个队列,存储所有等待队列,每次循环先把后来的加入到队尾,再把运行完的加入到队尾,只要进程数大于零且队列不为空,取出队首元素。当前进程剩余运行时间等于总需要服务时间-时间片。然后判断剩余运行时间是否小于等于0.小于等于则结束时间等于当前时间加上进循环之前的剩余服务时间。进程数-1。

private void calc() {
        int timeNow = 0;
        Queue queue=new LinkedList();
        int processRemain = processNumber;
        boolean noProcessRunInThisTurn=true;
        Process opProcess;Process linshi=null;
        int extime=0;


        while (processRemain != 0) {

            if(arrtime.contains(timeNow)){
                queue.add(processList.get(arrtime.indexOf(timeNow)));
            }
            if(timeNow==extime&&linshi!=null){
                noProcessRunInThisTurn=true;
                if(timeNow!=0&&linshi.getRemainServiceTime()>0)
                    queue.add(linshi);
            }
            if(noProcessRunInThisTurn&&!queue.isEmpty()) {
                opProcess = (Process) queue.poll();
                linshi=opProcess;

                if ((opProcess.getRemainServiceTime() > 0)
                        && (timeNow >= opProcess.getArrivalTime())) {
                    // First time
                    if (opProcess.getServicesTime() == opProcess
                            .getRemainServiceTime()) {
                        int waitTime = timeNow - opProcess.getArrivalTime();

                        opProcess.setStartTime(timeNow);
                        opProcess.setWaitTime(waitTime);
                    }
                    noProcessRunInThisTurn = false;

                    // Calculating remain service time
                    int rs=opProcess.getRemainServiceTime();

                    int remainServiceTime = rs - timeSlice;
                    opProcess.setRemainServiceTime(remainServiceTime);

                    // Last time
                    if (remainServiceTime == 0) {
                        int completionTime = timeNow + timeSlice; // The process ends when the current slice is completed.
                        int turnAroundTime = completionTime
                                - opProcess.getArrivalTime();
                        double turnAroundTimeWithWeight = 1.0 * turnAroundTime
                                / opProcess.getServicesTime();

                        opProcess.setCompletionTime(completionTime);
                        opProcess.setTurnAroundTime(turnAroundTime);
                        opProcess.setTurnAroundTimeWithWeight(turnAroundTimeWithWeight);
                        processRemain--;
                        timeNow +=1;
                        extime +=timeSlice;

                    }
                    if (remainServiceTime < 0) {
                        int completionTime  ;   // The process ends when the current slice is completed.
                        completionTime = timeNow +  rs;
                        int turnAroundTime = completionTime
                                - opProcess.getArrivalTime();
                        double turnAroundTimeWithWeight = 1.0 * turnAroundTime
                                / opProcess.getServicesTime();

                        opProcess.setCompletionTime(completionTime);
                        opProcess.setTurnAroundTime(turnAroundTime);
                        opProcess
                                .setTurnAroundTimeWithWeight(turnAroundTimeWithWeight);
                        processRemain--;
                        timeNow += 1;
                        extime+=rs;

                    }
                    if (remainServiceTime > 0) {
                        timeNow += 1;
                        extime+=timeSlice;

                    }


//                    System.out.println("    #STEP# Process" + (i + 1)
//                            + " remain service time:"
//                            + opProcess.getRemainServiceTime()
//                            + " , timeBefore:" + (timeNow - 1) + ", timeNow:"
//                            + timeNow
//                            + ((remainServiceTime <= 0) ? " Finish" : ""));
                }
            }
            else {
                timeNow++;
//                extime++;
            }


            // Means no process could run, because they have arrived.

        }
    }

短作业

短作业很简单就不详细说了直接放代码

 private void calc() {
        int timeNow = 0;
        Process opProcess;
        sertimecomparator com=new sertimecomparator();
        PriorityQueue<Process> pr=new PriorityQueue(10,com);

        boolean extime=true;boolean run=true;
        int endtime=0;
        while(run){
            if(arrtime.contains(timeNow)){
                if(!pr.contains(processList.get(arrtime.indexOf(timeNow)))){
                    pr.add(processList.get(arrtime.indexOf(timeNow)));
                }

            }
            if(pr.isEmpty()){
                run=false;
                break;
            }
            if(timeNow==endtime)extime=true;
            if(extime){
                extime=false;
                opProcess=pr.poll();
                int waitTime = timeNow - opProcess.getArrivalTime();
                int completionTime = timeNow + opProcess.getServicesTime();
                int turnAroundTime = completionTime
                        - opProcess.getArrivalTime();
                double turnAroundTimeWithWeight = (double) turnAroundTime
                        / opProcess.getServicesTime();

                opProcess.setStartTime(timeNow);
                opProcess.setWaitTime(waitTime);
                opProcess.setCompletionTime(completionTime);
                opProcess.setTurnAroundTime(turnAroundTime);
                opProcess.setTurnAroundTimeWithWeight(
                        turnAroundTimeWithWeight);

                timeNow += 1;
                endtime += opProcess.getServicesTime();
            }
            else{
                timeNow++;

            }

        }


    }
    static class sertimecomparator implements Comparator<Process> {

        @Override
        public int compare(Process o1, Process o2) {
            if(o1.getServicesTime()<o2.getServicesTime()){
                return -1;
            }
            if(o1.getServicesTime()>o2.getServicesTime()){
                return 1;
            }
            else return 0;
        }
    }

高响应比

高响应比就是设了一个优先级,优先级等于等待时间+要求服务时间/要求服务时间。
这个算法需要用到优先队列,优先队列就是优先级最高的在队首,每插入元素一个自动更新队列。
后面就跟前面差不多,判断当前时间有没有到达进程,有则加入优先队列,没有则时间加一
然后取出队首元素,结束时间等于当前时间加 服务时间,

private void calc() {
        int timeNow = 0;
        Process opProcess;
        sertimecomparator com=new sertimecomparator();
        PriorityQueue<Process> pr=new PriorityQueue(10,com);
        boolean extime=true;boolean run=true;
        int endtime=0;
        while(run){
            if(arrtime.contains(timeNow)){
                if(!pr.contains(processList.get(arrtime.indexOf(timeNow)))){
                    pr.add(processList.get(arrtime.indexOf(timeNow)));
                }
            }
            for(int x=0;x<pr.size();x++){
                Process p=pr.poll();
                p.setWaitTime(timeNow-p.getArrivalTime());
                pr.offer(p);
            }

            if(pr.isEmpty()){
                run=false;
                break;
            }
            if(timeNow==endtime)extime=true;
            if(extime){
                extime=false;
                opProcess=pr.poll();
                int waitTime = timeNow - opProcess.getArrivalTime();
                int completionTime = timeNow + opProcess.getServicesTime();
                int turnAroundTime = completionTime
                        - opProcess.getArrivalTime();
                double turnAroundTimeWithWeight = (double) turnAroundTime
                        / opProcess.getServicesTime();

                opProcess.setStartTime(timeNow);
                opProcess.setWaitTime(waitTime);
                opProcess.setCompletionTime(completionTime);
                opProcess.setTurnAroundTime(turnAroundTime);
                opProcess.setTurnAroundTimeWithWeight(
                        turnAroundTimeWithWeight);

                timeNow += 1;
                endtime += opProcess.getServicesTime();
            }
            else{
                timeNow++;

            }

        }


    }
    static class sertimecomparator implements Comparator<Process> {

        @Override
        public int compare(Process o1, Process o2) {
            if((o1.getServicesTime()+o1.getWaitTime())/o1.getServicesTime()
                    <(o2.getServicesTime()+o2.getWaitTime())/o2.getServicesTime()){
                return 1;
            }
            if((o1.getServicesTime()+o1.getWaitTime())/o1.getServicesTime()
                    >(o2.getServicesTime()+o2.getWaitTime())/o2.getServicesTime()){
                return -1;
            }
            else return 0;
        }
    }

写的不好,欢迎大家交流。

你可能感兴趣的:(实验)