# SJF Scheduling Algorithm Program Operating System LAB

By | December 18, 2022

SJF Scheduling Algorithm Program – Operating System Practicals LAB.
Write a C Program to input burst times of N processes and find the Waiting times and Turn around times using SJF – Shortest Job First Scheduling Method in Operating system.

Program Code

```#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;           /*contains process number*/
}

/*sorting burst time in ascending order using selection sort*/
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;            /*waiting time for first process will be zero */
total = 0;
/*calculate waiting time for processes */
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
total+=wt[i];
}

avg_wt=(float)total/n;      /*average waiting time*/
total=0;

printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];     /*calculate turnaround time*/
total+=tat[i];
printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n;     /*average turnaround time*/
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
```

Output:

SJF Scheduling Algorithm Program Operating System Practicals LAB

```Enter number of process:3

Enter Burst Time:
p1:24
p2:3
p3:3

Process Burst Time Waiting Time Turnaround Time
p2        3          0            3
p3        3          3            6
p1        24         6            30

Average Waiting Time=3.000000
Average Turnaround Time=13.000000
```