Monday 7 August 2017

sjf.java (OS)

import java.util.Scanner;

class sjf
{
public static void main(String ... args)
{
int n,curTime=0,sumTAT=0,sumWT=0,tmp;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of process : ");
n = sc.nextInt();
n=5;
int id[]=new int[n];
int BT[]=new int[n];
int WT[]=new int[n];
int TAT[]=new int[n];
for(int i=0; i<n; i++)
{
id[i]=(i+1);
System.out.println("Enter BT of #"+(i+1)+"No.: ");
BT[i]=sc.nextInt();
}
for(int i=0; i<(n-1); i++)
{
for(int j=0; j<(n-i-1); j++)
{
if(BT[j]>BT[j+1])
{
tmp=BT[j];
BT[j]=BT[j+1];
BT[j+1]=tmp;
tmp=id[j];
id[j]=id[j+1];
id[j+1]=tmp;
}
}
}
for(int i=0; i<n; i++)
{
WT[i]= curTime;
curTime+=BT[i];
TAT[i]= curTime;

sumWT+=WT[i];
sumTAT+=TAT[i];
}
System.out.println("AT\tBT\tWT\tTAT\n---------------------------------------");
for(int i=0; i<n; i++)
System.out.println(id[i]+"\t"+BT[i]+"\t"+WT[i]+"\t"+TAT[i]);
System.out.println("AvgWT = " + ((float)  sumWT/n) +"\nAvgTAT = " +  ((float) sumTAT/n));
}
}

Output:

Enter number of process :
5
Enter BT of #1No.:
2
Enter BT of #2No.:
8
Enter BT of #3No.:
7
Enter BT of #4No.:
4
Enter BT of #5No.:
3
AT      BT      WT      TAT
---------------------------------------
1       2       0       2
5       3       2       5
4       4       5       9
3       7       9       16
2       8       16      24
AvgWT = 6.4
AvgTAT = 11.2

No comments:

Post a Comment