Program in 'C' for implementing SJF CPU Scheduling

This program explains a scheduling policy known as SJF(Shortest Job First) used by CPU(Central Processing Unit)of computers that selects the waiting process with smallest execution time to execute first from a number of processes to be executed by the CPU at same time.

Introduction :

This program do this job of deciding which process to be executed first in a number of programs using SJF(Shortest Job First) using three steps:-

  • It first determines the number of processes to be executed by the CPU(Central Processing Unit) simultaneously.

  • Then it reads the name, burst time & arrival time of each process.

  • It then evaluates the TAT(Turn Around Time) and WT(Waiting Time)of each process.

  • Then by calculating the average burst time & turn around time it decides which of the process to be executed first among a number of processes.

This methodology was used by older CPU's to perform a number of task's simultaneously. They used to select the task which needs the lesser time to finish and then perform it. After that they used to select the next shortest job & so on. Now a days, this methodology is not used by CPU's. They now use time sharing methodology i.e. sharing time between process like 1 second for each task in 1 cycle.



void sjf(int,int[],int[],char p[10][10]);

void main()
char p[10][10];
int bt[10],i,at[10],n;
printf("Enter the number of processes:");
printf("Enter the name, burst time & arrival time for processes\n");
for(i=0;i {
printf("Enter the name of the process:");
printf("Enter Arrival time for process P%d in ms:",i+1);
printf("Enter Burst time for process P%d in ms:",i+1);

void sjf(int n,int at[],int bt[],char p[10][10])
char temp[10];
int tat[10],wt[10],i,j,temp1;
float totaltat=0,totalwt=0,avgtat,avgwt;

for(i=0;i {
for(j=i+1;j {
for(i=0;i {
for(i=0;i printf("%s\t%dms\t%dms\t%dms\t%dms\n",p[i],at[i],bt[i],tat[i],wt[i]);
printf("\nAvg turn around time=%5.2f ms\nAvg waiting time=%5.2f ms",avgtat,avgwt);


The output of the above program is :

Enter the number of processes:3
Enter the name, burst time & arrival time for processes
Enter the name of the process:p1
Enter Arrival time for process P1 in ms:0
Enter Burst time for process P1 in ms:10
Enter the name of the process:p2
Enter Arrival time for process P2 in ms:0
Enter Burst time for process P2 in ms:5
Enter the name of the process:p3
Enter Arrival time for process P3 in ms:0
Enter Burst time for process P3 in ms:3

Process AT BT TAT WT
p3 0ms 3ms 3ms 0ms
p2 0ms 5ms 8ms 3ms
p1 0ms 10ms 18ms 8ms

Avg turn around time= 9.67 ms
Avg waiting time= 3.67 ms


Related Articles

Most needed C and Data Structures programs for Campus Interviews

This article would provide you with the most important and repeated questions that are focused during an interview in the Campus Placements. If you are a CSE student or someone belonging to the IT sector, and if you are perfect with these programs and the background concepts involved, then, you can get any job involving C language in it.

Write A C Program To Implement Stack Using Array

In this program we have to implement the stack operation by using the arrays. Here the stack operations are push and pop. Push operation is used to insert the elements into a stack and pop operation is used to remove the elements from a stack

Calculations in C programming

Your computer is actually a big calculator. Whether you're listening to music, watching a movie or playing a video game, your computer only do calculations. This article will teach you to make the most of the calculations a computer can do. We will reuse what we have just learned, namely variables. The idea is just to make your calculations with variables: these variables add, multiply, store the result in another variable, etc.. Even if you're not a fan of math, this article will be absolutely essential.

A Basic Program In C And Its Structure

Here, I discuss a basic program in C so as to provide an insight of the basic structure of a program in C. Once the structure is clear, one can begin developing logics and learning syntax. it consist of line by line explanation of the program and its usage.

More articles: C program


Guest Author: Dhokla17 Aug 2017

What if the processes arrive at different time and some processes arrive at same time?

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: