# program of SJF(Shortest job first ) Scheduling algorithm (Non -preemptive) of Os in c

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 Non preemptive Shortest job first algorithm
In this code first we are creating the structure for the process in which we are declaring the id , waiting time , Arrival time , Burst time and turn around time .then after an array of the structure type.

logic:-

1. After taking the input first we sort the input on the basis of Arrival time (i.e the process which have less Arrival time will come first).

Here one more things we will do . we will check that the arrival time of all the processes are different or not. (Example:-  if all the processes comes at the same time then we don’t need to sort the array on the basis of arrival time ) . for checking this we will use check_ar.

2.  After doing this if processes are arrived at the different time then we can easily calculate the waiting and turn around time for the first process. and for other process first we will find the minimum burst time of the process which are arrived at cmp_time  (completion time of the previous process)  .

You can easily understand by Following code .

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

// function for swapping
void swap(int *b,int *c)
{
int tem;
tem=*c;
*c=*b;
*b=tem;
}

//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf(“Enter the number of process n”);
scanf(“%d”,&n);
printf(“Enter the Arrival time and Burst time of the processn”);
printf(“AT BTn”);
for(int i=0;i<n;i++)
{
scanf(“%d%d”,&a[i].AT,&a[i].BT);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;
if(check_ar!=a[i].AT )
check_ar=1;
}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<ni1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
}
}
}
}
// logic of SJF non preemptive algo
// if all the process are arrived at different time
if(check_ar!=0)
{
a.WT=a.AT;
a.TAT=a.BTa.AT;
Cmp_time=a.TAT;
Total_WT=Total_WT+a.WT;
Total_TAT=Total_TAT+a.TAT;
for(int i=1;i<n;i++)
{
int min=a[i].BT;
for(int j=i+1;j<n;j++)
{
if(min>a[j].BT && a[j].AT<=Cmp_time)
{
min=a[j].BT;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
}
}
a[i].WT=Cmp_timea[i].AT;
Total_WT=Total_WT+a[i].WT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
// Turn Around Time of the process
// compl-Arival
a[i].TAT=Cmp_timea[i].AT;
Total_TAT=Total_TAT+a[i].TAT;
}
}
// if all the process are arrived at same time
else
{
for(int i=0;i<n;i++)
{
int min=a[i].BT;
for(int j=i+1;j<n;j++)
{
if(min>a[j].BT && a[j].AT<=Cmp_time)
{
min=a[j].BT;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
}
}
a[i].WT=Cmp_timea[i].AT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
// Turn Around Time of the process
// compl-Arrival
a[i].TAT=Cmp_timea[i].AT;
Total_WT=Total_WT+a[i].WT;
Total_TAT=Total_TAT+a[i].TAT;
}
}
Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;

// Printing of the results
printf(“The process aren”);
printf(“ID WT TATn”);
for(int i=0;i<n;i++)
{
printf(“%dt%dt%dn”,a[i].id,a[i].WT,a[i].TAT);
}
printf(“Avg waiting time is:- %fn”,Avg_WT);
printf(“Avg turn around time is:- %f”,Avg_TAT);
return 0;
}

Output:-

Enter the number of process
4
Enter the Arrival time and Burst time of the process
AT BT
0 2
2 3
2 4
3 3
The process are
ID WT TAT
1   0   2
2   0   3
4   2   5
3   6   10
Avg waiting time is:- 2.000000
Avg turn around time is:- 5.000000

Recommended post:-