Java Program for Shortest Job First (SJF) Scheduling [Preemptive & Non-Preemptive]

Here you will get java program for shortest job first (sjf) scheduling algorithm, both preemptive and non-preemptive.

Shortest job first scheduling algorithm can also be known as shortest job next scheduling. It is very easy to implement and efficient in reducing average response time. Now we will see how it will work with the example and its implementation.

In shortest job first, we should know the execution time of each process before running. This we can estimate in many ways. This is the prerequisite for SJF.

Also Read: Java Program for First Come First Serve (FCFS) Scheduling Algorithm

Suppose we have set of processes are in ready queue. The SJF scheduling algorithm will choose the job which has shortest remaining time to complete. We have 2 variations of this SJF algorithm that are preemptive and non-preemptive. Preemptive version of SJF also known as SRTF.

Non-Preemptive

Example:

Process id

Arrival time

Burst time

P1 0 3
P2 0 1
P3 0 2

We have 3 processes in our ready queue. As we discussed SJF will schedule the job which is having least execution time or burst time.

Gantt Chart

sjf 1

Now we will calculate the completion time, waiting time, turnaround time of each process.

Process id Completion time Waiting time Turnaround time
P1 6 3 6
P2 1 0 1
P3 3 1 3

Average waiting time = (3+0+1)/3 = 1.33

Turnaround time = (6+1+3)/3 = 3.33

Java Program for Shortest Job First (SJF) Scheduling (Non-Preemptive)

import java.util.*;

public class SJF {
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		System.out.println ("enter no of process:");
		int n = sc.nextInt();
		int pid[] = new int[n];
		int at[] = new int[n]; // at means arrival time
		int bt[] = new int[n]; // bt means burst time
		int ct[] = new int[n]; // ct means complete time
		int ta[] = new int[n]; // ta means turn around time
		int wt[] = new int[n];  //wt means waiting time
		int f[] = new int[n];  // f means it is flag it checks process is completed or not
		int st=0, tot=0;
		float avgwt=0, avgta=0;

		for(int i=0;i<n;i++)
		{
			System.out.println ("enter process " + (i+1) + " arrival time:");
			at[i] = sc.nextInt();
			System.out.println ("enter process " + (i+1) + " brust time:");
			bt[i] = sc.nextInt();
			pid[i] = i+1;
			f[i] = 0;
		}
		
		boolean a = true;
		while(true)
		{
			int c=n, min=999;
			if (tot == n) // total no of process = completed process loop will be terminated
				break;
			
			for (int i=0; i<n; i++)
			{
				/*
				 * If i'th process arrival time <= system time and its flag=0 and burst<min 
				 * That process will be executed first 
				 */ 
				if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min))
				{
					min=bt[i];
					c=i;
				}
			}
			
			/* If c==n means c value can not updated because no process arrival time< system time so we increase the system time */
			if (c==n) 
				st++;
			else
			{
				ct[c]=st+bt[c];
				st+=bt[c];
				ta[c]=ct[c]-at[c];
				wt[c]=ta[c]-bt[c];
				f[c]=1;
				tot++;
			}
		}
		
		System.out.println("\npid  arrival brust  complete turn waiting");
		for(int i=0;i<n;i++)
		{
			avgwt+= wt[i];
			avgta+= ta[i];
			System.out.println(pid[i]+"\t"+at[i]+"\t"+bt[i]+"\t"+ct[i]+"\t"+ta[i]+"\t"+wt[i]);
		}
		System.out.println ("\naverage tat is "+ (float)(avgta/n));
		System.out.println ("average wt is "+ (float)(avgwt/n));
		sc.close();
	}
}

Output

sjf 2

Advantage:

  • It will result minimum average waiting time to each process.

Disadvantage:

  • It will create starvation problem to long process.
  • Execution time should be estimated prior to the scheduling. It will incur some cost.

Now we will see preemptive version of SJF that is SRTF (shortest remaining tie first)

Preemptive

