Shortest Job First Scheduling(SJF) program in C
Program:
#include<stdio.h>
struct pr
{
int n; //name
int bt; // brust time
} ;
int main()
{
int q,i,n,temp;
int aw,j;
float awt;
struct pr p[10];
clrscr();
printf("enter number of process :\t");
scanf("%d",&n);
printf("\n enter brust time \n");
for(i=0;i<n;i++)
{
printf("P%d ",i+1); p[i].n=i+1;
scanf("%d",&p[i].bt);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].bt>p[j].bt)
{
temp=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp;
temp=p[i].n;
p[i].n=p[j].n;
p[j].n=temp;
}
}
}
printf("\nprocess :") ;
for(i=0;i<n;i++)
{
printf(" %d",p[i].n);
}
printf("\nBrust time :");
for(i=0;i<n;i++)
{
printf(" %d",p[i].bt);
}
printf("\n Gaint chart \n\t");
for(i=0;i<n;i++)
{
printf("\tP%d",p[i].n);
}
printf("\nWaiting time:"); q=0; aw=0;
for(i=0;i<n;i++)
{
printf("\t %d",q);
aw=aw+q;
q=q+p[i].bt;
}
printf("\n\nWaiting time for all process %d",aw);
awt=aw;
printf("\nAverage Waiting time %f",awt/n);
getch();
return(0);
}
Comments