java模拟进程调度

news/2024/10/31 6:28:52/

先来先服务+优先级调度+短作业优先调度+响应比优先调度

代码

java">import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;class Main {static class tasks{int id;//序号char jinchengname;//进程名int jinchengId;//double arriveTime;//提交时间double runningTime;//运行时间int youxianshu;//优先数double jinrushuchujingshijian;//进入输出#时间double startRunningTime;//开始运行时间double shangxvyunxingshijian;//尚需运行时间double runningEndTime;//运行结束时间double zhouzhuanshijian;//周转时间double saiquanzhouzhuanshijian;//带权周转时间int order;//运行次序double xingyingbi;//响应比public int getId() {return id;}public void setId(int id) {this.id = id;}public char getJinchengname() {return jinchengname;}public void setJinchengname(char jinchengname) {this.jinchengname = jinchengname;}public int getJinchengId() {return jinchengId;}public void setJinchengId(int jinchengId) {this.jinchengId = jinchengId;}public double getArriveTime() {return arriveTime;}public void setArriveTime(double arriveTime) {this.arriveTime = arriveTime;}public double getRunningTime() {return runningTime;}public void setRunningTime(double runningTime) {this.runningTime = runningTime;}public int getYouxianshu() {return youxianshu;}public void setYouxianshu(int youxianshu) {this.youxianshu = youxianshu;}public double getJinrushuchujingshijian() {return jinrushuchujingshijian;}public void setJinrushuchujingshijian(double jinrushuchujingshijian) {this.jinrushuchujingshijian = jinrushuchujingshijian;}public double getStartRunningTime() {return startRunningTime;}public void setStartRunningTime(double startRunningTime) {this.startRunningTime = startRunningTime;}public double getShangxvyunxingshijian() {return shangxvyunxingshijian;}public void setShangxvyunxingshijian(double shangxvyunxingshijian) {this.shangxvyunxingshijian = shangxvyunxingshijian;}public double getRunningEndTime() {return runningEndTime;}public void setRunningEndTime(double runningEndTime) {this.runningEndTime = runningEndTime;}public double getZhouzhuanshijian() {return zhouzhuanshijian;}public void setZhouzhuanshijian(double zhouzhuanshijian) {this.zhouzhuanshijian = zhouzhuanshijian;}public double getSaiquanzhouzhuanshijian() {return saiquanzhouzhuanshijian;}public void setSaiquanzhouzhuanshijian(double saiquanzhouzhuanshijian) {this.saiquanzhouzhuanshijian = saiquanzhouzhuanshijian;}public int getOrder() {return order;}public void setOrder(int order) {this.order = order;}public tasks(int id, char jinchengname, int jinchengId, double arriveTime, double runningTime, int youxianshu) {this.id = id;this.jinchengname = jinchengname;this.jinchengId = jinchengId;this.arriveTime = arriveTime;this.runningTime = runningTime;this.youxianshu = youxianshu;}public double getXingyingbi() {return xingyingbi;}public void setXingyingbi(double xingyingbi) {this.xingyingbi = xingyingbi;}}public static void main(String[] args) {List<tasks> tasksList=new ArrayList<>();System.out.println("请输入task个数");Scanner sc=new Scanner(System.in);int n=sc.nextInt();for (int i = 0; i < n; i++) {System.out.println("请依次输入第"+(i+1)+"个进程的序号 进程名 进程号 到达时间 运行时间 优先数");int id=sc.nextInt();char jingchengname=sc.next().charAt(0);int jingchengid= sc.nextInt();double arrivetime= sc.nextDouble();double runningtime= sc.nextDouble();int youxianshu= sc.nextInt();tasksList.add(new tasks(id,jingchengname,jingchengid,arrivetime,runningtime,youxianshu));}//选择调度System.out.println("1.先来先服务");System.out.println("2.优先级调度");System.out.println("3.短作业优先调度");System.out.println("4.响应比高者优先调度");int ch=sc.nextInt();switch (ch){case 1:xianlaixianfuwu(tasksList);break;case 2:youxianjidiaodu(tasksList);break;case 3:duanzuoyeyouxian(tasksList);break;case 4:xiangyingbigaoyouxian(tasksList);break;}}public static void xianlaixianfuwu(List<tasks> tasksList) {//先来先服务Comparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);int order=0;double time=tasksList.get(0).arriveTime;//此时时间点for (tasks tasks : tasksList) {order++;tasks.setOrder(order);if(time>= tasks.arriveTime){tasks.startRunningTime=time;}else {time=tasks.arriveTime;//这里少些一句这个else 才看到 补上了}time+= tasks.runningTime;tasks.runningEndTime=time;tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}public static void youxianjidiaodu(List<tasks> tasksList) {List<tasks> tasksyiwanchengList=new ArrayList<>();//找最先的到达的作为时间time的开始,完成后更新time+=runningtimeComparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);//tasksyiwanchangList.add(task001)tasksyiwanchengList.add(tasksList.get(0));double time=tasksList.get(0).arriveTime;//此时时间点tasksList.get(0).startRunningTime=time;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;//tasksList.del(task001)删除最先的到达的tasktasksList.remove(0);//list.for执行下面这行的操作//在剩下的task里&&arrivetime<time也就是已经到达但还没开始的任务里,选执行优先级最高的执行,更新time+=runningtimeint n= tasksList.size();for (int i = 0; i < n; i++) {//每次for里都要完成且只完成一个任务//将未完成的tasks按优先数排序Comparator<tasks> comparator2=Comparator.comparing(tasks:: getYouxianshu);tasksList.sort(comparator2);int flag=0;for (tasks tasks : tasksList) {if(tasks.arriveTime<=time){tasks.startRunningTime=time;time+=tasks.runningTime;tasks.runningEndTime=time;tasksyiwanchengList.add(tasks);tasksList.remove(tasks);flag=1;break;}}if (flag==0){//如果全都没到达,直接移除接下来最早到达的Comparator<tasks> comparator3=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator3);tasksList.get(0).startRunningTime=tasksList.get(0).arriveTime;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;tasksyiwanchengList.add(tasksList.get(0));tasksList.remove(0);}}//最后遍历出结果tasksyiwanchengListint order=0;for (tasks tasks : tasksyiwanchengList) {order++;tasks.setOrder(order);tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}public static void duanzuoyeyouxian(List<tasks> tasksList) {List<tasks> tasksyiwanchengList=new ArrayList<>();//找最先的到达的作为时间time的开始,完成后更新time+=runningtimeComparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);//tasksyiwanchangList.add(task001)tasksyiwanchengList.add(tasksList.get(0));double time=tasksList.get(0).arriveTime;//此时时间点tasksList.get(0).startRunningTime=time;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;//tasksList.del(task001)删除最先的到达的tasktasksList.remove(0);//list.for执行下面这行的操作//在剩下的task里&&arrivetime<time也就是已经到达但还没开始的任务里,选作业runningtime最小的执行,更新time+=runningtimeint n= tasksList.size();for (int i = 0; i < n; i++) {//每次for里都要完成且只完成一个任务//将未完成的tasks按作业长短排序Comparator<tasks> comparator2=Comparator.comparing(tasks:: getRunningTime);//那这么的话和优先级相比只改了这一个单词tasksList.sort(comparator2);int flag=0;for (tasks tasks : tasksList) {if(tasks.arriveTime<=time){tasks.startRunningTime=time;time+=tasks.runningTime;tasks.runningEndTime=time;tasksyiwanchengList.add(tasks);tasksList.remove(tasks);flag=1;break;}}if (flag==0){//如果全都没到达,直接移除接下来最早到达的Comparator<tasks> comparator3=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator3);tasksList.get(0).startRunningTime=tasksList.get(0).arriveTime;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;tasksyiwanchengList.add(tasksList.get(0));tasksList.remove(0);}}//最后遍历出结果tasksyiwanchengListint order=0;for (tasks tasks : tasksyiwanchengList) {order++;tasks.setOrder(order);tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}public static void xiangyingbigaoyouxian(List<tasks> tasksList) {List<tasks> tasksyiwanchengList=new ArrayList<>();//响应比高优先  当每次进程被调度就要重新计算新的响应比//找最先的到达的作为时间time的开始,完成后更新time+=runningtimeComparator<tasks> comparator=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator);//tasksyiwanchangList.add(task001)tasksyiwanchengList.add(tasksList.get(0));double time=tasksList.get(0).arriveTime;//此时时间点tasksList.get(0).startRunningTime=time;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;//tasksList.del(task001)删除最先的到达的tasktasksList.remove(0);//list.for执行下面这行的操作//在剩下的task里&&arrivetime<time也就是已经到达但还没开始的任务里,选执行响应比最高的执行,更新time+=runningtimeint n= tasksList.size();for (int i = 0; i < n; i++) {//每次for里都要完成且只完成一个任务 然后响应比改变 重新计算新的响应比//计算runningendtime 迭代每个任务是下一次就被调的情况得到的tasks.runningendtimefor (tasks tasks : tasksList) {//好像应该time和arrivetime比较 还没到的要不要算响应比去比较啊??//先按照还没来的就不参与响应比的计算tasks.xingyingbi=-1;//为了计算响应比才暂时赋值 后面每次for里都要完成一个任务时才是正式的赋值if (tasks.arriveTime<=time){//给来的任务计算响应比 不来的响应比默认-1tasks.runningEndTime+=(time+ tasks.runningTime);}}//每次重新计算响应比 所以没个任务的zhouzhuanshijian必须在下面的计算前就已经知道tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;for (tasks tasks : tasksList) {tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;//arrivetime已知不用管 runningendtime必须知道tasks.xingyingbi=1+(tasks.zhouzhuanshijian/ tasks.runningTime);}//将未完成的tasks按响应比排序Comparator<tasks> comparator2=Comparator.comparing(tasks:: getXingyingbi).reversed();//在这之前必须把响应比算出来tasksList.sort(comparator2);int flag=0;for (tasks tasks : tasksList) {if(tasks.arriveTime<=time){tasks.startRunningTime=time;time+=tasks.runningTime;tasks.runningEndTime=time;tasksyiwanchengList.add(tasks);tasksList.remove(tasks);flag=1;break;}}if (flag==0){//如果全都没到达,直接移除接下来最早到达的Comparator<tasks> comparator3=Comparator.comparing(tasks:: getArriveTime);tasksList.sort(comparator3);tasksList.get(0).startRunningTime=tasksList.get(0).arriveTime;time+=tasksList.get(0).runningTime;tasksList.get(0).runningEndTime=time;tasksyiwanchengList.add(tasksList.get(0));tasksList.remove(0);}}//最后遍历出结果tasksyiwanchengListint order=0;for (tasks tasks : tasksyiwanchengList) {order++;tasks.setOrder(order);tasks.zhouzhuanshijian=tasks.runningEndTime-tasks.arriveTime;//arrivetime已知不用管 runningendtime必须知道tasks.saiquanzhouzhuanshijian=tasks.zhouzhuanshijian / tasks.runningTime;System.out.println("序号\t"+"进程名\t"+"进程号\t"+"到达时间\t"+"运行时间\t"+"优先数\t"+"开始运行时间\t"+"运行结束时间\t"+"周转时间\t"+"带权周转时间(左->右)");System.out.println(tasks.id+" "+tasks.jinchengname+" "+tasks.jinchengId+" "+ tasks.arriveTime+" "+ tasks.runningTime+" "+ tasks.youxianshu+" "+tasks.startRunningTime+" "+ tasks.runningEndTime+" "+tasks.zhouzhuanshijian+" "+tasks.saiquanzhouzhuanshijian+" "+ tasks.getOrder());}}
}

