Java模拟进程调度FIFO先进先出,SJF最短时间优先,RR时间片轮换以及HRRN最高响应比算法

本文主要是介绍Java模拟进程调度FIFO先进先出,SJF最短时间优先,RR时间片轮换以及HRRN最高响应比算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

进程调度算法模拟

本次操作系统试验是使用程序来模拟操作系统中进程调度的不同的调度策略,分别为FIFO先进先出,SJF最短时间优先,RR时间片轮换以及HRRN最高响应比算法。
模拟的情况下,进程数为8,进程所需执行时间为随机产生的整数,单位为S,默认进程同时到达。

工程结构

结构

  • process类用于测试不同算法
  • FIFO先进先出
  • SJF最短时间优先
  • RR时间片轮换
  • HRRN最高响应比算法

代码:

package process;import java.util.ArrayList;
import java.util.List;public class Process {public static List<double[]> taskInfo = new ArrayList<double[]>();//进程列表public static int taskNum = 8;//进程数目public static  void initTask(){for(int i=0;i<taskNum;i++){double[] t=new double[4];t[0]=i;//进程号t[1]=0;//到达时间t[2]=0;//响应比t[3]=(int)(Math.random()*100)%20+1;//需要运行时间taskInfo.add(t);}}public static void main(String[] args) {// TODO Auto-generated method stubProcess.initTask();System.out.println("最短作业优先================================================");SJF sjf = new SJF();sjf.initTask(taskInfo,taskNum);sjf.SJFShow();//最短作业优先System.out.println("时间片轮转调度================================================");RR rr = new RR();rr.initTask(taskInfo);rr.circleTime();System.out.println("最高相应比策略================================================");HRRN hrrn = new HRRN();hrrn.initTask(taskInfo,taskNum);hrrn.HRRNShow();System.out.println("先进先出策略================================================");FIFO fifo = new FIFO();fifo.initTask(taskInfo);fifo.FIFOShow();}}
package process;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;public class FIFO {private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");private int taskNum = 8;public ArrayBlockingQueue<double[]> taskQueue = new ArrayBlockingQueue<>(taskNum);private List<double[]> executeTime = new ArrayList<>();public void FIFOShow(){try {while(true){double [] task = new double[4];task = taskQueue.take();int taskNo = (int) task[0];int CurrentTaskTime = (int)task[3];System.out.println(timeFormat.format(new Date())+":第"+taskNo+"任务开始执行……");Thread.sleep(CurrentTaskTime*1000);System.out.println(timeFormat.format(new Date())+"结束执行   用时:"+CurrentTaskTime+"s");double []executeTask = new double[2];executeTask[0] = taskNo;executeTask[1] = System.currentTimeMillis()-task[1];executeTime.add(executeTask);if(taskQueue.size()==0){break;}}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}showTime();}public void showTime() {// TODO Auto-generated method stubdouble sumTime = 0;for (int i = 0; i < executeTime.size(); i++) {double []task = executeTime.get(i);System.out.println("task:"+(int)task[0]+":周转周期 = "+(int)(task[1]/1000)+"s");sumTime += task[1];}System.out.println("使用先进先出策略,平均周转周期为:"+(int)(sumTime/executeTime.size()/1000)+"s");}public void initTask(List<double []> initTask)//初始化进程列表{for(int i=0;i<initTask.size();i++){double [] task=initTask.get(i);task[1] = System.currentTimeMillis();//获得进程到达时间try {taskQueue.put(task);} catch (InterruptedException e) {e.printStackTrace();}}}
}
package process;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class SJF {private int taskNum = 8;public List<double[]> taskTime = new ArrayList<>();//private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");private List<double[]> executeTime = new ArrayList<>();public void SJFShow(){for (int i = 0; i < taskNum; i++) {try {double [] task = get_task(taskTime);System.out.println(timeFormat.format(new Date())+":第"+(int) task[0]+"任务开始执行……");Thread.sleep(1000*(int) task[3]);System.out.println(timeFormat.format(new Date())+"执行完成      用时:"+(int) task[3]+"s");double executeTime = System.currentTimeMillis()-task[1];double [] e = new double[2];e[0] = (int)task[0];e[1] = executeTime;this.executeTime.add(e);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}showTime();}public void showTime() {// TODO Auto-generated method stubdouble sumTime = 0;for (int i = 0; i < executeTime.size(); i++) {double []task = executeTime.get(i);System.out.println("task:"+(int)task[0]+"周转时间:"+(int)(task[1]/1000)+"s");sumTime+=task[1];}System.out.println("使用最短作业优先策略,平均周转时间为:"+(int)(sumTime/executeTime.size()/1000)+"s");}public double[] get_task(List<double[]> taskTime2) {// TODO Auto-generated method stubdouble [] task = new double[4];double smallestTime = 50;//运行时间int temp = -1;for (int i = 0; i < taskTime2.size(); i++) {if(taskTime2.get(i)[3] < smallestTime){smallestTime = taskTime2.get(i)[3];temp = i;}}task = taskTime2.get(temp);taskTime2.remove(temp);return task;}public void initTask(List<double[]> taskInfo, int taskNum){this.taskNum = taskNum;for (int i = 0; i < this.taskNum; i++) {double [] task = taskInfo.get(i);task[1] = System.currentTimeMillis();this.taskTime.add(task);}}}
package process;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;public class RR {private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");private int taskNum = 8;private int circleSize = 4;public ArrayBlockingQueue<double[]> taskQueue = new ArrayBlockingQueue<>(taskNum);private List<double[]> executeTime = new ArrayList<>();public void circleTime(){try {while(true){double [] task = new double[4];task = taskQueue.take();int CurrentTaskTime = (int)task[3];int taskNo = (int)task[0];System.out.println(timeFormat.format(new Date())+":第"+taskNo+"任务开始执行……");if(CurrentTaskTime < circleSize){//如果可以在时间片内运行Thread.sleep(CurrentTaskTime*1000);System.out.println(timeFormat.format(new Date())+"结束执行   用时:"+CurrentTaskTime+"s");double []executeTask = new double[2];executeTask[0] = taskNo;executeTask[1] = System.currentTimeMillis()-task[1];executeTime.add(executeTask);}else{//不能在本时间片中运行task[3] = task[3] - circleSize;taskQueue.put(task);Thread.sleep(circleSize*1000);System.out.println(timeFormat.format(new Date())+"本时间片用完,进程等待");}if(taskQueue.size() == 0){break;}}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}showTime();}public void showTime() {// TODO Auto-generated method stubdouble sumTime = 0;for (int i = 0; i < executeTime.size(); i++) {double []task = executeTime.get(i);System.out.println("task:"+(int)task[0]+":周转周期 = "+(int)(task[1]/1000)+"s");sumTime += task[1];}System.out.println("使用时间片轮转策略,平均周转周期为:"+(int)(sumTime/executeTime.size()/1000)+"s");}public void initTask(List<double []> initTask)//初始化进程列表{for(int i=0;i<initTask.size();i++){double [] task=initTask.get(i);task[1] = System.currentTimeMillis();//获得进程到达时间try {taskQueue.put(task);} catch (InterruptedException e) {e.printStackTrace();}}}}
package process;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class HRRN {private  static SimpleDateFormat timeFormat= new SimpleDateFormat("HH:mm:ss");public  static List<double []> taskInfo=new ArrayList<>();//进程信息列表public static  int task_num=8;//进程数private static List<double[]> executeTime = new ArrayList<>();//进程周转时间列表public void HRRNShow(){for (int i = 0; i < task_num; i++) {getRatio();double []currentTask = getATaskOfHign();System.out.println(timeFormat.format(new Date())+"第"+(int)currentTask[0]+"任务正在执行……");try {Thread.sleep((long) (currentTask[3]*1000));} catch (InterruptedException e) {e.printStackTrace();}System.out.println(timeFormat.format(new Date())+"任务运行结束  用时: "+currentTask[3]+"s");double [] execTask = new double[4];execTask[0] = currentTask[0];execTask[1] = System.currentTimeMillis()-currentTask[1];executeTime.add(execTask);}showTime();}public void showTime() {// TODO Auto-generated method stubdouble sumTime = 0;for (int i = 0; i < executeTime.size(); i++) {double [] temp = executeTime.get(i);System.out.println("task:"+(int)temp[0]+"周转时间="+(int)(temp[1]/1000)+"s");sumTime+=temp[1];}System.out.println("使用最高相应比策略,平均周转时间"+(int)(sumTime/executeTime.size()/1000)+"s");}private double[] getATaskOfHign() {// TODO Auto-generated method stubdouble [] tempTask = new double[4];double max_ratio = 0;int No = -1;for (int i = 0; i < taskInfo.size(); i++) {if (taskInfo.get(i)[2] > max_ratio) {tempTask = taskInfo.get(i);max_ratio = taskInfo.get(i)[2];No = i;}}taskInfo.remove(No);return tempTask;}public void getRatio() {// TODO Auto-generated method stubfor (int i = 0; i < taskInfo.size(); i++) {double []task = taskInfo.get(i);taskInfo.remove(i);double ratio = (System.currentTimeMillis()-task[1])/task[3]+1;task[2] = ratio;taskInfo.add(task);}}public void initTask(List<double[]> taskInfo,int tn){task_num=tn;for(int i=0;i<taskInfo.size();i++){double[] task=taskInfo.get(i);task[1]=System.currentTimeMillis();//获得进程到达时间HRRN.taskInfo.add(task);}}
}

这篇关于Java模拟进程调度FIFO先进先出,SJF最短时间优先,RR时间片轮换以及HRRN最高响应比算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/486890

相关文章

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads