Open In App

Clairvoyant Shortest Job first (SJF)

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we discuss Clairvoyant SJF. It is a theoretical concept in which the algorithm looks in the future and waits for the shortest process to arrive, this results in the least average waiting time.

Difference between Clairvoyant SJF and Shortest Job First: Both algorithms work on the same principle of allocating CPU time to the shorter process. The difference lies in the fact that Clairvoyant can look into the future and wait for the shortest process and allocate the resource accordingly, whereas SJF has to allocate the resources to the process which have already arrived (i.e., are waiting for the ready queue). 

Examples:

Input: The processes are,  

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

Output: Process scheduling according to Clairvoyant SJF is,  

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

The average waiting time is: 2.5 

Output: Process scheduling according to Shortest Job First is, 

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

The average waiting time is : 2.75

Note: Clairvoyant SJF and SJF will give the same result if all the processes arrive at the same time.

Input: The processes are,  

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

Output: Process scheduling according to Clairvoyant SJF is,  

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

The average waiting time is : 5.5 

Output: Process scheduling according to Shortest Job First is,  

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

The average waiting time is: 5.5

Code: 

C++




#include <bits/stdc++.h>
#define SIZE 4
using namespace std;
 
// Structure to store the information about the process
typedef struct proinfo {
    string pname; // Process name
    int atime; // Arrival time
    int btime; // Burst time
} proinfo;
 
// This function schedules the process
// according to the Clairvoyant SJF scheduling algorithm.
void clairvoyantSjf(proinfo* arr)
{
    // To sort the processes according to the burst 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].btime < arr[index].btime) {
 
                index = j;
            }
        }
        swap(arr[i], arr[index]);
    }
}
 
void display(proinfo* arr)
{
    cout << endl;
    cout << "Process id"
        << "\t";
    cout << "Arrival time"
        << "\t";
    cout << "Burst time"
        << "\t";
    cout << endl;
    for (int i = 0; i < SIZE; i++) {
        cout << arr[i].pname << "\t\t";
        cout << arr[i].atime << "\t\t";
        cout << arr[i].btime << "\t\t";
        cout << endl;
    }
}
 
// To calculate the average waiting time
void avgWait(proinfo* arr)
{
    int ctime = 0;
    int twait = 0;
    for (int i = 0; i < SIZE; i++) {
        twait += abs(arr[i].atime - ctime);
        ctime += arr[i].btime;
    }
    cout << "The average waiting time is: " << (float)twait / SIZE << endl;
}
 
int main()
{
    // Array of process info structures.
    proinfo arr[SIZE];
    arr[0] = { "p1", 0, 5 };
    arr[1] = { "p2", 1, 2 };
    arr[2] = { "p3", 3, 1 };
    arr[3] = { "p4", 4, 3 };
 
    cout << "Process scheduling according to Clairvoyant SJF is: " << endl;
 
    clairvoyantSjf(arr);
    // To display the schedule
    display(arr);
    // to calculate the Average waiting time
    avgWait(arr);
}


Java




// Java implementation of the approach
public class GFG {
    static int SIZE = 4;
 
    // Class to store the information about the process
    static class proinfo {
        String pname; // Process name
        int atime; // Arrival time
        int btime; // Burst time
 
        proinfo(String pname, int atime, int btime)
        {
            this.pname = pname;
            this.atime = atime;
            this.btime = btime;
        }
    }
 
    // This function schedules the process
    // according to the Clairvoyant SJF scheduling
    // algorithm.
    static void clairvoyantSjf(proinfo[] arr)
    {
 
        // To sort the processes according to the burst 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].btime < arr[index].btime) {
 
                    index = j;
                }
            }
            swap(arr, i, index);
        }
    }
 
    static void swap(proinfo[] arr, int i, int index)
    {
        proinfo tmp = arr[i];
        arr[i] = arr[index];
        arr[index] = tmp;
    }
 
    static void display(proinfo[] arr)
    {
        System.out.println();
        System.out.print("Process id\t");
        System.out.print("Arrival time\t");
        System.out.println("Burst time\t");
        for (int i = 0; i < SIZE; i++) {
            System.out.print(arr[i].pname + "\t\t");
            System.out.print(arr[i].atime + "\t\t");
            System.out.println(arr[i].btime + "\t\t");
        }
    }
 
    // To calculate the average waiting time
    static void avgWait(proinfo[] arr)
    {
        int ctime = 0;
        int twait = 0;
        for (int i = 0; i < SIZE; i++) {
            twait += Math.abs(arr[i].atime - ctime);
            ctime += arr[i].btime;
        }
        System.out.println("The average waiting time is: "
                           + (float)twait / SIZE);
    }
 
    public static void main(String[] args)
    {
        proinfo[] arr = new proinfo[SIZE];
 
        arr[0] = new proinfo("p1", 0, 5);
        arr[1] = new proinfo("p2", 1, 2);
        arr[2] = new proinfo("p3", 3, 1);
        arr[3] = new proinfo("p4", 4, 3);
 
        clairvoyantSjf(arr);
 
        // To display the schedule
        display(arr);
 
        // to calculate the Average waiting time
        avgWait(arr);
    }
}
 
// This code is contributed by Karandeep Singh


C#




// Importing necessary libraries
using System;
 
