 Open in App
Not now

# Clairvoyant Shortest Job first (SJF)

• Last Updated : 10 Feb, 2022

In this article, we discuss about 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 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 :

## CPP

 `#include ` `#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 = { "p1", 0, 5 };` `    ``arr = { "p2", 1, 2 };` `    ``arr = { "p3", 3, 1 };` `    ``arr = { "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` `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`

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```

My Personal Notes arrow_drop_up
Related Articles