0% found this document useful (0 votes)
7 views1 page

SJF CPU Scheduling Simulation Code

Uploaded by

sagarreddy9743
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

SJF CPU Scheduling Simulation Code

Uploaded by

sagarreddy9743
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Program-2

Simulate the following CPU scheduling algorithms to find turnaround time and
waiting time
[Link](shortest job first)

#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,pos,temp,avwt=0,avtat=0;
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;
}
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;
for(i=1;i<n;i++)
{ wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

}
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{ tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];

printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);


}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time=%d",avwt);
printf("\nAverage Turnaround Time=%d\n",avtat);
return 0;
}

You might also like