运行结果


http://www.ppmy.cn/news/1543285.html

相关文章

银河麒麟v10 xrdp安装

为了解决科技被卡脖子的问题&#xff0c;国家正在大力推进软硬件系统的信创替代&#xff0c;对于一些平时对Linux操作系统不太熟练的用户来讲提出了更高的挑战和要求。本文以银河麒麟v10 24.03为例带领大家配置kylin v10的远程桌面。 最近公司为了配置信创开发新购了几台银河麒…

啤酒游戏—企业经营决策沙盘

感谢黄浦区文华学院的邀请&#xff0c;今年是为南房集团开展系统思考培训的第二年。我们现在为客户设计的一整年系统思考训练中&#xff0c;会将系统环路结构图与真实议题研讨作为前置内容&#xff0c;让大家在理解整体框架后&#xff0c;再体验麻省理工学院系统动力学著名的“…

PDF无法转换成其他格式的常见原因与解决方法解析

在处理PDF文件转换时&#xff0c;用户常常会遇到一些问题&#xff0c;导致无法将PDF转换为其他格式&#xff08;如Word、Excel、或图片等&#xff09;。以下是一些常见原因以及解决方法的解析。 ## 一、常见原因 ### 1. **PDF文件的安全性设置** 许多PDF文件在创建时可能设置…

