2016-12-22 8 views
0

このプログラムは、q = 1のFCFS、SJF、およびRRを使用して5つのプロセスをスケジュールします.RRメソッドが呼び出されるたびに問題が開始されます。 NetBeansは実行中であることを示していますが、出力コンソールには何も表示されず、FCFSとSJF平均のみが表示されます。待ち時間と平均。ターンアラウンドタイム。しかし、時にはそれが動作します。私はこのプログラムに何が間違っているのか分かりません。あなたのラウンドロビンシミュレーションでRRメソッドが呼び出されると、私のプログラムは非常に遅く実行されますか?

package hw5; 

import java.util.Random; 

/** 
* This program schedules 5 processes using FCFS, SJF, and RR with q = 1 
* 
* @author 
*/ 
public class HW5 { 

    // sort is a method that sorts arrivalTime array for FCFS algorithm 
    public static void sort(int[] bt, int[] at) {   
     for (int i = 0; i <= 5; i++) { 
      for (int j = i + 1; j < 5; j++) { 
       if (at[i] > at[j]) {      
        int tempAr = at[i]; 
        at[i] = at[j]; 
        at[j] = tempAr;      
        int tempBr = bt[i]; 
        bt[i] = bt[j]; 
        bt[j] = tempBr; 
       } 
      } 

     } 
    } 

    // print the current set of processes with their corresponding burst time and arrival time 
    public static void print(int[] bt, int[] at) { 
     System.out.println("Process" + "\t" + "Burst time" + " Arrival time"); 
     for (int index = 0; index < 5; index++) { 
      System.out.println("P" + (index + 1) + "\t " + bt[index] + "\t  " + at[index]); 
     } 
     System.out.println(); 
    } 

    // FCFS method that schedules processes using first come first serve algorithm 
    public static int[] FCFC(int[] bt, int[] at) { 
     int[] total = new int[2]; // Array to store total waiting time and total turnaround time 
     int waitTime[] = new int[5]; // Array to store wait time for each process 
     waitTime[0] = 0; 
     for (int i = 1; i < 5; i++) { 
      waitTime[i] = bt[i - 1] + waitTime[i - 1]; 
     } 
     int turnaroundTime[] = new int[5]; 
     for (int i = 0; i < 5; i++) { 
      turnaroundTime[i] = bt[i] + waitTime[i]; 
     } 
     // To Calculate waitTime and turnaroundTime for each process and their total 
     for (int i = 0; i < 5; i++) { 
      waitTime[i] = waitTime[i] - at[i]; 
      turnaroundTime[i] = turnaroundTime[i] - at[i]; 
      total[0] += waitTime[i]; 
      total[1] += turnaroundTime[i]; 
     } 
     return total; 
    } 

