Java实现进程调度算法(二) RR(时间片轮转)
程序员文章站
2022-07-06 07:53:08
RR类(主类) 只有calc()中涉及了算法,init()和printResult()都只有简单的输入输出操作。 Process类 模拟了进程,对属性进行了封装。 Tools类 因为我这次作业要实现几个类似的算法,所以我把每个算法中都要用到的方法都提取出来作为单独的工具类。 也可以将这些工具方法都放 ......
rr类(主类)
只有calc()中涉及了算法,init()和printresult()都只有简单的输入输出操作。
1 package xqy.algorithm; 2 3 import java.util.*; 4 5 import xqy.util.tools; 6 import xqy.been.process; 7 8 /** 9 * @author xqy 10 * @date 2018年12月19日19:14:49 11 */ 12 public class rr { 13 private int processnumber; 14 private arraylist<process> processlist; 15 private int timeslice; 16 17 public rr() { 18 init(); 19 calc(); 20 tools.printresult(processlist); 21 } 22 23 private void init() { 24 scanner sc = new scanner(system.in); 25 26 system.out.print("<rr> please enter the slice time:"); 27 timeslice = sc.nextint(); 28 system.out.print("<rr> please enter the process num:"); 29 processnumber = sc.nextint(); 30 31 processlist = new arraylist<process>(); 32 for (int i = 0; i < processnumber; i++) { 33 processlist.add(new process()); 34 } 35 36 system.out.println("<rr> please enter each process arrival time:"); 37 for (int i = 0; i < processnumber; i++) { 38 system.out.print(" process" + (i + 1) + ":"); 39 processlist.get(i).setarrivaltime(sc.nextint()); 40 } 41 42 system.out.println("<rr> please enter each process service time:"); 43 for (int i = 0; i < processnumber; i++) { 44 system.out.print(" process" + (i + 1) + ":"); 45 int servicestime = sc.nextint(); 46 system.out.println(); 47 48 processlist.get(i).setservicestime(servicestime); 49 processlist.get(i).setremainservicetime(servicestime); 50 } 51 } 52 53 private void calc() { 54 int timenow = 0; 55 int processremain = processnumber; 56 process opprocess; 57 58 while (processremain != 0) { 59 for (int i = 0; i < processnumber; i++) { 60 opprocess = processlist.get(i); 61 62 if (opprocess.getremainservicetime() > 0) { 63 // first time 64 if (opprocess.getservicestime() == opprocess.getremainservicetime()) { 65 int waittime = timenow - opprocess.getarrivaltime(); 66 67 opprocess.setstarttime(timenow); 68 opprocess.setwaittime(waittime); 69 } 70 71 // calculating remain service time 72 int remainservicetime = opprocess.getremainservicetime() - timeslice; 73 opprocess.setremainservicetime(remainservicetime); 74 75 // last time 76 if (remainservicetime <= 0) { 77 int completiontime = timenow + timeslice; // 进程是运行完当前时间片才结束的 78 int turnaroundtime = completiontime 79 - opprocess.getarrivaltime(); 80 double turnaroundtimewithweight = 1.0 * turnaroundtime 81 / opprocess.getservicestime(); 82 83 opprocess.setcompletiontime(completiontime); 84 opprocess.setturnaroundtime(turnaroundtime); 85 opprocess.setturnaroundtimewithweight( 86 turnaroundtimewithweight); 87 88 processremain--; 89 } 90 91 system.out.println(" #step#process" + (i + 1) + " remain service time:" + opprocess.getremainservicetime() + ((remainservicetime <= 0)?" finish":"")); 92 timenow += timeslice; 93 } else { 94 // do noting, because this process has been completed. 95 } 96 97 } 98 } 99 } 100 101 public static void main(string [] args) { 102 new rr(); 103 } 104 }
process类
模拟了进程,对属性进行了封装。
1 package xqy.been; 2 3 public class process { 4 private int arrivaltime; 5 private int servicestime; 6 private int remainservicetime; 7 private int starttime; 8 private int waittime; 9 private int completiontime; 10 11 /** 12 * turnaroundtime = completiontime - arrivaltime 13 */ 14 private int turnaroundtime; 15 16 /** 17 * turnaroundtimewithweight = turnaroundtime / servicestime 18 */ 19 private double turnaroundtimewithweight; 20 21 public process() { 22 ; 23 } 24 25 public int getarrivaltime() { 26 return arrivaltime; 27 } 28 29 public void setarrivaltime(int arrivaltime) { 30 this.arrivaltime = arrivaltime; 31 } 32 33 public int getservicestime() { 34 return servicestime; 35 } 36 37 public void setservicestime(int servicestime) { 38 this.servicestime = servicestime; 39 } 40 41 public int getremainservicetime() { 42 return remainservicetime; 43 } 44 45 public void setremainservicetime(int remainservicetime) { 46 this.remainservicetime = remainservicetime; 47 } 48 49 public int getstarttime() { 50 return starttime; 51 } 52 53 public void setstarttime(int starttime) { 54 this.starttime = starttime; 55 } 56 57 public int getwaittime() { 58 return waittime; 59 } 60 61 public void setwaittime(int waittime) { 62 this.waittime = waittime; 63 } 64 65 public int getcompletiontime() { 66 return completiontime; 67 } 68 69 public void setcompletiontime(int completiontime) { 70 this.completiontime = completiontime; 71 } 72 73 public int getturnaroundtime() { 74 return turnaroundtime; 75 } 76 77 public void setturnaroundtime(int turnaroundtime) { 78 this.turnaroundtime = turnaroundtime; 79 } 80 81 public double getturnaroundtimewithweight() { 82 return turnaroundtimewithweight; 83 } 84 85 public void setturnaroundtimewithweight(double turnaroundtimewithweight) { 86 this.turnaroundtimewithweight = turnaroundtimewithweight; 87 } 88 89 @override 90 public string tostring() { 91 return "process [arrivaltime=" + arrivaltime + ", servicestime=" 92 + servicestime + ", remainservicetime=" + remainservicetime 93 + ", starttime=" + starttime + ", waittime=" + waittime 94 + ", completiontime=" + completiontime + ", turnaroundtime=" 95 + turnaroundtime + ", turnaroundtimewithweight=" 96 + turnaroundtimewithweight + "]"; 97 } 98 }
tools类
因为我这次作业要实现几个类似的算法,所以我把每个算法中都要用到的方法都提取出来作为单独的工具类。
也可以将这些工具方法都放入fcfs类中。
1 package xqy.util; 2 3 import java.util.arraylist; 4 5 import xqy.been.process; 6 7 public class tools { 8 9 public static double calcaverageturnaroundtime( 10 arraylist<process> processlist) { 11 double sum = 0; 12 for (int i = 0; i < processlist.size(); i++) { 13 sum += processlist.get(i).getturnaroundtime(); 14 } 15 return math.round(sum / processlist.size() * 100) / 100.0; 16 } 17 18 public static double calcaverageturnaroundtimewithweight( 19 arraylist<process> processlist) { 20 double sum = 0; 21 for (int i = 0; i < processlist.size(); i++) { 22 sum += processlist.get(i).getturnaroundtimewithweight(); 23 } 24 return math.round(sum / processlist.size() * 100) / 100.0; 25 } 26 27 public static void printresult(arraylist<process> processlist) { 28 system.out.println("\n #result#"); 29 30 system.out.print("\tarrive:\t\t"); 31 for (int i = 0; i < processlist.size(); i++) { 32 system.out.print(processlist.get(i).getarrivaltime() + "\t"); 33 } 34 system.out.println(); 35 36 system.out.print("\tservice:\t"); 37 for (int i = 0; i < processlist.size(); i++) { 38 system.out.print(processlist.get(i).getservicestime() + "\t"); 39 } 40 system.out.println(); 41 42 system.out.print("\tstart:\t\t"); 43 for (int i = 0; i < processlist.size(); i++) { 44 system.out.print(processlist.get(i).getstarttime() + "\t"); 45 } 46 system.out.println(); 47 48 system.out.print("\twait:\t\t"); 49 for (int i = 0; i < processlist.size(); i++) { 50 system.out.print(processlist.get(i).getwaittime() + "\t"); 51 } 52 system.out.println(); 53 54 system.out.print("\tfinish:\t\t"); 55 for (int i = 0; i < processlist.size(); i++) { 56 system.out.print(processlist.get(i).getcompletiontime() + "\t"); 57 } 58 system.out.println(); 59 60 system.out.print("\tturn around:\t"); 61 for (int i = 0; i < processlist.size(); i++) { 62 system.out.print(processlist.get(i).getturnaroundtime() + "\t"); 63 } 64 system.out.println(); 65 66 system.out.print("\tta wight:\t"); 67 for (int i = 0; i < processlist.size(); i++) { 68 system.out.print(math.round(processlist.get(i) 69 .getturnaroundtimewithweight() * 100) / 100.0 + "\t"); 70 } 71 system.out.println(); 72 73 system.out.println("\taverage turn around time:" 74 + tools.calcaverageturnaroundtime(processlist) + "\t"); 75 system.out.println("\taverage turn around time with wight:" 76 + tools.calcaverageturnaroundtimewithweight(processlist)); 77 78 system.out.println(); 79 } 80 }
上一篇: 老婆