Clairvoyant Shortest Job first (SJF)
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;
typedef struct proinfo {
string pname;
int atime;
int btime;
} proinfo;
void clairvoyantSjf(proinfo* arr)
{
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;
}
}
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()
{
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);
display(arr);
avgWait(arr);
}
|
Java
public class GFG {
static int SIZE = 4 ;
static class proinfo {
String pname;
int atime;
int btime;
proinfo(String pname, int atime, int btime)
{
this .pname = pname;
this .atime = atime;
this .btime = btime;
}
}
static void clairvoyantSjf(proinfo[] arr)
{
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" );
}
}
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);
display(arr);
avgWait(arr);
}
}
|
C#
using System;
public class Program {
struct Proinfo
{
public string pname;
public int atime;
public int btime;
}
static void ClairvoyantSjf(Proinfo[] arr)
{
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();
}
}
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()
{
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);
Display(arr);
AvgWait(arr);
}
}
|
Python3
SIZE = 4
class Proinfo:
def __init__( self , pname, atime, btime):
self .pname = pname
self .atime = atime
self .btime = btime
def clairvoyantSjf(arr):
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" )
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__" :
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)
display(arr)
avgWait(arr)
|
Javascript
const SIZE = 4;
class proinfo {
constructor(pname, atime, btime) {
this .pname = pname;
this .atime = atime;
this .btime = btime;
}
}
function clairvoyantSjf(arr) {
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" );
}
}
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);
display(arr);
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
Share your thoughts in the comments
Please Login to comment...