2015-09-29 44 views
1

我需要实现一个“循环”调度程序,其中包含一个我无法修改的作业类。循环调度程序应该先处理等待时间最长的作业,然后将定时器重置为零。如果两个作业的等待时间相同,则先处理较低的ID。工作分类只给出三个值(工作ID,剩余时间和优先级(这不是必需的),每个工作都有一个开始时间,所以在第一个周期只有几个工作可用,下一个周期只需要几个工作,等等。由于“工作数组”我呼吁每一个我称之为时间是不同的,我不知道如何存储的等待时间Round Robin Scheduler

这是作业类:

public class Jobs{ 
private int[] stas = new int[0]; 
private int[] durs = new int[0]; 
private int[] lefs = new int[0]; 
private int[] pris = new int[0]; 
private int[] fins = new int[0]; 
private int clock; 

public Jobs() 
{ 
    this("joblist.csv"); 
} 

public Jobs(String filename) 
{ 
    BufferedReader fp = null; 
    String line = ""; 
    String[] b = null; 
    int[] tmp; 
    try 
    { 
     fp = new BufferedReader(new FileReader(filename)); 
     while((line = fp.readLine()) != null) 
     { 
      b = line.split(","); 
      if(b.length == 3) 
      { 
       try 
       { 
        int sta = Integer.parseInt(b[0]); 
        //System.out.println("sta: " + b[0]); 
        int dur = Integer.parseInt(b[1]); 
        //System.out.println("dur: " + b[1]); 
        int pri = Integer.parseInt(b[2]); 
        //System.out.println("pri: " + b[2]); 
        stas = app(stas, sta); 
        //System.out.println("stas: " + Arrays.toString(stas)); 
        durs = app(durs, dur); 
        //System.out.println("durs: " + Arrays.toString(durs)); 
        lefs = app(lefs, dur); 
        //System.out.println("lefs: " + Arrays.toString(lefs)); 
        pris = app(pris, pri); 
        //System.out.println("pris: " + Arrays.toString(pris)); 
        fins = app(fins, -1); 
        //System.out.println("fins: " + Arrays.toString(fins)); 
       } 
       catch(NumberFormatException e) {} 
      } 
     } 
     fp.close(); 
    } 
    catch(FileNotFoundException e) { e.printStackTrace(); } 
    catch(IOException e) { e.printStackTrace(); } 
    clock = 0; 
} 

public boolean done() 
{ 
    boolean done = true; 
    for(int i=0; done && i<lefs.length; i++) 
     if(lefs[i]>0) done=false; 
    return done; 
} 

public int getClock() { return clock; } 
public int[][] getJobs() 
{ 
    int count = 0; 
    for(int i=0; i<stas.length; i++) 
     if(stas[i]<=clock && lefs[i]>0) 
      count++; 
    int[][] jobs = new int[count][3]; 
    count = 0; 
    for(int i=0; i<stas.length; i++) 
     if(stas[i]<=clock && lefs[i]>0) 
     { 
      jobs[count] = new int[]{i, lefs[i], pris[i]}; 
      count++; 
     } 
    return jobs; 
} 

public int cycle() { return cycle(-1); } 
public int cycle(int j) 
{ 
    if(j>=0 && j<lefs.length && clock>=stas[j] && lefs[j]>0) 
    { 
     lefs[j]--; 
     if(lefs[j] == 0) fins[j] = clock+1; 
    } 
    clock++; 
    return clock; 
} 

private int[] app(int[] a, int b) 
{ 
    int[] tmp = new int[a.length+1]; 
    for(int i=0; i<a.length; i++) tmp[i] = a[i]; 
    tmp[a.length] = b; 
    return tmp; 
} 

public String report() 
{ 
    String r = "JOB,PRIORITY,START,DURATION,FINISH,DELAY,PRI*DELAY\n"; 
    float dn=0; 
    float pdn=0; 
    for(int i=0; i<stas.length; i++) 
    { 
     if(fins[i]>=0) 
     { 
      int delay = ((fins[i]-stas[i])-durs[i]); 
      r+= ""+i+","+pris[i]+","+stas[i]+","+durs[i]+","+fins[i]+","+delay+","+(pris[i]*delay)+"\n"; 
      dn+= delay; 
      pdn+= pris[i]*delay; 
     } 
     else 
     { 
      int delay = ((clock*10-stas[i])-durs[i]); 
      r+= ""+i+","+pris[i]+","+stas[i]+","+durs[i]+","+fins[i]+","+delay+","+(pris[i]*delay)+"\n"; 
      dn+= delay; 
      pdn+= pris[i]*delay; 
     } 
    } 
    if(stas.length>0) 
    { 
     r+= "Avg,,,,,"+(dn/stas.length)+","+pdn/stas.length+"\n"; 
    } 
    return r; 
} 

public String toString() 
{ 
    String r = "There are "+stas.length+" jobs:\n"; 
    for(int i=0; i<stas.length; i++) 
    { 
     r+= " JOB "+i+": START="+stas[i]+" DURATION="+durs[i]+" DURATION_LEFT="+lefs[i]+" PRIORITY="+pris[i]+"\n"; 
    } 
    return r; 
} 

我不不需要完整的代码,只是一个想法,如何存储等待时间和循环正确的工作。

+0

这似乎工作: 对(INT I = 0; I user1726845

+0

}对(INT I = 0;我 longestWait) \t \t \t \t \t \t \t \t { \t \t \t \t \t \t \t \t \t longestWait = WAITTIME [I]; \t \t \t \t \t \t \t \t \t nextJob = I; \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t} jobsource。cycle(nextJob); waitTime [nextJob] = 0; – user1726845

回答

0

虽然基于数组的解决方案'可能'工作,我会主张一个更面向对象的方法。用欲望属性(id,start_time,wait等)创建'Job'类。使用csv文件创建Job对象并将其保存在列表中。写一个比较器来排序这个作业列表(在这种情况下基于作业等待/年龄将是因素)。

工作执行然后必须执行下列操作:

while(jobs exist) { 
    iterate on the list { 
    if job is executable // start_time > current sys_time 
    consume cycles/job for executable jobs 
    mark completed jobs (optional) 
    }  
    remove the completed jobs 
} 
+0

为此,我无法改变Jobs类,我只能从每个工作中获得(id,durationLeft,priority),并且我可以为一个周期运行一个工作。如果我可以改变工作类,这将容易得多。我非常需要用主方法创建一个类,并以某种方式为每个作业存储等待时间变量。 – user1726845

0
//\ This loop will add +1 to each job 
    for(int i = 0; i < jobs.length; i++) 
    { 
     waitTime[jobs[i][0]] += 1;   
    } 

int longestWait = 0;//\ This holds value for greatest wait time 
int nextJob = 0; //\ This holds value for index of job with greatest wait time 

    //\ this loop will check for the greatest wait time and and set variables accordingly 
    for(int i = 0; i < waitTime.length; i++) 
    { 
     if(waitTime[i] > longestWait) 
    { 
      longestWait = waitTime[i]; 
      nextJob = i; 
    } 
    } 

    //\ this cycles the job with the highest wait time 
    jobsource.cycle(nextJob); 

    //\ this resets the wait time for processed job 
    waitTime[nextJob] = 0; 
+0

这看起来可以完成这项工作。 – user1726845