Implementation of Non-Preemptive Shortest Job First using Priority Queue

Read here for Shortest Job First Scheduling algorithm for same arrival times.

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.

In this article, we will implement the Shortest Job First Scheduling algorithm (SJF) using a priority queue, so that we can handle processes at different arrival time.



Examples:

Input:  The processes are
Process id    Arrival time    Burst time    
    p1         4                  3        
    p2         0                  8        
    p3         5                  4        
    p4         9                  2    

Output: Process scheduling according to SJF is
Process id    Arrival time    Burst time    
    p2         0                  8        
    p1         4                  3        
    p4         9                  2        
    p3         5                  4    


Input: The processes are
Process id    Arrival time    Burst time    
    p1         0                  3        
    p2         0                  8        
    p3         5                  4        
    p4         9                  2

Output: Process scheduling according to SJF is
Process id    Arrival time    Burst time    
    p1         0                  3        
    p2         0                  8        
    p4         9                  2        
    p3         5                  4    

In this program, the task is to schedule the processes according to SJF scheduling algorithm, which states that the process with minimum burst time will be given priority, which can simply be implemented by sorting the burst time of the processes in ascending order. The problem arises when we have to handle the processes at different arrival time, then we simply can’t sort the processes according to burst time as we need to consider the arrival time of the process so that the processor doesn’t stay idle.

Example:
If a process with more burst time arrives before a process with less burst time, then we have to allow the processor time to the process that arrived first so that the processor doesn’t stay idle.

Approach:
To handle processes with a different arrival time in case of SJF scheduling:

  • First, sort the processes according to the arrival time.
  • Maintain a wait queue, which keeps the process with minimum burst time at the top.
  • Maintain the current run time, that is the sum of burst times of the executed processes.
    • A process enters the wait queue according to it’s arrival time if a new process has arrival time less than
      equal to the current running time, it is pushed in the wait queue.
    • A process is popped from the wait queue when it is to be executed. It’s burst time is added to the current run time, it’s arrival time is updated to -1 so that it doesn’t enter the wait queue again.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of SJF
#include <bits/stdc++.h>
using namespace std;
  
// number of process
#define SIZE 4
  
// Structure to store the
// process information
typedef struct proinfo {
    string pname; // process name
    int atime; // arrival time
    int btime; // burst time
  
} proinfo;
  
// This structure maintains the
// wait queue, using burst
// time to compare.
typedef struct cmpBtime {
    int operator()(const proinfo& a,
                   const proinfo& b)
    {
        return a.btime > b.btime;
    }
} cmpBtime;
  
// This function schedules the
// process according to the SJF
// scheduling algorithm.
void sjfNonpremetive(proinfo* arr)
{
    // Used to sort the processes
    // according to arrival time
    int index = 0;
    for (int i = 0; i < SIZE - 1; i++) {
        index = i;
        for (int j = i + 1; j < SIZE; j++) {
            if (arr[j].atime
                < arr[index].atime) {
                index = j;
            }
        }
        swap(arr[i], arr[index]);
    }
  
    // ctime stores the current run time
    int ctime = arr[0].atime;
  
    // priority queue, wait, is used
    // to store all the processes that
    // arrive <= ctime (current run time)
    // this is a minimum priority queue
    // that arranges values according to
    // the burst time of the processes.
    priority_queue<proinfo, vector<proinfo>,
                   cmpBtime>
        wait;
  
    int temp = arr[0].atime;
  
    // The first process is
    // pushed in the wait queue.
    wait.push(arr[0]);
    arr[0].atime = -1;
  
    cout << "Process id"
         << "\t";
    cout << "Arrival time"
         << "\t";
    cout << "Burst time"
         << "\t";
  
    cout << endl;
  
    while (!wait.empty()) {
  
        cout << "\t";
        cout << wait.top().pname << "\t\t";
        cout << wait.top().atime << "\t\t";
        cout << wait.top().btime << "\t\t";
        cout << endl;
  
        // ctime is increased with
        // the burst time of the
        // currently executed process.
        ctime += wait.top().btime;
  
        // The executed process is
        // removed from the wait queue.
        wait.pop();
  
        for (int i = 0; i < SIZE; i++) {
            if (arr[i].atime <= ctime
                && arr[i].atime != -1) {
                wait.push(arr[i]);
  
                // When the process once
                // enters the wait queue
                // its arrival time is
                // assigned to -1 so that
                // it doesn't enter again
                // int the wait queue.
                arr[i].atime = -1;
            }
        }
    }
}
  
// Driver Code
int main()
{
    // an array of process info structures.
    proinfo arr[SIZE];
  
    arr[0] = { "p1", 4, 3 };
    arr[1] = { "p2", 0, 8 };
    arr[2] = { "p3", 5, 4 };
    arr[3] = { "p4", 9, 2 };
  
    cout << "Process scheduling ";
    cout << "according to SJF is: \n"
         << endl;
  
    sjfNonpremetive(arr);
}

chevron_right


Output:

Process scheduling according to SJF is: 

Process id    Arrival time    Burst time    
    p2         0                8        
    p1         4                3        
    p4         9                2        
    p3         5                4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.