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

Characteristics:-

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

Code:-

 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.

logic:-. 

  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 .

#include<stdio.h>
struct process
{
int WT,AT,BT,TAT;
};

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


Output:-

Enter the number of the process
3
Enter the arrival time and burst time of the process
AT WT
0 7
2 3
7 2
Id WT TAT
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;-

Hackerrank:-


C-tutorial:-

See more:-