FCFS Scheduling Algorithm Java implementation.
//Java program for FCFS scheduling algorithm.
import java.util.*;
class FCFS
{
public static void main(String args[])
{
int n, at[], bt[], wt[], tat[];
float avgT;
float awt;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of processes to be scheduled: ");
at=new int[10];
bt=new int[10];
wt=new int[10];
tat=new int[10];
n=sc.nextInt();
System.out.println("Enter arrival times of the processes:");
for(int i=0; i<n; i++)
{
at[i]=sc.nextInt();
}
System.out.println("Enter burst times of the processes: ");
for(int i=0; i<n; i++)
{
bt[i]=sc.nextInt();
}
wt[0]=0;
for(int i=1; i<n; i++)
{
wt[i]=wt[i-1]+bt[i-1];
wt[i]=wt[i]-at[i];
}
avgT=0;
awt=0;
for(int i=0; i<n; i++)
{
tat[i]=wt[i]+bt[i];
avgT=avgT+tat[i];
awt=awt+wt[i];
}
System.out.println("AT BT WT TAT");
for(int i=0; i<n; i++)
{
System.out.println(" "+at[i]+" "+bt[i]+" "+wt[i]+" "+tat[i]+" ");
}
awt=awt/n;
avgT=avgT/n;
System.out.println("Avg waiting time: "+awt);
System.out.println("Avg TAT: "+avgT);
}
}
/*OUTPUT:
Enter the number of processes to be scheduled:
4
Enter arrival times of the processes:
0
0
0
0
Enter burst times of the processes:
6
7
3
4
AT BT WT TAT
0 6 0 6
0 7 6 13
0 3 13 16
0 4 16 20
Avg waiting time: 8.75
Avg TAT: 13.75
import java.util.*;
class FCFS
{
public static void main(String args[])
{
int n, at[], bt[], wt[], tat[];
float avgT;
float awt;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of processes to be scheduled: ");
at=new int[10];
bt=new int[10];
wt=new int[10];
tat=new int[10];
n=sc.nextInt();
System.out.println("Enter arrival times of the processes:");
for(int i=0; i<n; i++)
{
at[i]=sc.nextInt();
}
System.out.println("Enter burst times of the processes: ");
for(int i=0; i<n; i++)
{
bt[i]=sc.nextInt();
}
wt[0]=0;
for(int i=1; i<n; i++)
{
wt[i]=wt[i-1]+bt[i-1];
wt[i]=wt[i]-at[i];
}
avgT=0;
awt=0;
for(int i=0; i<n; i++)
{
tat[i]=wt[i]+bt[i];
avgT=avgT+tat[i];
awt=awt+wt[i];
}
System.out.println("AT BT WT TAT");
for(int i=0; i<n; i++)
{
System.out.println(" "+at[i]+" "+bt[i]+" "+wt[i]+" "+tat[i]+" ");
}
awt=awt/n;
avgT=avgT/n;
System.out.println("Avg waiting time: "+awt);
System.out.println("Avg TAT: "+avgT);
}
}
/*OUTPUT:
Enter the number of processes to be scheduled:
4
Enter arrival times of the processes:
0
0
0
0
Enter burst times of the processes:
6
7
3
4
AT BT WT TAT
0 6 0 6
0 7 6 13
0 3 13 16
0 4 16 20
Avg waiting time: 8.75
Avg TAT: 13.75
Comments
Post a Comment