最短作业优先算法

原创 2007年09月28日 18:00:00
 

public class Job {
 private String name = "";
 private String inTime = "";
 private int maybeRunTime = 0;
 private int waitTime = 0;
 private int turnoverTime = 0;
 private int withPoweroverTime = 0;
 public int getTurnoverTime() {
  return turnoverTime;
 }
 public void setTurnoverTime(int turnoverTime) {
  this.turnoverTime = turnoverTime;
 }
 public int getWithPoweroverTime() {
  return withPoweroverTime;
 }
 public void setWithPoweroverTime(int withPoweroverTime) {
  this.withPoweroverTime = withPoweroverTime;
 }
 public String getInTime() {
  return inTime;
 }
 public void setInTime(String inTime) {
  this.inTime = inTime;
 }
 public int getMaybeRunTime() {
  return maybeRunTime;
 }
 public void setMaybeRunTime(int maybeRunTime) {
  this.maybeRunTime = maybeRunTime;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getWaitTime() {
  return waitTime;
 }
 public void setWaitTime(int waitTime) {
  this.waitTime = waitTime;
 }
 
}


/////////////////////////////////////////////////////////////////////////////////////////

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import java.util.Vector;

public class Operating {
 public static String dealTime(String time, int parMinutes) {
  StringTokenizer tokenizer = new StringTokenizer(time,":");
  String hour = tokenizer.nextToken();
  String minute = tokenizer.nextToken();
  int intHour = Integer.parseInt(hour);
  int intMinute = Integer.parseInt(minute);
  if((intMinute + parMinutes) < 60) {
   intMinute = intMinute + parMinutes;
  }else {
   intHour = (int)(intMinute + parMinutes)/60 + intHour;
   intMinute = (intMinute + parMinutes)%60;
  }
  if(intMinute < 10)
   return intHour+":0"+intMinute;
  else
   return intHour+":"+intMinute;
 }
 public static boolean compareTime(String time1,String time2) {
  
  StringTokenizer tokenizer1 = new StringTokenizer(time1,":");
  String hour1 = tokenizer1.nextToken();
  String minute1 = tokenizer1.nextToken();
  int intHour1 = Integer.parseInt(hour1);
  int intMinute1 = Integer.parseInt(minute1);
  
  StringTokenizer tokenizer2 = new StringTokenizer(time2,":");
  String hour2 = tokenizer2.nextToken();
  String minute2 = tokenizer2.nextToken();
  int intHour2 = Integer.parseInt(hour2);
  int intMinute2 = Integer.parseInt(minute2);
  
  if(intHour1 > intHour2) {
   return true;
  }else if (intHour1 == intHour2) {
   if(intMinute1 > intMinute2) {
    return true;
   }
  }
  return false;
 }
 public static void main(String[] args) throws Exception {
  int sumOverturn = 0;
  float sumWithPower = 0;
  int jobNum = 0;
  BufferedReader fromFile = new BufferedReader(new FileReader("in.txt"));
  Vector<Job> jobs = new Vector<Job>();  
  String jobInLine;
  while((jobInLine = fromFile.readLine()) !=null) {
   Job job = new Job();
   StringTokenizer  strTokenizer = new StringTokenizer(jobInLine);
   job.setName(strTokenizer.nextToken());
   job.setInTime(strTokenizer.nextToken());
   job.setMaybeRunTime(Integer.parseInt(strTokenizer.nextToken()));
   jobs.addElement(job);
  }  
  System.out.println("作业名称 " + "/t进入 " + "/t开始" + "/t结束 " + "/t周转时间 " + "/t带权周转时间 ");
  while(!jobs.isEmpty()) {
   Job toDealJob = new Job();
   int k = 0;
   for (int i = 0,j = 0;i < jobs.size();) {
    i = 0;
    j = 0;
    k = 0;
    toDealJob = jobs.elementAt(i);    
    for (j = i + 1;j < jobs.size(); j++) {
     String jInTime = jobs.elementAt(j).getInTime();
     String nowTime = Operating.dealTime(toDealJob.getInTime(),toDealJob.getWaitTime());
     
     
     if((jobs.elementAt(j).getMaybeRunTime() < toDealJob.getMaybeRunTime())
       && (Operating.compareTime(jInTime, nowTime))) {
       toDealJob = jobs.elementAt(j);
       k = j;
     }
    }
    String startTime = Operating.dealTime(toDealJob.getInTime(), toDealJob.getWaitTime());    
    toDealJob = jobs.elementAt(k);
    int overturn = toDealJob.getWaitTime() + toDealJob.getMaybeRunTime();
    float withPower = overturn/toDealJob.getMaybeRunTime();
    String endTime = Operating.dealTime(toDealJob.getInTime(), overturn);
    System.out.println(toDealJob.getName() + "/t" //作业名称
         + toDealJob.getInTime()+ "/t" //进入
         + startTime +  "/t" //开始
         + endTime + "/t"//结束
         + overturn + "/t"
         + withPower);
    sumOverturn = sumOverturn + overturn;
    sumWithPower = sumWithPower + withPower;
    jobNum++;    
    for(int t = 0;t < jobs.size();t++) {
     jobs.elementAt(t).setWaitTime(jobs.elementAt(t).getWaitTime() + toDealJob.getMaybeRunTime());
    }
    jobs.remove(k);
   }
  }
  System.out.println();
  float averageOverturn = sumOverturn/jobNum;
  float averageWithPower = sumWithPower/jobNum;
  System.out.println("平均周转时间为 :" + averageOverturn + " 分钟");
  System.out.println("平均带权周转时间为 :" + averageWithPower + " 分钟");
 }
}

/////////////////////////////////////////////////////////////////////////////////////////


in.txt的内容:

Job1 12:00 30
Job2 12:30 40
Job3 12:40 30
Job4 13:00 30
Job5 13:50 50
Job6 14:00 30
Job7 14:40 30
Job8 15:01 30

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

最短作业优先调度算法(SJF算法)的C++实现

题目要求: 在作业调度中,该算法每次从后备作业队列中挑选估计服务时间最短的一个或几个作业,将他们调入内存,分配必要的资源,创建进程并放入就绪队列。与在进程调度中的原理类似。假设有n项作业位于就绪队列中...

短作业优先算法-SJF

//任何转载源代码的请保留出处作者等相关信息 //作者:chlaws //运行环境:visual studio 2008 //描述:短作业优先算法-SJF //说明:才用c风格写的,这里不给出...
  • chlaws
  • chlaws
  • 2009-12-26 22:23
  • 9688

C语言 scanf()和gets()函数的区别

C语言 scanf()和gets()函数的区别 1.相同点:scanf( )函数和gets( )函数都可用于输入字符串 2.不同点:两者在功能上有所区别,具体区别如下: 要实现如下需求“从控...

Java模拟最短作业优先、时间片轮转、最高响应比三种进程调度算法

本次试验是使用程序来模拟操作系统中进程调度的三种不同的调度策略,分别为最短作业有限、时间片轮转、最高响应比。 模拟的情况下,进程数为8,进程所需执行时间为随机产生的整数,单位为1S,默认进程同时到达。...

最短作业优先算法(不完善)

最短作业优先(SJF)是一种调试任务请求的调试策略。每个任务请求都含有请求时间(即向系统提交请求的时间)和持续时间(即完成任务所需时间)属性。当前任务完成后,SJF策略选择最短持续时间的任务执行;如果...

最短作业优先

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)