2015-04-15 19 views
-1

我的數組稱爲myArr,該包含剩餘時間最短調度MIN()錯誤

Process burst arrive 
1   8  0 
2   4  1 
3   9  2 
4   5  3 

是我得到的問題是,我想我的MIN()方法是錯誤的,當和我'得到這個輸出

Gantt Chart 
| p[2] | p[4] | p[1] | p[3] | 
0  1  5  10  17 26 

,我應該去獲得這個下面的輸出

enter image description here

System.out.println("--------------------------------------Shortest Process Next--------------------------------------"); 
      System.out.println(); 

      Arrays.sort(myArr, new Comparator<int[]>() { 
        public int compare(int[] o1, int[] o2) { 
         int ret = Integer.compare(o1[2], o2[2]); 
         // if the entries are equal at index 2, compare index 1 
         if (0 == ret) { 
          ret = Integer.compare(o1[1], o2[1]); 
         } 
         return (ret); 
        } 
       }); 

      System.out.println("____Process_____"); 
      System.out.println("P "+"B "+"A "); 
      for(int t=0; t<myArr.length; t++){ 


       System.out.println(myArr[t][0]+" "+myArr[t][1]+" "+myArr[t][2]+" "); 
      } 


       InputStreamReader isr = new InputStreamReader(System.in); 
       BufferedReader in = new BufferedReader(isr); 

       int n=myArr.length; //# of process 


       int p[] = new int[n]; 
       int at[] = new int[n]; 
       int bt[] = new int[n]; 
       int bt2[] = new int[n]; 
       int wt[] = new int[n]; 
       int tat[] = new int[n]; 

       for (int i = 0; i < n; i++) { 

        p[i] = myArr[i][0]; // Process number 
        at[i] = myArr[i][2]; //arrival time 
        bt[i] = myArr[i][1];//burst time 

        bt2[i] = bt[i];//copy of the burst times 
       } 

       int tbt = 0; 
       for (int i = 0; i < n; i++) { 
        tbt = tbt + bt[i];    //Suma de todos los burst time 
       } 

       int time[] = new int[tbt]; // array time tiene un size del count de los burst time 
       int k = 0; 
       int q2 = 0; 

       System.out.println("Gantt Chart"); 
       System.out.print("|"); 
       //bt[0] = bt[0] - 1; 

       for (int i = 0; i < tbt; i++) { 
        int q = Min(bt, at, tbt, i, n); 
        if (q != q2) { 
         System.out.print(" p[" + p[q] + "]\t|"); 
         time[k++] = i; 
         wt[q] = i; 
         tat[q] = i + bt[q]; 
        } 
        bt[q] = bt[q] - 1; 
        q2 = q; 
       } 
       time[k] = tbt; 
       System.out.println(); 
       System.out.print("0\t"); 
       for (int i = 0; i <= k; i++) { 
        System.out.print(time[i] + "\t"); 
       } 

,我認爲是搞亂我的輸出

public static int Min(int b[], int a[], int tbt, int r, int n) { 

     int j = 0; 
     int min = tbt; 

     for (int i = n - 1; i >= 0; i--) { 
      if (b[i] < min && b[i] > 0 && r >= a[i]) { 
       min = b[i]; 
       j = i; 
      } 
     } 
     return j; 
    } 
+0

調試您的代碼並告訴我們爲什麼得到錯誤的輸出。 – Mathemats

+0

我試圖做到這一點,但一切都很好 –

回答

1

你需要安排此按到達時間,而不是直接用最短剩餘時間的因素這是我的MIN()方法。

的不正確輸出的原因是: -

  1. 除非處理隊列已經來臨,你不能在就緒隊列調度。

  2. 另外,您的預期輸出圖表也顯示不正確。如果只是,需要的是實現最短第一剩餘時間,沒有任何時間片,然後在甘特圖看起來像: - 現在

    | P1 | P2 | P4 | P3 |


0   8  12  17    26 

我贏了不要評論代碼中的錯誤,因爲它看起來很多,但是,我會建議你採用更好的方法。

a)您應先檢查到達時刻爲每個進程開始 0和第一選擇最短的突發時間的工作,讓它完全執行。

B)的第一份工作完成後,再檢查一遍所有進程的到達時間,然後選擇最短的突發時間過程(誰已經到達的過程)

C)迭代中提到對於n個過程。選擇最短的突發時間過程並完全完成,然後按照步驟a,b和c進行,直到過程結束。

如果您在執行相同操作時遇到問題,請提出一個關於此問題的新問題。