×

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

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

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

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

3 minutes read.