Open In App

Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)

 

The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive.  

Characteristics of SJF Scheduling:



Algorithm: 

Shortest Job First Scheduling Algorithm

How to compute below times in SJF using a program? 



Program for Non-Preemptive Shortest Job First  CPU Scheduling

Non-Preemptive Shortest Job First algorithm can be implemented using Segment Trees data structure. For detailed implementation of Non-Preemptive Shortest Job First scheduling algorithm, please refer: Program for Non-Preemptive Shortest Job First CPU Scheduling. 

In this post, we have assumed arrival times as 0, so turn around and completion times are same.

Examples to show working of Non-Preemptive Shortest Job First CPU Scheduling Algorithm:

Example-1: Consider the following table of arrival time and burst time for five processes P1, P2, P3, P4 and P5

Process Burst Time Arrival Time
 P1    6 ms 2 ms
 P2  2 ms 5 ms
 P3  8 ms 1 ms
 P4  3 ms 0 ms
 P5  4 ms 4 ms

The Shortest Job First CPU Scheduling Algorithm will work on the basis of steps as mentioned below:

At time = 0,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
0-1ms P4 0ms   1ms 3ms 2ms

At time= 1, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
1-2ms P4 0ms   1ms 2ms 1ms
P3 1ms P3 0ms 8ms 8ms

At time =2, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
2-3ms P4 0ms   1ms 1ms 0ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3, P1 0ms 6ms 6ms

At time = 3, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
3-4ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 6ms 5ms

At time = 4, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
4-5ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 5ms 4ms
P5 4ms P3, P5 0ms 4ms 4ms

At time = 5, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
5-6ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 4ms 3ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms

At time = 6, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
6-9ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 3ms 3ms 0ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms

At time=9, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
9-11ms P3 1ms P3 0ms 8ms 8ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5 2ms 2ms 0ms

At time = 11, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
11-15ms P3 1ms P3 0ms 8ms 8ms
P5 4ms P3 4ms 4ms 0ms

At time = 15,

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
15-23ms P3 1ms   8ms 8ms 0ms

At time = 23, 

Time Instance Process Arrival Time Waiting Table Execution Time Initial Burst Time Remaining Burst 
Time
0-1ms P4 0ms   1ms 3ms 2ms
1-2ms P4 0ms   1ms 2ms 1ms
P3 1ms P3 0ms 8ms 8ms
2-3ms P4 0ms   1ms 1ms 0ms
P3 1ms P3 0ms 8ms 8ms
P1 2ms P3, P1 0ms 6ms 6ms
3-4ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 6ms 5ms
4-5ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 5ms 4ms
P5 4ms P3, P5 0ms 4ms 4ms
5-6ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 1ms 4ms 3ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms
6-9ms P3 1ms P3 0ms 8ms 8ms
P1 2ms P3 3ms 3ms 0ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5, P2 0ms 2ms 2ms
9-11ms P3 1ms P3 0ms 8ms 8ms
P5 4ms P3, P5 0ms 4ms 4ms
P2 5ms P3, P5 2ms 2ms 0ms
11-15ms P3 1ms P3 0ms 8ms 8ms
P5 4ms P3 4ms 4ms 0ms
15-23ms P3 1ms   8ms 8ms 0ms

Gantt chart for above execution:

Gantt chart

Now, let’s calculate the average waiting time for above example:

P4 = 0 – 0 = 0

P1 = 3 – 2 = 1

P2 = 9 – 5 = 4

P5 = 11 – 4 = 7

P3 = 15 – 1 = 14

Average Waiting Time = 0 + 1 + 4 + 7 + 14/5 = 26/5 = 5.2

Advantages of SJF:

Disadvantages of SJF: 

Implementation of SJF Algorithm in C




#include <iostream>
using namespace std;
 
int main() {
   
    // Matrix for storing Process Id, Burst
    // Time, Average Waiting Time & Average
    // Turn Around Time.
    int A[100][4];
    int i, j, n, total = 0, index, temp;
    float avg_wt, avg_tat;
 
    cout << "Enter number of process: ";
    cin >> n;
 
    cout << "Enter Burst Time:" << endl;
   
    // User Input Burst Time and alloting Process Id.
    for (i = 0; i < n; i++) {
        cout << "P" << i + 1 << ": ";
        cin >> A[i][1];
        A[i][0] = i + 1;
    }
 
    // Sorting process according to their Burst Time.
    for (i = 0; i < n; i++) {
        index = i;
        for (j = i + 1; j < n; j++)
            if (A[j][1] < A[index][1])
                index = j;
        temp = A[i][1];
        A[i][1] = A[index][1];
        A[index][1] = temp;
 
        temp = A[i][0];
        A[i][0] = A[index][0];
        A[index][0] = temp;
    }
 
    A[0][2] = 0;
    // Calculation of Waiting Times
    for (i = 1; i < n; i++) {
        A[i][2] = 0;
        for (j = 0; j < i; j++)
            A[i][2] += A[j][1];
        total += A[i][2];
    }
 
    avg_wt = (float)total / n;
    total = 0;
    cout << "P     BT     WT     TAT" << endl;
   
    // Calculation of Turn Around Time and printing the
    // data.
    for (i = 0; i < n; i++) {
        A[i][3] = A[i][1] + A[i][2];
        total += A[i][3];
        cout << "P" << A[i][0] << "     " << A[i][1] << "     " << A[i][2] << "      " << A[i][3] << endl;
    }
 
    avg_tat = (float)total / n;
    cout << "Average Waiting Time= " << avg_wt << endl;
    cout << "Average Turnaround Time= " << avg_tat << endl;
}




