Java Program for Shortest Job First (SJF) Scheduling Algorithm

About Shortest Job First (SJF) Scheduling

This is a non-preemptive scheduling algorithm and best approach to minimize waiting time.

Program to implement CPU scheduling for shortest job first.

You might also like: FCFS program in Java

Program


import java.util.Scanner;

class TestClass{
public static void main(String args[]){
int burst_time[],process[],waiting_time[],tat[],i,j,n,total=0,pos,temp;
float wait_avg,TAT_avg;
Scanner s = new Scanner(System.in);

System.out.print("Enter number of process: ");
n = s.nextInt();

process = new int[n];
burst_time = new int[n];
waiting_time = new int[n];
tat = new int[n];

System.out.println("\nEnter Burst time:");
for(i=0;i<n;i++)
{
System.out.print("\nProcess["+(i+1)+"]: ");
burst_time[i] = s.nextInt();;
process[i]=i+1; //Process Number
}

//Sorting
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(burst_time[j]<burst_time[pos])
pos=j;
}

temp=burst_time[i];
burst_time[i]=burst_time[pos];
burst_time[pos]=temp;

temp=process[i];
process[i]=process[pos];
process[pos]=temp;
}



//First process has 0 waiting time
waiting_time[0]=0;
//calculate waiting time
for(i=1;i<n;i++)
{
waiting_time[i]=0;
for(j=0;j<i;j++)
waiting_time[i]+=burst_time[j];

total+=waiting_time[i];
}


//Calculating Average waiting time
wait_avg=(float)total/n;
total=0;

System.out.println("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=burst_time[i]+waiting_time[i]; //Calculating Turnaround Time
total+=tat[i];
System.out.println("\n p"+process[i]+"\t\t "+burst_time[i]+"\t\t "+waiting_time[i]+"\t\t "+tat[i]);
}

//Calculation of Average Turnaround Time
TAT_avg=(float)total/n;
System.out.println("\n\nAverage Waiting Time: "+wait_avg);
System.out.println("\nAverage Turnaround Time: "+TAT_avg);

}
}




Output

 

Enter number of process: 3

Enter Burst time:

Process[1]: 20

Process[2]: 30

Process[3]: 15

Process            Burst Time            Waiting Time        Turnaround Time

  p3                  15                   0                  15

  p1                  20                   15                  35

  p2                  30                   35                  65


Average Waiting Time: 16.666666

Average Turnaround Time: 38.333332
BUILD SUCCESSFUL (total time: 12 seconds)


 

 

More:  CPU Scheduling Algorithms

 

View this program in C++ :   C++ Program for Shortest Job First (SJF) Scheduling Algorithm

2 Replies to “Java Program for Shortest Job First (SJF) Scheduling Algorithm”

Leave a Reply

Your email address will not be published. Required fields are marked *