In SRTF the selection of job is same like in SJF. But the difference is In SJF process will run till completion. In SRTF process will run till completion or a new process added into queue which is having smaller execution time than the current process remaining execution time.

Process id Arrival time  Burst time
P1 2 3
P2 1 2
P3 3 4
P4 5 6

When process is added to queue or process is completed then only CPU may switch the process.

Gantt Chart

sjf 3

Process id Completion time Waiting time Turnaround time
P1 6 1 4
P2 3 0 2
P3 10 3 7
P4 16 5 11

Average turnaround time = (4+2+7+11)/4 = 6.0

Average waiting time = (1+0+3+5)/4 = 2.25

Java Program for Shortest Job First (SRTF) Scheduling (Preemptive)

import java.util.*;

public class SRTF {
	public static void main (String args[])
	{
		Scanner sc=new Scanner(System.in);
		System.out.println ("enter no of process:");
		int n= sc.nextInt();
		int pid[] = new int[n]; // it takes pid of process
		int at[] = new int[n]; // at means arrival time
		int bt[] = new int[n]; // bt means burst time
		int ct[] = new int[n]; // ct means complete time
		int ta[] = new int[n];// ta means turn around time
		int wt[] = new int[n];  // wt means waiting time
		int f[] = new int[n];  // f means it is flag it checks process is completed or not
		int k[]= new int[n];   // it is also stores brust time
	    int i, st=0, tot=0;
	    float avgwt=0, avgta=0;

	    for (i=0;i<n;i++)
	    {
	    	pid[i]= i+1;
	    	System.out.println ("enter process " +(i+1)+ " arrival time:");
	    	at[i]= sc.nextInt();
	    	System.out.println("enter process " +(i+1)+ " burst time:");
	    	bt[i]= sc.nextInt();
	    	k[i]= bt[i];
	    	f[i]= 0;
	    }
	    
	    while(true){
	    	int min=99,c=n;
	    	if (tot==n)
	    		break;
	    	
	    	for ( i=0;i<n;i++)
	    	{
	    		if ((at[i]<=st) && (f[i]==0) && (bt[i]<min))
	    		{	
	    			min=bt[i];
	    			c=i;
	    		}
	    	}
	    	
	    	if (c==n)
	    		st++;
	    	else
	    	{
	    		bt[c]--;
	    		st++;
	    		if (bt[c]==0)
	    		{
	    			ct[c]= st;
	    			f[c]=1;
	    			tot++;
	    		}
	    	}
	    }
	    
	    for(i=0;i<n;i++)
	    {
	    	ta[i] = ct[i] - at[i];
	    	wt[i] = ta[i] - k[i];
	    	avgwt+= wt[i];
	    	avgta+= ta[i];
	    }
	    
	    System.out.println("pid  arrival  burst  complete turn waiting");
	    for(i=0;i<n;i++)
	    {
	    	System.out.println(pid[i] +"\t"+ at[i]+"\t"+ k[i] +"\t"+ ct[i] +"\t"+ ta[i] +"\t"+ wt[i]);
	    }
	    
	    System.out.println("\naverage tat is "+ (float)(avgta/n));
	    System.out.println("average wt is "+ (float)(avgwt/n));
	    sc.close();
	}
}

Output

sjf 4

Advantages:

  • Shorter jobs will be scheduled quickly.
  • Less average waiting time.

Disadvantage:

  • Starvation for longer jobs.

Comment below if you have doubts related to above program for sjf in Java.

9 thoughts on “Java Program for Shortest Job First (SJF) Scheduling [Preemptive & Non-Preemptive]”

  1. Bro plz provide round robin algorithm in Java
    Your way of programming is awesome
    Easier to understand thanks alot plz provide
    Round robin algorithm in Java find it in 2 to3 hr
    If u find it thane send me as mail

  2. What is the variable ‘min’ for?

    Why is “min=999” only?

    Can ‘min’ be of any different value? If not, why?

Leave a Comment

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