Android:ViewPaper动态添加移除第一页

问题&#xff1a; 动态改变itemList之后&#xff0c;调用notifyDataSetChanged();没有作用。 原因&#xff1a; 原因是PagerAdapter内部的getItemPosition方法获取的值是固定POSITION_UNCHANGED&#xff0c;表示未改变&#xff0c;所以不更新。 解决&#xff1a; 要更新则…

AUTOSAR 规范中的设计模式:传感器执行器模式

在 AUTOSAR Adaptive Platform (AP) 规范中&#xff0c;传感器执行器模式是一种典型的设计模式&#xff0c;主要用于实时控制系统中&#xff0c;用来实现传感器数据的获取和执行器指令的发送。该模式通过分离传感器和执行器的实现&#xff0c;使其独立运行并且能够通过某种通信…

显卡服务器的作用都有哪些?

显卡服务器就是人们日常生活中的GPU服务器&#xff0c;显卡服务器相比较普通的服务器来说&#xff0c;其最突出的特点是带有显卡功能&#xff0c;是对于企业中需要处理大数据的应用场景中可以添加独立显卡的服务器&#xff0c;那么显卡服务器的作用都有哪些呢&#xff1f; 显卡…

Kafka相关API开发

(一)引入依赖 用API直接去操作kafka(读写数据)在实际开发中用的并不多&#xff0c;学习它主要还是为了加深对Kafka功能的理解。kafka的读写操作&#xff0c;实际开发中&#xff0c;是通过各类更上层的组件去实现。而这些组件在读写kafka数据时&#xff0c;用的当然是kafka的jav…

如何配置,npm install 是从本地安装依赖

在 Node.js 中&#xff0c;要使npm install从本地安装依赖&#xff0c;可以按照以下步骤进行配置&#xff1a; 一、准备本地依赖包 确保你有本地的依赖包。这个依赖包可以是一个包含package.json文件的文件夹&#xff0c;或者是一个已经打包好的.tgz文件。 二、使用相对路径…