30/03/2023

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

Write a Program to Shortest Job First Scheduling Program in C++ With Gantt Chart. SJF Non-Preemptive scheduling program in C++ with Gantt chart. Shortest Job First (SJF) is a Non- primitive Scheduling Algorithm we also know SJF as Shortest Job Next (SJN). Shortest Job First (SJF) is also a pre-emptive scheduling algorithm, which means It is compulsory for the CPU to know the next process and how much time the process will take for executing.

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

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


  1. What is the Shortest Job First (SJF) Scheduling Algorithm?.
  2. How to Calculate Turn-Around Time?.
  3. How to Calculate Waiting Time?.
  4. 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.

Non-Preemptive scheduling program in C++ with Gantt chart

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.

Enter The Total Number of Processes: 4

Enter CBT of Process:
6
8
7
3

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

P4 | P1 | P3 | P2 |
--------------------------------------------------------
00 03 09 16 24

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

The Output of Shortest Job First Scheduling Non-Preemptive


The Output of Shortest Job First Scheduling Program in C++ With Gantt Chart

Similar to Shortest Job First Non-Preemptive


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: