SPOS - First Come First Served [FCFS] Program

 FCFS Program

Code :

import java.util.Scanner;
class fcfs{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Number of Processes: ");
int n = sc.nextInt();
int i=0;
int [] at = new int [n];
int [] bt = new int [n];
int [] ct = new int [n];
int [] id = new int [n];
int [] tr = new int [n];
int [] wt = new int [n];
float avgwt = 0 , avgtr = 0;
while(i<n){
System.out.println("Enter arrival time for process: " + (i+1));
at[i] = sc.nextInt();
System.out.println("Enter the burst time: ");
bt[i] = sc.nextInt();
id[i] = i+1;
i++;
}
//calculating complete time
for(i=0 ; i<n ; i++){
if(i==0){
ct[i] = at[i] + bt[i];
}
else{
if(at[i]> ct[i-1]){
ct[i] = at[i] - bt[i];
}
else{
ct[i] = ct[i-1] + bt[i];
}
}
tr[i] = ct[i] - at[i];
wt[i] = tr[i] - bt[i];
avgwt += wt[i];
avgtr += tr[i];
}System.out.println("\nID Arrival Burst Complete Turn Waiting ");
for(i=0; i<n ; i++){
System.out.println(id[i] + "\t" + at[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + tr[i] + "\t" + wt[i] );
}
sc.close();
System.out.println("Average Waiting Time: " + avgwt);
System.out.println("Average Turn Around Time: "+ avgtr);
}
}

Output :



Your review for this post?