SCHEDULING ALGORITHMS
Shortest Job First
// Program
// 2. SJF scheduling
#include<stdio.h>
#include<stdlib.h>
typedef struct process
{
int pid,ft,wt,bt;
}pr;
int n;
pr p[10];
void sjf();
void disp_gantt();
int main()
{
int i;
printf(“\nHow many processes : “);
scanf(“%d”,&n);
printf(“\tEnter the values for Burst time\n”);
for(i=0;i<n;i++)
{
printf(“\tEnter for Process %d : “,i);
scanf(“%d”,&p[i].bt);
p[i].pid=i;
}
sjf();
}
void sjf()
{
int i,j;
pr temp;
float avgwt, avgtat, sum_wt=0, sum_tat=0;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(p[j].bt>p[j+1].bt)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=0;i<n;i++)
{
if(i==0)
p[i].wt=0;
else
p[i].wt=p[i-1].ft;
p[i].ft=p[i].wt+p[i].bt;
sum_wt=sum_wt+p[i].wt;
sum_tat=sum_tat+p[i].ft;
}
avgwt=sum_wt/n;
avgtat=sum_tat/n;
disp_gantt(n);
printf(“\nAverage Turnaround Time : %5.2f ms”,avgtat);
printf(“\nAverage Waiting Time : %5.2f ms \n\n”, avgwt);
}
void disp_gantt()
{
int i;
printf(“\n\nGANTT CHART\n\n\t”);
for(i=0;i<n;i++)
printf(“P%d\t”,p[i].pid);
printf(“\n 0\t”);
for(i=0;i<n;i++)
printf(” %d\t”,p[i].ft);
printf(“\n”);
}