This is a project given to us by our professor.
The requirements are to implement 3 pre-picked algorithms of CPU Scheduling in JAVA.
our group was given FCFS(First Come First Serve),Round Robin,and MFQ(Multi-feedback Queue) algorithms.
now i had made this FCFS code:
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");
}
}
The supposed output is like this:
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:
now i'm having problems in my code and may have overlooked too much to see the problem and these are:
- It doesn't record the correct burst-time of the idle time
- It doesn't display right amount of idle times in gannt chart
- I think there will be an input that would totally mess up my program.
- Also i have declared some variables that at first i thought i can use but then i change my mind and decided to not use them.
- I also think there are flaws in my logic for this algorithm please point them out,it will help me greatly in school.
How do I solve for these problems??
I hope I have provided enough info.
Best Answer