simulation of CPU Scheduling Algorithm - SJF in C
Code goes like that
# include < stdio.h>
# include < conio.h>
struct process
{
int pid ;
int burst ;
int wait ;
};
void main()
{
struct process p[5] , temp ;
int i,j,wait=0 ;
float twait;
clrscr();
for(i=0;i<5;i++)
{
p[i].pid = i+1 ;
printf("\nprocess p%d\n",i+1) ;
printf("\nenter burst time\n");
scanf("%d",&p[i].burst);
}
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
{
if(p[j].burst>p[i].burst)
{
temp=p[i];
p[i]=p[j] ;
p[j]=temp;
}
}
}
printf("\nprocess\t\tburst time\t\twaiting time\n");
for(i=0;i<5;i++)
{
p[i].wait=wait ;
printf("\np%d\t\t%d\t\t\t%d\n",p[i].pid,p[i].burst,p[i].wait);
wait = wait + p[i].burst ;
}
twait=wait;
printf("\n\n average waiting time= %f",(twait/5));
getch();
}
No comments:
Post a Comment