First Come First Serve Algorithm Implementation [closed]
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开发者_Python百科 = 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FCFS_Console
{
class Program
{
static void Main(string[] args)
{
//----------------------------------------Reading I/O File--------------------------------------
string s = Environment.CurrentDirectory.ToString(); // returns the directory of the exe file
if (File.Exists(s + @"\input.txt")) //checking if the input files exists
Console.WriteLine("File Exists");
else
{
Console.WriteLine("File Not Found");
Console.WriteLine("_________________________________________________________________");
return;
}
Console.WriteLine("_________________________________________________________________");
//----------------------------------------Data Into List--------------------------------------
string FileText = File.ReadAllText(s + @"\input.txt"); //reading all the text in the input file
string[] lines = FileText.Split('\n'); //splitting the lines
List<Process> processes = new List<Process>();
for (int i = 1; i < lines.Length; i++)
{
string[] tabs = lines[i].Split('\t');//splitting the tabs to get objects' variables
Process x = new Process(tabs[0], int.Parse(tabs[1]), int.Parse(tabs[2]), int.Parse(tabs[3]));//creating object
processes.Add(x);//adding object to the list
}
// ----------------------------------------Sorting The List--------------------------------------
Process temp;
for (int k = 0; k < processes.Count; k++)
{
for (int i = k + 1; i < processes.Count; i++)
{
if (processes[k].arrivalTime > processes[i].arrivalTime || (processes[k].arrivalTime == processes[i].arrivalTime && processes[k].brust > processes[i].brust))
{
temp = processes[i];
processes[i] = processes[k];
processes[k] = temp;
}
}
}
Console.WriteLine("Processes After Sorting");
Console.WriteLine("_________________________________________________________________");
Console.WriteLine("Name\tArrival\tBrust\tPriority");
for (int i = 0; i < processes.Count; i++)
{
Console.Write(processes[i].name + "\t" + processes[i].arrivalTime + "\t" + processes[i].brust + "\t" + processes[i].priority);
Console.WriteLine();
}
Console.WriteLine("_________________________________________________________________");
//----------------------------------------Gantt Chart--------------------------------------
Console.WriteLine("Gantt Chart");
Console.WriteLine("_________________________________________________________________");
int counter = 0;
for (int i = 0; i < processes.Count; i++)
{
Console.Write(processes[i].name + "\t");
if (processes[i].arrivalTime < counter)
printSpaces(counter);
else
{
printSpaces(processes[i].arrivalTime);
counter = processes[i].arrivalTime;
}
printHashes(processes[i].brust);
counter += processes[i].brust;
Console.WriteLine();
}
Console.WriteLine("_________________________________________________________________");
//-----------------------------------Completing Data And final Table-------------------------
int clock = 0, totalwait = 0, totalturnAround = 0;
for (int i = 0; i < processes.Count; i++)
{
if (processes[i].arrivalTime > clock)
{
processes[i].start = processes[i].arrivalTime;
clock += processes[i].start - processes[i].arrivalTime;
clock += processes[i].brust;
}
else
{
if (i > 0)
processes[i].start = processes[i - 1].end;
clock += processes[i].brust;
}
if (processes[i].start > processes[i].arrivalTime)
processes[i].wait = processes[i].start - processes[i].arrivalTime;
else processes[i].wait = 0;
processes[i].end = processes[i].start + processes[i].brust;
processes[i].turnAround = processes[i].wait + processes[i].brust;
totalwait += processes[i].wait;
totalturnAround += processes[i].turnAround;
}
Console.WriteLine("Name\tArrival\tBrust\tStart\tEnd\tWait\tturnaround");
for (int i = 0; i < processes.Count; i++)
{
Console.Write(processes[i].name + "\t" + processes[i].arrivalTime + "\t" + processes[i].brust + "\t" + processes[i].start + "\t" + processes[i].end + "\t" + processes[i].wait + "\t" + processes[i].turnAround);
Console.WriteLine();
}
double att = 0, awt = 0;
awt = (double)totalwait / (double)processes.Count;
att = (double)totalturnAround / (double)processes.Count;
Console.WriteLine("A.W.T= " + awt + "\t A.T.T= " + att);
Console.ReadKey();
}
public static void printSpaces(int counter)
{
for (int i = 0; i < counter; i++)
{
Console.Write(" ");
}
}
public static void printHashes(int brust)
{
for (int i = 0; i < brust; i++)
{
Console.Write("#");
}
}
}
}
class Process
{
public Process(string name, int arrivalTime, int brust, int priority)
{
this.name = name;
this.arrivalTime = arrivalTime;
this.brust = brust;
this.priority = priority;
}
public Process()
{
}
public string name;
public int arrivalTime;
public int brust;
public int priority;
public int wait;
public int end;
public int start;
public int turnAround;
}
精彩评论