public class Program {
    // Structure to store the information about the process
    struct Proinfo
    {
        public string pname; // Process name
        public int atime; // Arrival time
        public int btime; // Burst time
    }
    // This function schedules the process
    // according to the Clairvoyant SJF scheduling
    // algorithm.
    static void ClairvoyantSjf(Proinfo[] arr)
    {
        // To sort the processes according to the burst time
        int index = 0;
        for (int i = 0; i < arr.Length - 1; i++) {
            index = i;
            for (int j = i + 1; j < arr.Length; j++) {
                if (arr[j].btime < arr[index].btime) {
                    index = j;
                }
            }
            Proinfo temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
    }
 
    static void Display(Proinfo[] arr)
    {
        Console.WriteLine();
        Console.WriteLine(
            "Process id\tArrival time\tBurst time");
        for (int i = 0; i < arr.Length; i++) {
            Console.Write(arr[i].pname + "\t\t");
            Console.Write(arr[i].atime + "\t\t");
            Console.Write(arr[i].btime + "\t\t");
            Console.WriteLine();
        }
    }
 
    // To calculate the average waiting time
    static void AvgWait(Proinfo[] arr)
    {
        int ctime = 0;
        int twait = 0;
        for (int i = 0; i < arr.Length; i++) {
            twait += Math.Abs(arr[i].atime - ctime);
            ctime += arr[i].btime;
        }
        Console.WriteLine("The average waiting time is: "
                          + (float)twait / arr.Length);
    }
 
    public static void Main()
    {
        // Array of process info structures.
        Proinfo[] arr = new Proinfo[4];
        arr[0] = new Proinfo{ pname = "p1", atime = 0,
                              btime = 5 };
        arr[1] = new Proinfo{ pname = "p2", atime = 1,
                              btime = 2 };
        arr[2] = new Proinfo{ pname = "p3", atime = 3,
                              btime = 1 };
        arr[3] = new Proinfo{ pname = "p4", atime = 4,
                              btime = 3 };
 
        Console.WriteLine(
            "Process scheduling according to Clairvoyant SJF is:");
        ClairvoyantSjf(arr);
        // To display the schedule
        Display(arr);
        // to calculate the Average waiting time
        AvgWait(arr);
    }
}
//This Code is Contributed by chinmaya121221


Python3




# Python implementation of the approach
SIZE = 4
 
# Structure to store the information about the process
 
 
class Proinfo:
    def __init__(self, pname, atime, btime):
        self.pname = pname  # Process name
        self.atime = atime  # Arrival time
        self.btime = btime  # Burst time
 
# This function schedules the process
# according to the Clairvoyant SJF scheduling algorithm.
 
 
def clairvoyantSjf(arr):
    # To sort the processes according to the burst time
    for i in range(SIZE - 1):
        index = i
        for j in range(i + 1, SIZE):
            if arr[j].btime < arr[index].btime:
                index = j
        arr[i], arr[index] = arr[index], arr[i]
 
 
def display(arr):
    print("\nProcess id\tArrival time\tBurst time\n")
    for i in range(SIZE):
        print(f"\t{arr[i].pname}\t\t\t{arr[i].atime}\t\t\t\t{arr[i].btime}\t\t")
 
# To calculate the average waiting time
 
 
def avgWait(arr):
    ctime, twait = 0, 0
    for i in range(SIZE):
        twait += abs(arr[i].atime - ctime)
        ctime += arr[i].btime
    print("The average waiting time is:", float(twait) / SIZE)
 
 
if __name__ == "__main__":
    # Array of process info structures.
    arr = [Proinfo("p1", 0, 5), Proinfo("p2", 1, 2),
           Proinfo("p3", 3, 1), Proinfo("p4", 4, 3)]
 
    print("Process scheduling according to Clairvoyant SJF is: \n")
 
    clairvoyantSjf(arr)
    # To display the schedule
    display(arr)
    # to calculate the Average waiting time
    avgWait(arr)


Javascript




// JavaScript implementation of the approach
const SIZE = 4;
 
// Class to store the information about the process
class proinfo {
constructor(pname, atime, btime) {
this.pname = pname;
this.atime = atime;
this.btime = btime;
}
}
 
// This function schedules the process
// according to the Clairvoyant SJF scheduling
// algorithm.
 
function clairvoyantSjf(arr) {
    // To sort the processes according to the burst time
let index = 0;
for (let i = 0; i < SIZE - 1; i++) {
    index = i;
    for (let j = i + 1; j < SIZE; j++) {
        if (arr[j].btime < arr[index].btime) {
            index = j;
        }
    }
    swap(arr, i, index);
}
}
 
function swap(arr, i, index) {
let tmp = arr[i];
arr[i] = arr[index];
arr[index] = tmp;
}
 
function display(arr) {
console.log();
console.log("Process id\t" + "Arrival time\t" + "Burst time\t");
for (let i = 0; i < SIZE; i++) {
console.log(arr[i].pname + "\t\t" + arr[i].atime + "\t\t" + arr[i].btime + "\t\t");
}
}
 
// To calculate the average waiting time
function avgWait(arr) {
let ctime = 0;
let twait = 0;
for (let i = 0; i < SIZE; i++) {
twait += Math.abs(arr[i].atime - ctime);
ctime += arr[i].btime;
}
console.log("The average waiting time is: " + twait / SIZE);
}
 
let arr = [];
 
arr[0] = new proinfo("p1", 0, 5);
arr[1] = new proinfo("p2", 1, 2);
arr[2] = new proinfo("p3", 3, 1);
arr[3] = new proinfo("p4", 4, 3);
 
clairvoyantSjf(arr);
 
// To display the schedule
display(arr);
 
// to calculate the Average waiting time
avgWait(arr);


Output: 

Process scheduling according to Clairvoyant SJF is: 

Process id    Arrival time    Burst time    
p3        3        1        
p2        1        2        
p4        4        3        
p1        0        5        
The average waiting time is: 2.5

 



Last Updated : 21 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads