Java简单实现——操作系统进程调度算法

文章目录

实现功能

简单实现四种进程调度算法:

  1. 先来先服务算法(FCFS)
  2. 短作业优先算法(SJF)
  3. 时间片轮转算法(RR)
  4. 高响应比优先调度算法(HRRN)

源代码

  1. Process class
package com.company.schedulingalgorithm;

/** * @author hudongsheng * @date 2020/12/14 - 20:25 */
public class Process { 
    //进程名
    private String peocessName;
    //进程到达时间
    private int arriveTime;
    //进程所需要的服务时间
    private int serviceTime;
    //开始时间
    private int startTime;
    //完成时间
    private int finishTime;
    //周转时间
    private int turnoverTime;


    public String getPeocessName() { 
        return peocessName;
    }

    public void setPeocessName(String peocessName) { 
        this.peocessName = peocessName;
    }

    public int getArriveTime() { 
        return arriveTime;
    }

    public void setArriveTime(int arriveTime) { 
        this.arriveTime = arriveTime;
    }

    public int getServiceTime() { 
        return serviceTime;
    }

    public void setServiceTime(int serviceTime) { 
        this.serviceTime = serviceTime;
    }

    public int getStartTime() { 
        return startTime;
    }

    public void setStartTime(int startTime) { 
        this.startTime = startTime;
    }

    public int getFinishTime() { 
        return finishTime;
    }

    public void setFinishTime(int finishTime) { 
        this.finishTime = finishTime;
    }

    public int getTurnoverTime() { 
        return turnoverTime;
    }

    public void setTurnoverTime(int turnoverTime) { 
        this.turnoverTime = turnoverTime;
    }

    @Override
    public String toString(){ 
        StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("进程名:").append(peocessName).append(" ")
                        .append("到达时间:").append(arriveTime).append(" ")
                        .append("需要的服务时间:").append(serviceTime).append(" ")
                        .append("开始时间:").append(startTime).append(" ")
                        .append("完成时间:").append(finishTime).append(" ")
                        .append("周转时间:").append(turnoverTime).append(" ");
        return stringBuilder.toString();
    }
}

  1. SchedulingAlgorithm class (主类)
package com.company.schedulingalgorithm;

import java.util.Scanner;

/** * @author hudongsheng * @date 2020/12/9 - 21:00 * 进程调度算法 */
public class SchedulingAlgorithm { 
    Scanner scanner = new Scanner(System.in);
    //进程存储
    private Process[] processes;
    //进程数
    private int size;


    //初始化进程
    public void  getProcess(){ 
        //获取进程数
        System.out.println("请输入进程数目:");
        size = scanner.nextInt();
        //初始化进程数组
        processes = new Process[size];
        //获取进程
        for (int i = 0; i < size; i++) { 
            Process process = new Process();
            System.out.println("请输入第"+(i+1)+"个进程的名字 到达时间 服务时间");
            process.setPeocessName(scanner.next());
            process.setArriveTime(scanner.nextInt());
            process.setServiceTime(scanner.nextInt());
            processes[i] = process;
        }
    }
    //选择相应的调度算法
    public void selectDispatching(){ 

        while (true){ 
            System.out.println("---------------");
            System.out.println("1.先来先服务");
            System.out.println("2.短作业");
            System.out.println("3.时间片轮转");
            System.out.println("4.高响应比");
            System.out.println("5.退出");
            System.out.println("---------------");
            int chioce;
            System.out.print("请输入选择的业务:");
            chioce = scanner.nextInt();
            //判断输入合法性
            while (chioce <= 0 || chioce > 5){ 
                System.out.println("无此选项请重新输入:");
                chioce = scanner.nextInt();
            }
            if(chioce == 5){ 
                return;
            }
            switch (chioce){ 
                case 1:
                    FCFSDispatching.fcfsDispatching(processes);
                    break;
                case 2:
                    SJFDispatching.sjfDispatching(processes);
                    break;
                case 3:
                    RRDispatching.rrDispatching(processes);
                    break;
                case 4:
                    HRRNDispatching.hrrnDispatching(processes);
                    break;
            }
        }
    }
}
  1. Test class (测试类)
    package com.company.bankeralgorithm;

/**

  • @author hudongsheng
  • @date 2020/12/16 – 19:14
    */
    public class Test {
    public static void main(String[] args) {
    BankerAlgorithm banker = new BankerAlgorithm();
    banker.start();
    }
    }
  1. FCFSDispatching class
public class FCFSDispatching { 
    public static void fcfsDispatching(Process[] processes){ 
        //时间片计数器
        int time = 0;
        //运行
        for (int i = 0; i < processes.length; i++) { 
            processes[i].setStartTime(time);
            processes[i].setFinishTime(processes[i].getServiceTime()+time);
            processes[i].setTurnoverTime(processes[i].getFinishTime() - processes[i].getArriveTime());
            time += processes[i].getServiceTime();
            System.out.println(processes[i].toString());
        }
        System.out.println("进程完成总时间:"+time);
    }
}
  1. SJFDispatching class
package com.company.schedulingalgorithm;

/** * @author hudongsheng * @date 2020/12/14 - 20:23 * 短作业优先 */
public class SJFDispatching { 

    public static void sjfDispatching(Process[] processes){ 
        //运行时间
        int time = 0;
        //根据进程的服务时间对进程排序
        processes = hillSort(processes);
        //运行
        for (int i = 0; i < processes.length; i++) { 
            processes[i].setStartTime(time);
            processes[i].setFinishTime(processes[i].getServiceTime()+time);
            processes[i].setTurnoverTime(processes[i].getFinishTime() - processes[i].getArriveTime());
            time += processes[i].getServiceTime();
            System.out.println(processes[i].toString());
        }
        System.out.println("进程完成总时间:"+time);
    }


    //排序
    public static Process[] hillSort(Process[] arrays){ 
        //初始增量
        int increment = arrays.length/2;
        //定义交换变量
        Process tmp = null;
        //增量为0时 排序完成
        while (increment != 0){ 
            //选择排序
            for (int i = increment; i < arrays.length; i++) { 
                tmp = arrays[i];
                int j;
                for (j = i-increment; j >= 0; j -= increment) { 
                    if(tmp.getServiceTime() >= arrays[j].getServiceTime()){ 
                        break;
                    }
                    arrays[j+increment] = arrays[j];
                }
                //将抽出元素插入
                arrays[j+increment] = tmp;
            }
            increment /= 2;
        }
        return arrays;
    }
}

  1. RRDispatching
package com.company.schedulingalgorithm;

/** * @author hudongsheng * @date 2020/12/14 - 20:21 * 时间片轮转法 */
public class RRDispatching { 

    public static void rrDispatching(Process[] processes){ 
        //运行时间
        int time = 0;
        //时间片大小
        int slice = 2;
        //进程运行需要时间片个数
        int[] array = new int[processes.length];
        //初始化每个进程所需要的时间片
        initTime(array,processes,slice);
        //所需要的时间片数
        int size = 0;
        for (int i = 0; i < array.length; i++) { 
            size += array[i];
        }
        //已用的时间片数
        int count = 0;
        //运行
        //第一次轮转 记录开始时间
        for (int i = 0; i < processes.length; i++) { 
            processes[i].setStartTime(time);
            time += slice;
            array[i] -= 1;
            count++;
        }
        //轮转
        while (count <= size){ 
            for (int i = 0; i < processes.length; i++) { 
                //执行完的程序不再轮转
                if(array[i] == -1){ 
                    continue;
                }
                if(array[i] > 0){ 
                    time += slice;
                    array[i] -= 1;
                    count++;
                }
                //判断进程是否执行完
                if (array[i] == 0){ 
                    //执行完设置完成时间
                    processes[i].setFinishTime(time);
                    processes[i].setTurnoverTime(processes[i].getFinishTime() - processes[i].getArriveTime());
                    //设置进程已结束标志
                    array[i] = -1;
                }
            }
            if(array[array.length-1] == -1){ 
                break;
            }
        }
        //打印结果
        for (int i = 0; i < processes.length; i++) { 
            System.out.println(processes[i].toString());
        }


    }

    //初始化进程需要的轮转片数
    public static void initTime(int[] array,Process[] processes,int slice){ 
        for (int i = 0; i < processes.length; i++) { 
            array[i] = processes[i].getServiceTime()/slice;
            if((processes[i].getServiceTime() % slice) != 0){ 
                array[i] += 1;
            }
        }
    }

}

  1. HRRNDispatching class
package com.company.schedulingalgorithm;

/** * @author hudongsheng * @date 2020/12/14 - 20:23 * 响应比高者优先(HRRN)调度算法:RP(响应比)=作业周转时间/作业运行时间=1+作业等待时间/作业运行时间 */
public class HRRNDispatching { 

    public static void hrrnDispatching(Process[] processes){ 
        //进程响应比
        float[] array = new float[processes.length];
        array = responseRatio(array,processes);
        //进程总服务时间
        int time = 0;
        //运行
        for (int i = 0; i < processes.length; i++) { 
            processes[i].setStartTime(time);
            processes[i].setFinishTime(processes[i].getServiceTime()+time);
            processes[i].setTurnoverTime(processes[i].getFinishTime() - processes[i].getArriveTime());
            time += processes[i].getServiceTime();
            System.out.println(processes[i].toString()+"响应比:"+array[i]);
        }
        System.out.println("进程完成总时间:"+time);
    }

    //计算进程响应比
    public static float[] responseRatio(float[] array,Process[] processes){ 
        for (int i = 0; i < array.length; i++) { 
            //等待时间
            float waitTime = 0;
            for (int j = 0; j < i; j++) { 
                waitTime += processes[j].getServiceTime();
            }
            waitTime -= processes[i].getArriveTime();
            //响应比
            array[i] =  ((float) 1.0 + (waitTime/(float) processes[i].getServiceTime()));
        }
        return array;
    }

    //根据响应比对进程运行顺序排序
    public static Process[] hillSort(float[] array,Process[] processes){ 
        //初始增量
        int increment = processes.length/2;
        //定义交换变量
        Process tmp = null;
        //增量为0时 排序完成
        while (increment != 0){ 
            //选择排序
            for (int i = increment; i < processes.length; i++) { 
                tmp = processes[i];
                int j;
                for (j = i-increment; j >= 0; j -= increment) { 
                    if(array[i] >= array[j]){ 
                        break;
                    }
                    processes[j+increment] = processes[j];
                }
                //将抽出元素插入
                processes[j+increment] = tmp;
            }
            increment /= 2;
        }
        return processes;
    }
}

总结

学校的操作系统实验,只是简单模拟,只实现了最基础的调度功能,其中交互功能,异常处理,非法处理并未实现,欢迎各位大佬指点。

    原文作者:北渺
    原文地址: https://blog.csdn.net/qq_43722914/article/details/111397176
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