4
这是我们教授给我们的一个项目。先来先服务算法实现
要求是在JAVA中实现3个预先挑选的CPU调度算法。 我们的团队获得了FCFS(先来先服务),Round Robin和MFQ(多反馈队列)算法。
现在我做了这个FCFS代码:
import java.util.Vector;
public class FCFS
{
protected int[] arrivalTime;
protected int[] burstTime;
protected int[] job;
protected int[] jobIdle;
protected int numberOfProcess;
protected int[] waitingTime;
protected int[] finishedTime;
protected int averageWT,averageTTsum;
protected int jobs;
public FCFS (int[] aT,int[] bT,int[] job,int num)
{
arrivalTime = aT;
burstTime = bT;
this.job = job;
numberOfProcess = num;
waitingTime = new int[numberOfProcess];
finishedTime = new int[numberOfProcess];
jobs = 0;
}
public void FCFS()
{
int firstCome,tempArrivalTime,tempBurst;
//sort processes
for (int i = 0; i < (numberOfProcess - 1); i++)
{
for (int j = (i + 1); j < numberOfProcess; j++)
{
if (arrivalTime[j] < arrivalTime[i])
{
firstCome = job[j];
job[j] = job[i];
job[i] = firstCome;
tempArrivalTime = arrivalTime[j];
arrivalTime[j] = arrivalTime[i];
arrivalTime[i] = tempArrivalTime;
tempBurst = burstTime[j];
burstTime[j] = burstTime[i];
burstTime[i] = tempBurst;
}
}
}
System.out.println("\n==Displaying Table of Jobs Sorted According to Arrival Time==");
displaySorted();
System.out.println("======DISPLAYING GANTT CHART======");
solveWaitingTime();
solveAverageTime();
}
public void solveAverageTime()
{
//ATT
for(int i = 0;i<numberOfProcess;i++)
averageTTsum = averageTTsum+(finishedTime[i]-arrivalTime[i]);
//AWT
for(int j=0;j<numberOfProcess;j++)
averageWT = averageWT+(waitingTime[j] - arrivalTime[j]);
double aTT = (float)averageTTsum/numberOfProcess;
double aWT=(float)averageWT/numberOfProcess;
System.out.format("ATT: %.2f ",aTT);
System.out.println("");
System.out.format("AWT: %.2f ",aWT);
}
public void solveWaitingTime()
{ int ctr=0;
Vector<Integer> idleWT = new Vector<Integer>();
Vector<Boolean> idle = new Vector<Boolean>();
for(int z = 0; z < numberOfProcess; z++) //run through all processes
{
if(ctr > arrivalTime[z]) //if counter is greater than the arrival time
{idle.add(Boolean.TRUE); //an idle time is not needed hence TRUE
for(int k = 0; k < burstTime[z]; k++) //do burst time of current process
{
ctr++;
}
jobs++;
}
else //if counter is less than arrival time
{
while(ctr <= arrivalTime[z])
{
if(ctr == arrivalTime[z]) //if counter is equal to arrivalTime
{
jobs++;
for(int j = arrivalTime[z]; j < (arrivalTime[z] + burstTime[z]); j++)//starting from arrival time
{ //do the burst time of process
ctr++;
}
idle.add(Boolean.TRUE);
}
else //if not equal to arrival time
{
jobs++;
ctr++; //an idle time will be consumed
idle.add(Boolean.FALSE); //idle has been detected
}
}
}
finishedTime[z] = ctr; //turn-around time is = to total counter
if(z==0) //if time is 0
idleWT.add(0); //IdlewaitingTime of first process is 0
else idleWT.add(ctr); //else ctr
}
waitingTime[0] = 0;
for(int z = 1;z<numberOfProcess;z++)
{
waitingTime[z] = finishedTime[z] - burstTime[z];//compute waiting time
}
//debugging purposes
/* for(int i = 0;i<numberOfProcess;i++)
{ System.out.print("arrival: "+arrivalTime[i]);
System.out.print("burst: "+burstTime[i]);
System.out.print("wait: "+waitingTime[i]);
System.out.print("finished: "+finishedTime[i]);
}*/
System.out.println(""+idleWT.toString()); //debugging purposes
System.out.println(""+idle.toString()); //debugging purposes
System.out.println("Jobs: "+jobs); //debugging purposes
int ctr2 = 0;
for(int y = 0; y < numberOfProcess; y++) //displays gannt Chart
{
if(idle.elementAt(ctr2)==false) //if an idle time is detected
{ if(ctr2==0)
{System.out.print("|I"+(waitingTime[y+1])+" |"); ctr2++;} //print an I to symbolize idle time and its burst time
else {
System.out.print("|I "+(idleWT.elementAt(y)-waitingTime[y])+" |");
ctr2++;
}
}
System.out.print("|P"+job[y]+"("+burstTime[y]+")"+" |"); //else print name of processes
ctr2++;
}
System.out.println("");
//gantt chart time display
for(int x = 0;x<numberOfProcess;x++)
{ if(idleWT.elementAt(x) == 0)
System.out.print(""+waitingTime[x]);
else System.out.print(" "+waitingTime[x]+ " "+idleWT.elementAt(x));
}
System.out.println("");
}
public void displaySorted()
{
System.out.println("\n------------------------------------");
System.out.println("Process | Arrival Time | CPU Burst ");
for(int y = 0; y < numberOfProcess; y++)
{
System.out.println("P" + job[y] + "\t\t"
+ arrivalTime[y] +"\t "
+ burstTime[y]);
}
System.out.println("------------------------------------\n");
}
}
的输出应该是这样的:
Enter Number of processes: 5 //sample only
Enter Arrival time of p1:
Enter Burst Time of p1:
.
.
.
.
.
.
.
.
Enter Arrival time of p5:
Enter Burst Time of p5:
==Displaying Table of Jobs Sorted According to Arrival Time==
------------------------------------
Process | Arrival Time | CPU Burst
p1
p2
p3
p4
p5
======DISPLAYING GANTT CHART======
p1(burst) | p2(burst2) | .....
0 burst| burst2| .....
AWT:
ATT:
现在我有我的代码的问题,可能忽略了太多看问题和这些是:
- 它不记录空闲时间的正确突发时间
- 它不显示适当数量的海啸图中的空闲时间
- 我认为会有一个输入,将完全搞乱我的程序。
- 此外,我已经宣布了一些变量,起初我认为我可以使用,但后来我改变了主意,决定不使用它们。
- 我也觉得这个算法在我的逻辑中存在缺陷,请指出来,这对我的学习会有很大的帮助。
我该如何解决这些问题? 我希望我已经提供了足够的信息。
你的代码太乱了。我猜在处理数组索引时可能会出现一些错误?你可能想考虑做一些重构(比如说,引入一个Job,JobStatistics和一个CPU类来帮助计算你的时间片);也许它可能会帮助你发现你的错误。 – happymeal
张贴您的主要() – suenda
除了@happymeal所说的,FCFS系统的基础是一个简单的过程队列。除非教授另有指示,否则您应该使用更合适的数据结构来处理流程(作业)的排队和出队。 – LJ2