×

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as the scheduling process or the scheduling policy in the algorithm that prescribes the waiting time process into the execution time next.

There are Two types of SJF are there:

  • Non-Preemptive SJF
  • Pre-emptive SJF

These are the algorithms are serialized in the order which has the minimum or least average Time.

The SJF can be considered in three aspects:

  1. Burst Time
  2. Average TAT (Turn Around Time)
  3. Average WT (Waiting Time)

Characteristics of the SJF Scheduling in C

  • The first advantage of the shortest job first is it calculate the minimum average of the waiting time from all of the scheduling processed algorithms.
  • It is one of the Greedy algorithms.
  • It can be ready for the CPU process that can wait with the low priority due to it runs indefinitely.
  • The shortest First job can be used in the specialized scenario where it estimates the accurate running time.

Algorithm for the SJF

  • Firstly, we have to sort all the processes with respective to its arrival time.
  • After that we have to select the processes which have the minimum arrival and burst time.
  • The processes after the completion we have to sort the processes that will wait for completing the previous processes and we have to select the processes which having the least Burst time value.

Let's take an Example for SJF (shortest job First)

    Processes    Burst Time
P1          5
P2          8
P3          2
P4          7

1. The process P1 second because it has the burst time value as 5 which is greater than P3 and less than the P2 and P4.

Then the waiting time of the P1 will be consider as 2 (I.e., P1 = 2).

2.  Secondly, the process P2 will be executed as it consists of the greatest burst time of the value 8. which is greater than the P1, P3, P4.

Then the waiting time of the P2 will be considered as 14(P1+P3+P4).

3. Later on the P2, the Process P3 will be executed and it has the burst time value as 2, which is less than the P1, P2, P3. It will be executed First.

The waiting time for P3 = 0.

4. Then the Process P4 will be executed it has the Burst time value as 7 which is greater than the P1, P3 and less than the P2.

The waiting time for the process P4 will be considered as 7(P1+P3).

Computing the SJF using an example program.

  • The completion Time defined as the time calculated after the process completes the Execution part.
  • The Turn Around Time is the difference between Completion time and the Arrival time.

          TAT = (Completion – Arrival) Time.

  • Waiting Time will be calculated as the difference between the TAT (Turn Around Time) and the Burst Time.

  Waiting Time = (TAT – Burst Time).

Let’s see an example program for Non-preemptive SJF

#include <stdio.h>   // preprocessor
intmain()    // main function
{
intN[20][4]; // we are using matrix to store values
// Time, Average WT & Average TAT
inti, j, x, sum = 0, pos, temp;
float avgwt, avgtat; //  Average WT & Average TAT
printf("Enter the number of the processes: ");
scanf("%d", &x);
printf("Enter the Burst Time value:\n");
for (i = 0; i< x; i++)  // allocating values for processes
{
printf("Processes: ", i + 1);
scanf("%d", &N[i][1]);
N[i][0] = i + 1;
}
for (i = 0; i< x; i++) // soring the Burst Time value
{
pos = i;
for (j = i + 1; j < x; j++)
if (N[j][1] < N[pos][1])
pos = j;
temp = N[i][1];
N[i][1] = N[pos][1];
N[pos][1] = temp;


temp = N[i][0];
N[i][0] = N[pos][0];
N[pos][0] = temp;
}
N[0][2] = 0;
// Calculating the Waiting Times values in SJF
for (i = 1; i< x; i++)
{
N[i][2] = 0;
for (j = 0; j <i; j++)
N[i][2] += N[j][1];
sum += N[i][2];
}
avgwt = (float)sum / x;
sum = 0;
printf("processes	Burst Time	Waiting Time Turnaround Time\n");
for (i = 0; i< x; i++)  // calculating the TAT values and printing
{
N[i][3] = N[i][1] + N[i][2];
sum += N[i][3];
printf("processes	 %d	 %d	%d\n", N[i][0],
N[i][1], N[i][2], N[i][3]);
}
avgtat = (float)sum / x;
printf("\nAverage Waiting Time is calculated = %f", avgwt);
printf("\nAverage Turn Around Time is calculated= %f", avgtat);
}

Output:

SJF Scheduling Program in C

In the following programming we calculated the Average waiting time and the average Turnaround Time with respective to their jobs.

Let's see an example program for pre-emptive SJF scheduling in c:

#include <stdio.h>
intmain()
{
intat[100], bt[100], temp[100];
inti, least, count = 0, time, n;
double wt = 0, tat = 0, end;
float averageWt, averageTat;
printf("\nEnter the Total Number of the Processes:");
scanf("%d", &n);
printf("nEnter the Details of the %d Processes\n", n);
for(i = 0; i< n; i++)
{
printf("\nEnter the Arrival Time of Processes:");
scanf("%d", &at[i]);
printf("Enter the Burst Time of Processes:");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}
bt[99] = 10000;
for(time = 0; count != n; time++)
{
least = 99;
for(i = 0; i< n; i++)
{
if(at[i] <= time &&bt[i] <bt[least] &&bt[i] > 0)
{
least = i;
}
}
bt[least]--;
if(bt[least] == 0)
{
count++;
end = time + 1;
wt = wt + end - at[least] - temp[least];
tat = tat + end - at[least];
}
}
averageWt = wt / n;
averageTat = tat / n;
printf("\n The Average Waiting Time of Processes:%lfn", averageWt);
printf("\n The Average Turn Around Time of Processes:%lfn", averageTat);
return 0;
}

Output:

SJF Scheduling Program in C

Related Topics

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop. What is...

5 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Double Specifier in C

What is a double specifier? The term double denotes the double data type in C. It more accurately depicts floating point numbers. The idea that it has twice the precision of...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.