最短作业优先算法

原创 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, Shortest Job First)又称为“短进程优先”SPN(Shortest Process Next);是对FCFS算法的改进,其目标是减少平均周转时间。 短作业优先调...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

短作业优先算法c++版

 #include "stdafx.h" #include "iostream" #include "string" using namespace std; struct JOB//作...

调度算法——短作业优先(java实现)

调度算法——短作业优先(java实现) class A{  class B{//内部类方便   private String processname ;//s进程名   private...

短作业优先(SJF)算法

SJF算法:将一些作业按照服务时间升序排列,较短的作业先执行,这样可以保证较好的周转时间,但是同时,长作业可能会等待很长时间才会运行。 //main.cpp #include "SJF.h...

短作业优先算法-SJF

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

最短作业优先(SJF)

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

计算机操作系统调度算法——短作业优先算法简单实现

小小编程题~

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

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

短作业优先调度算法(SJF)——Java实现

短作业优先调度算法(SJF)         短作业优先调度算法(Short Job First)用于进程调度时又被称为短进程优先调度算法(Short Process First),该算法既可以用于...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:最短作业优先算法
举报原因:
原因补充:

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