#include <stdio.h>
int main()
{
    // Matrix for storing Process Id, Burst
    // Time, Average Waiting Time & Average
    // Turn Around Time.
    int A[100][4];
    int i, j, n, total = 0, index, temp;
    float avg_wt, avg_tat;
    printf("Enter number of process: ");
    scanf("%d", &n);
    printf("Enter Burst Time:\n");
    // User Input Burst Time and alloting Process Id.
    for (i = 0; i < n; i++) {
        printf("P%d: ", i + 1);
        scanf("%d", &A[i][1]);
        A[i][0] = i + 1;
    }
    // Sorting process according to their Burst Time.
    for (i = 0; i < n; i++) {
        index = i;
        for (j = i + 1; j < n; j++)
            if (A[j][1] < A[index][1])
                index = j;
        temp = A[i][1];
        A[i][1] = A[index][1];
        A[index][1] = temp;
 
        temp = A[i][0];
        A[i][0] = A[index][0];
        A[index][0] = temp;
    }
    A[0][2] = 0;
    // Calculation of Waiting Times
    for (i = 1; i < n; i++) {
        A[i][2] = 0;
        for (j = 0; j < i; j++)
            A[i][2] += A[j][1];
        total += A[i][2];
    }
    avg_wt = (float)total / n;
    total = 0;
    printf("P     BT     WT     TAT\n");
    // Calculation of Turn Around Time and printing the
    // data.
    for (i = 0; i < n; i++) {
        A[i][3] = A[i][1] + A[i][2];
        total += A[i][3];
        printf("P%d     %d     %d      %d\n", A[i][0],
               A[i][1], A[i][2], A[i][3]);
    }
    avg_tat = (float)total / n;
    printf("Average Waiting Time= %f", avg_wt);
    printf("\nAverage Turnaround Time= %f", avg_tat);
}




import java.io.*
import java.util.*;
 
public class Main {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int n;
        // Matrix for storing Process Id, Burst
        // Time, Average Waiting Time & Average
        // Turn Around Time.
        int[][] A = new int[100][4];
        int total = 0;
        float avg_wt, avg_tat;
        System.out.println("Enter number of process:");
        n = input.nextInt();
        System.out.println("Enter Burst Time:");
        for (int i = 0; i < n; i++) {
            // User Input Burst Time and alloting
            // Process Id.
            System.out.print("P" + (i + 1) + ": ");
            A[i][1] = input.nextInt();
            A[i][0] = i + 1;
        }
        for (int i = 0; i < n; i++) {
            // Sorting process according to their
            // Burst Time.
            int index = i;
            for (int j = i + 1; j < n; j++) {
                if (A[j][1] < A[index][1]) {
                    index = j;
                }
            }
            int temp = A[i][1];
            A[i][1] = A[index][1];
            A[index][1] = temp;
            temp = A[i][0];
            A[i][0] = A[index][0];
            A[index][0] = temp;
        }
        A[0][2] = 0;
        // Calculation of Waiting Times
        for (int i = 1; i < n; i++) {
            A[i][2] = 0;
            for (int j = 0; j < i; j++) {
                A[i][2] += A[j][1];
            }
            total += A[i][2];
        }
        avg_wt = (float)total / n;
        total = 0;
        // Calculation of Turn Around Time and printing the
        // data.
        System.out.println("P\tBT\tWT\tTAT");
        for (int i = 0; i < n; i++) {
            A[i][3] = A[i][1] + A[i][2];
            total += A[i][3];
            System.out.println("P" + A[i][0] + "\t"
                               + A[i][1] + "\t" + A[i][2]
                               + "\t" + A[i][3]);
        }
        avg_tat = (float)total / n;
        System.out.println("Average Waiting Time= "
                           + avg_wt);
        System.out.println("Average Turnaround Time= "
                           + avg_tat);
    }
}




