该程序使用FCFS,SJF和RR调度5个进程,q = 1,只要调用RR方法,问题就会开始。 NetBeans显示它正在运行但输出控制台上没有显示任何内容,只有FCFS和SJF Avg。等待时间和平均周转时间。但是,有时它会起作用。我不知道这个程序有什么问题。
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 :(得分:0)
在您的循环模拟中,如果全部bt[j] > 0 && at[j] <= t
,则时钟不会前进。由于数组以随机值播种,这种情况随机发生,因此有时模拟无法完成。