In simple word, we can say that Shortest Job First (SJF) is executed (Put on CPU priority) the only process those have a minimum time for execution.

Shortest Job First (SJF) is a pre-emptive Scheduling Algorithm for execution Process in Minimum time order means, a process has a minimum time for execution execute first and then second minimum time taking process. SJF is an alternative to

Turn Around Time = Completion Time – Arrival Time

Total Turn Around Time = Turn Around Time / Total Number of Process

With the help of this formula, we can calculate a Turn Around Time of all processes in Queue.

Waiting Time = (Turn Around Time – Burst Time)

Total Waiting Time = Waiting Time / Total Number of Process.

## Shortest Job First Scheduling Program in C++ With Gantt Chart

```
#include <bits/stdc++.h>
using namespace std;
int main()
{
/*Shortest Job First Scheduling C++ */
int p, i, j, sum = 0, min, index;
float awt = 0, atat = 0;
cout << "\nEnter The Total Number of Process: ";
cin >> p;
int proc[p];
int *cbt = new int[p];
int *wt = new int[p];
int *gc = new int[p];
int *tat = new int[p];
int *tmp = new int[p];
cout << "\nEnter CBT of Process:\n";
for (i = 0; i < p; i++)
{
cin >> cbt[i];
tmp[i] = cbt[i];
}
sort(cbt, cbt + p);
cout << "\n========================================================\n";
cout << "\t\tShortest Job First Gantt. Chart";
cout << "\n========================================================\n";
for (j = 0; j <= p; j++)
{
min = 100;
for (i = 0; i < p; i++)
{
if (min > tmp[i] && tmp[i] != -1)
{
min = tmp[i];
index = i;
}
}
gc[j] = sum;
wt[j] = sum;
sum += tmp[index];
tat[j] = sum;
tmp[index] = -1;
if (j == p)
break;
cout << 'P' << index + 1 << " | ";
proc[j] = index + 1;
}
cout << "\n--------------------------------------------------------\n";
sum = 0;
for (j = 0; j <= p; j++)
{
if (gc[j] < 10)
cout << 0;
cout << gc[j] << " ";
sum += gc[j];
}
cout << endl;
atat = (sum *1.0) / p;
cout << "\n--------------------------------------------------------";
cout << "\nProcess\t\tCBT\tWaiting Time\tTurn Around Time";
cout << "\n--------------------------------------------------------\n";
for (i = 0; i < p; i++)
{
cout << "P[" << proc[i] << "]\t\t" << cbt[i] << "\t" << wt[i] << "\t\t" << tat[i] << endl;
awt = awt + wt[i];
}
awt = (awt *1.0) / p;
cout << "\n\nTotal Waiting Time: " << awt;
cout << "\n\nTotal Turn Around Time: " << atat << endl;
return 0;
}
```

## Shortest Job First Scheduling

- What is the Shortest Job First (SJF) Scheduling Algorithm?.
- How to Calculate Turn-Around Time?.
- How to Calculate Waiting Time?.
- SJF Scheduling Example

### 1. What is the Shortest Job First (SJF) Scheduling Algorithm?.

Shortest Job First (SJF) is a pre-emptive Scheduling Algorithm for execution Process in Minimum time order means, a process has a minimum time for execution execute first and then second minimum time taking process. SJF is an alternative to

**FCFS**(First Come and First Serve) cause it reduces the average waiting time and is also good for Batch systems.

### 2. How to Calculate Turn-Around Time in SJF Scheduling?.

Turn Around Time = Completion Time – Arrival Time

Total Turn Around Time = Turn Around Time / Total Number of Process

With the help of this formula, we can calculate a Turn Around Time of all processes in Queue.

### 3. How to Calculate Waiting Time in SJF Scheduling?.

Waiting Time = (Turn Around Time – Burst Time)

Total Waiting Time = Waiting Time / Total Number of Process.

### 4. SJF Scheduling Example

We are taking 4 processes whose CBT, or Burst Time Is 6, 8, 7, and 3 respectively and the output is given below.

6

8

7

3

P4 | P1 | P3 | P2 |

--------------------------------------------------------

00 03 09 16 24

P[4] 3 0 3

P[1] 6 3 9

P[3] 7 9 16

P[2] 8 16 24

Total Waiting Time: 7

Total Turn Around Time: 13

**Enter The Total Number of Processes: 4****Enter CBT of Process:**6

8

7

3

**========================================================**

Gantt. Chart

========================================================Gantt. Chart

========================================================

P4 | P1 | P3 | P2 |

--------------------------------------------------------

00 03 09 16 24

**--------------------------------------------------------**

Process CBT Waiting Time Turn Around Time

--------------------------------------------------------Process CBT Waiting Time Turn Around Time

--------------------------------------------------------

P[4] 3 0 3

P[1] 6 3 9

P[3] 7 9 16

P[2] 8 16 24

Total Waiting Time: 7

Total Turn Around Time: 13

## 0 Comments: