Header Ads Widget

C program on the SJF(Shortest job first) preemptive algorithm in os

 Here we are going discuss all things about the SJF scheduling algorithm i.e what is SJF , what are the advantage of SJF , what is disadvantage of SJF ,Code of SJF.  

What is SJF(Shortest job First) scheduling:-

   As it is clear by the name of this scheduling algorithm the job which have the less burst time will get the CPU first .it is the best method to minimize the waiting time .it is of two type 

1. preemptive
2. non preemptive


  • Sjf scheduling can be either preemptive or non-preemptive.
  • IN SJF CPU is assigned to the process that has the smallest next CPU Burst time.
  • If the next CPU  Burst of two process is the same then FCFS scheduling is used to break the tie.
  • This process give the minimum average waiting time for a given processes.


 this code is for Preemptive Shortest job first algorithm
In this code first we are creating the structure for the process in which we are declaring the  waiting time , Arrival time , Burst time ,Priority and turn around time .then after an array of the structure type.


  1. First we copy the burst time of the process in a new array temp[] because in the further calculation we will be going to decrease the Burst time of the process but we will have to need the real burst time of the process in the calculation of the waiting time .(If you confused then don't worry you will be able understand after going through code)
  2. we initialize the burst time of a process with the maximum (you can take any maximum value). and we will use 9th process because we assumed that there will not be more than 10 process but you can use any number.
  3. In this code we are going to use a loop which executed until all the processes are completed. for checking how many processes are completed we use count .Initially it's value is 0 (i.e no processes are completed yet).
  4. In each  cycle we will find the process which have shortest burst time and arrive at time t and burst time of the process is not equal to zero.
  5. After doing this we will decrease the burst time of the process by 1 in each cycle of the time.
  6. And if the process will be complete (Burst time =0) then we will increase the value of the count by 1 (i.e one process is completed)
  7. For calculating the waiting time we will use a formula (WT= time- arrival-Burst time)        let's understand by  an example :- Lets say we have any work in the bank for 5 hours . and we go at the 2 pm and we will come at 9 pm from the bank then waiting time in the bank is:-
                       = (time spend in the bank ) - (Total work time)   

                     =  (9-2) - 5   = 2                                                                                                                                       So we have to wait for the 2 hours .

     8.For calculating turn around time we simply use TAT= completion time - arrival           time.

You can easily understand by Following code .

struct process

struct process a[10];

int main()
int n,temp[10];
int count=0,t=0,short_P;
float total_WT=0, total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
printf("Enter the arrival time and burst time of the process\n");
printf("AT WT\n");
for(int i=0;i<n;i++)
// copying the burst time in
// a temp array for the further use
// in calculation of WT
// we initialize the burst time
// of a process with the maximum
// loop will be execute until all the process
// complete so we use count!= number of
// the process
// for finding min burst
// it is useful
for(int i=0;i<n;i++)
if(a[i].BT<a[short_P].BT && (a[i].AT<=t && a[i].BT>0))
// if any process is completed
// one process complete
// total calculation
// printing of the answer
printf("Id WT TAT\n");
for(int i=0;i<n;i++)
printf("Avg waiting time of the process is %f\n",Avg_WT);
printf("Avg turn around time of the process %f\n",Avg_TAT);


Enter the number of the process
Enter the arrival time and burst time of the process
0 7
2 3
7 2
1   5   12
2   0   3
3   0   2
Avg waiting time of the process is 1.666667
Avg turn around time of the process 5.666667

Recommended post:-

codechef problems:-

Wipro :-

Infytq :-

Key Points;-



See more:-

Post a Comment