# Clairvoyant Shortest Job first (SJF)

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 :**

`#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);` `}` |

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

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the **CS Theory Course** at a student-friendly price and become industry ready.