    // SJF method that schedules processes using shortest job first algorithm 
    public static int[] SJF(int[] bt, int[] at) { 
     int[] total = new int[2]; // Array to store total waiting time and total turnaround time 
     int waitTime[] = new int[5]; // Array to store wait time for each process 
     int[] copyBT = new int[5]; 
     int turnaroundTime[] = new int[5]; 
     int sum = 0; 
     for (int i = 0; i < 5; i++) { 
      copyBT[i] = bt[i]; 
      sum += bt[i]; 
     } 
     for (int t = 1; t <= sum; t++) { 
      int min = 20, index = 0; 
      // This for loop used to find the min. burst time each 1 time unit 
      for (int j = 0; j < 5; j++) { 
       if (at[j] < t && bt[j] < min && bt[j] > 0) { 
        min = bt[j]; 
        index = j; 
       } 
      } 
      bt[index]--; 
      if (bt[index] == 0) { 
       turnaroundTime[index] = t - at[index]; 
       waitTime[index] = turnaroundTime[index] - copyBT[index]; 
      } 
     } 
     for (int i = 0; i < 5; i++) { 
      total[0] += waitTime[i]; 
      total[1] += turnaroundTime[i]; 
     } 
     return total; 
    } 
    // RR method that schedules processes using round robin algorithm 
    public static int[] RR(int[] bt, int[] at) { 
     int tq = 1; // a time slice of 1 quantum 
     int[] total = new int[2]; // Array to store total waiting time and total turnaround time 
     int waitTime[] = new int[5]; // Array to store wait time for each process 
     int[] copyBT = new int[5]; 
     int turnaroundTime[] = new int[5]; 
     int sum = 0; 
     for (int i = 0; i < bt.length; i++) { 
      copyBT[i] = bt[i]; 
      sum += bt[i]; 
     } 
     int t = 0; //time 
     while (t < sum) { 
      for (int j = 0; j < 5; j++) { 
       if (bt[j] > 0 && at[j] <= t) { 
        if (bt[j] > tq) { 
         bt[j] -= tq; 
         t += tq; 
        } else { 
         t += bt[j]; 
         bt[j] = 0; 
         turnaroundTime[j] = t - at[j]; 
         waitTime[j] = turnaroundTime[j] - copyBT[j]; 
        } 
       } 
      } 
     } 
     for (int i = 0; i < 5; i++) { 
      total[0] += waitTime[i]; 
      total[1] += turnaroundTime[i]; 
     } 
     return total; 
    } 


    public static void main(String[] args) { 

      int[] burstTime = new int[5]; // Array of burst times 

      int[] arrivalTime = new int[5]; // Array of arrival times 

      int[] copyBurstTime = new int[5]; // A copy array of burstTime array 

      int[] copyArrivalTime = new int[5]; // A copy array of arrivalTime array 

      Random r = new Random(); // r is a variable of type Random 

      int[] total; //= new int[2]; // saves two values, one total wait time and second total average time 

      long startTime, estimatedTime; 

      double throughput; 

      // Filling the above arrays with random integer values 
      for (int index = 0; index < 5; index++) { 
       burstTime[index] = r.nextInt(10) + 1; 
       copyBurstTime[index] = burstTime[index]; 
       arrivalTime[index] = r.nextInt(10); 
       copyArrivalTime[index] = arrivalTime[index]; 
      } 

      print(burstTime, arrivalTime); // print a set of 5 processes 

      sort(copyBurstTime, copyArrivalTime); // sort copyArrivalTime in ascending order and 

      // FCFS scheduling block 
      startTime = System.nanoTime(); 
      total = FCFC(copyBurstTime, copyArrivalTime); 
      estimatedTime = System.nanoTime() - startTime; 
      throughput = 5.0/estimatedTime; 
      System.out.println("FCFS\t" + "Average Waiting Time: " + (total[0]/5.0) + "\tAverage Turnaround Time: " + (total[1]/5.0) + "\tThroughput: " + throughput); 

      System.arraycopy(burstTime, 0, copyBurstTime, 0, 5); 

      // SJF scheduling block 
      startTime = System.nanoTime(); 
      total = SJF(burstTime, arrivalTime); 
      estimatedTime = System.nanoTime() - startTime; 
      throughput = 5.0/estimatedTime; 
      System.out.println("SJF\t" + "Average Waiting Time: " + (total[0]/5.0) + "\tAverage Turnaround Time: " + (total[1]/5.0) + "\tThroughput: " + throughput); 

      // RR scheduling block 
      startTime = System.nanoTime(); 
      total = RR(copyBurstTime, arrivalTime); 
      estimatedTime = System.nanoTime() - startTime; 
      throughput = 5.0/estimatedTime; 
      System.out.println("RR\t" + "Average Waiting Time: " + (total[0]/5.0) + "\tAverage Turnaround Time: " + (total[1]/5.0) + "\tThroughput: " + throughput); 
     //} 
    } 

} 

答えて

0

すべてbt[j] > 0 && at[j] <= t場合は、あなたの時計は進みません。配列にランダムな値が設定されているため、この条件はランダムに発生するため、シミュレーションが完了できないことがあります。

関連する問題