Simulate the following CPU scheduling algorithms SJF
#include<stdio.h>
void main(){
int p[10],bt[10],wt[10],tt[10],n,i,tot_wt=0,tot_tt=bt[0],j,t,c;
float avg_wt,avg_tt;
clrscr();
printf("\nEnter no of Processes");
scanf("%d",&n);
printf("Enter process name and of process Alternatively");
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i],&bt[i]);
}
printf("SJF order of the Process is:\t");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[i])
{
t=bt[i];
c=p[i];
bt[i]=bt[j];
p[i]=p[j];
bt[j]=t;
p[j]=c;
}
}
printf("p%d\t",p[i]);
}
tot_tt=bt[0];
wt[0]=0;
tt[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tt[i]=wt[i]+bt[i];
tot_wt+=wt[i];
tot_tt+=tt[i];
}
avg_wt=(float)tot_wt/n;
avg_tt=(float)tot_tt/n;
for(i=0;i<n;i++)
{
printf("\np%d \t %d\t%d\t%d",p[i],bt[i],wt[i],tt[i]);
}
printf("\nAverage waiting time is %f",avg_wt);
printf("\nAverage turnaround timeis %f",avg_tt);
}
x
void main(){
int p[10],bt[10],wt[10],tt[10],n,i,tot_wt=0,tot_tt=bt[0],j,t,c;
float avg_wt,avg_tt;
clrscr();
printf("\nEnter no of Processes");
scanf("%d",&n);
printf("Enter process name and of process Alternatively");
for(i=0;i<n;i++)
{
scanf("%d%d",&p[i],&bt[i]);
}
printf("SJF order of the Process is:\t");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[i])
{
t=bt[i];
c=p[i];
bt[i]=bt[j];
p[i]=p[j];
bt[j]=t;
p[j]=c;
}
}
printf("p%d\t",p[i]);
}
tot_tt=bt[0];
wt[0]=0;
tt[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tt[i]=wt[i]+bt[i];
tot_wt+=wt[i];
tot_tt+=tt[i];
}
avg_wt=(float)tot_wt/n;
avg_tt=(float)tot_tt/n;
for(i=0;i<n;i++)
{
printf("\np%d \t %d\t%d\t%d",p[i],bt[i],wt[i],tt[i]);
}
printf("\nAverage waiting time is %f",avg_wt);
printf("\nAverage turnaround timeis %f",avg_tt);
}
x
Comments
Post a Comment