# converting the code to python3
 
 
def main():
    # Taking the number of processes
    n = int(input("Enter number of process: "))
    # Matrix for storing Process Id, Burst Time, Average Waiting Time & Average Turn Around Time.
    A = [[0 for j in range(4)] for i in range(100)]
    total, avg_wt, avg_tat = 0, 0, 0
    print("Enter Burst Time:")
    for i in range(n):  # User Input Burst Time and alloting Process Id.
        A[i][1] = int(input(f"P{i+1}: "))
        A[i][0] = i + 1
    for i in range(n):  # Sorting process according to their Burst Time.
        index = i
        for j in range(i + 1, n):
            if A[j][1] < A[index][1]:
                index = j
        temp = A[i][1]
        A[i][1] = A[index][1]
        A[index][1] = temp
        temp = A[i][0]
        A[i][0] = A[index][0]
        A[index][0] = temp
    A[0][2] = 0  # Calculation of Waiting Times
    for i in range(1, n):
        A[i][2] = 0
        for j in range(i):
            A[i][2] += A[j][1]
        total += A[i][2]
    avg_wt = total / n
    total = 0
    # Calculation of Turn Around Time and printing the data.
    print("P     BT     WT     TAT")
    for i in range(n):
        A[i][3] = A[i][1] + A[i][2]
        total += A[i][3]
        print(f"P{A[i][0]}     {A[i][1]}     {A[i][2]}      {A[i][3]}")
    avg_tat = total / n
    print(f"Average Waiting Time= {avg_wt}")
    print(f"Average Turnaround Time= {avg_tat}")
 
 
if __name__ == "__main__":
    main()




//C# equivalents
 
using System;
 
namespace Main
{
  class Program
  {
    static void Main(string[] args)
    {
      // Matrix for storing Process Id, Burst
      // Time, Average Waiting Time & Average
      // Turn Around Time.
      int[,] A = new int[100, 4];
      int n;
      int total = 0;
      float avg_wt, avg_tat;
      Console.WriteLine("Enter number of process:");
      n = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Enter Burst Time:");
      for (int i = 0; i < n; i++)
      {
        // User Input Burst Time and alloting
        // Process Id.
        Console.Write("P" + (i + 1) + ": ");
        A[i, 1] = Convert.ToInt32(Console.ReadLine());
        A[i, 0] = i + 1;
      }
      for (int i = 0; i < n; i++)
      {
        // Sorting process according to their
        // Burst Time.
        int index = i;
        for (int j = i + 1; j < n; j++)
        {
          if (A[j, 1] < A[index, 1])
          {
            index = j;
          }
        }
        int temp = A[i, 1];
        A[i, 1] = A[index, 1];
        A[index, 1] = temp;
        temp = A[i, 0];
        A[i, 0] = A[index, 0];
        A[index, 0] = temp;
      }
      A[0, 2] = 0;
      // Calculation of Waiting Times
      for (int i = 1; i < n; i++)
      {
        A[i, 2] = 0;
        for (int j = 0; j < i; j++)
        {
          A[i, 2] += A[j, 1];
        }
        total += A[i, 2];
      }
      avg_wt = (float)total / n;
      total = 0;
      // Calculation of Turn Around Time and printing the
      // data.
      Console.WriteLine("P\tBT\tWT\tTAT");
      for (int i = 0; i < n; i++)
      {
        A[i, 3] = A[i, 1] + A[i, 2];
        total += A[i, 3];
        Console.WriteLine("P" + A[i, 0] + "\t"
                          + A[i, 1] + "\t" + A[i, 2]
                          + "\t" + A[i, 3]);
      }
      avg_tat = (float)total / n;
      Console.WriteLine("Average Waiting Time= "
                        + avg_wt);
      Console.WriteLine("Average Turnaround Time= "
                        + avg_tat);
    }
  }
}




// Javascript code
let A = [];
let total = 0;
let index, temp;
let avg_wt, avg_tat;
let n = prompt("Enter the number of process: ");
 
for (let i = 0; i < n; i++) {
    let input = prompt("P" + (i + 1) + ": ");
    A.push([i+1, parseInt(input)]);
}
 
for (let i = 0; i < n; i++) {
    index = i;
    for (let j = i + 1; j < n; j++) {
        if (A[j][1] < A[index][1]) {
            index = j;
        }
    }
    temp = A[i][1];
    A[i][1] = A[index][1];
    A[index][1] = temp;
 
    temp = A[i][0];
    A[i][0] = A[index][0];
    A[index][0] = temp;
}
 
A[0][2] = 0;
for (let i = 1; i < n; i++) {
    A[i][2] = 0;
    for (let j = 0; j < i; j++) {
        A[i][2] += A[j][1];
    }
    total += A[i][2];
}
 
avg_wt = total / n;
total = 0;
console.log("P     BT     WT     TAT");
for (let i = 0; i < n; i++) {
    A[i][3] = A[i][1] + A[i][2];
    total += A[i][3];
    console.log("P" + A[i][0] + "     " + A[i][1] + "     " + A[i][2] + "      " + A[i][3]);
}
 
avg_tat = total / n;
console.log("Average Waiting Time= " + avg_wt);
console.log("Average Turnaround Time= " + avg_tat);

Time Complexity: O(n^2)

Auxiliary Space: O(n)

Note: In this post, we have assumed arrival times as 0, so turn around and completion times are same.

In Set-2 we will discuss the preemptive version of SJF i.e. Shortest Remaining Time First


Article Tags :