最短剩余时间调度 Min() 错误
Shortest Remaining Time scheduling Min() error
我的名为 myArr 的数组包含
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
我应该得到以下输出
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");
}
我认为我的 Min() 方法弄乱了我的输出
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;
}
需要按到达时间来排,不能直接用最短剩余时间排。
输出不正确的原因是:-
除非进程已经到达队列,否则不能将其调度到就绪队列。
此外,您的预期输出图表也不正确。如果只是,需要首先实现最短剩余时间,没有任何时间片,那么甘特图看起来像:-
| P1 | P2 | P4 | P3 |
0 8 12 17 26
现在,我不会评论你的代码中的错误,因为它看起来太多了,但是,我会建议你有更好的方法。
a) You should first check the arrival-time for each process starting with
0 and select the shortest burst-time job first,let it execute completely.
b) After completion of first job, then again check for the arrival-time of all the processes and then select the shortest burst-time process(among the processes who have arrived)
c) Iterate as mentioned for n number of processes.select the shortest burst-time process and complete it entirely and then follow step a,b and c until the processes end up.
如果您在执行相同内容时遇到问题,请就此提出新的问题。
我的名为 myArr 的数组包含
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
我应该得到以下输出
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");
}
我认为我的 Min() 方法弄乱了我的输出
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;
}
需要按到达时间来排,不能直接用最短剩余时间排。
输出不正确的原因是:-
除非进程已经到达队列,否则不能将其调度到就绪队列。
此外,您的预期输出图表也不正确。如果只是,需要首先实现最短剩余时间,没有任何时间片,那么甘特图看起来像:-
| P1 | P2 | P4 | P3 |
0 8 12 17 26
现在,我不会评论你的代码中的错误,因为它看起来太多了,但是,我会建议你有更好的方法。
a) You should first check the arrival-time for each process starting with 0 and select the shortest burst-time job first,let it execute completely.
b) After completion of first job, then again check for the arrival-time of all the processes and then select the shortest burst-time process(among the processes who have arrived)
c) Iterate as mentioned for n number of processes.select the shortest burst-time process and complete it entirely and then follow step a,b and c until the processes end up.
如果您在执行相同内容时遇到问题,请就此提出新的问题。