C Program to show an example of Priority Scheduling
#include<stdio.h>
void main()
{
int n,i,j,k,pos[30],pr[30],temp1,temp2,temp3,s=0,r=0,x=0,a[30],e[30],t[30],w[30];
float m,p=0;
printf("Enter the number of process:");
scanf("%d",&n);
printf("Enter the execution time and the priority for each\n");
for(i=0;i<n;i++)
{
printf("Burst time:");
scanf("%d",&a[i]);
printf("Priority:");
scanf("%d",&pr[i]);
pos[i]=i;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(pr[i]<pr[j])
{
temp3=a[i];
a[i]=a[j];
a[j]=temp3;
temp2=pos[i];
pos[i]=pos[j];
pos[j]=temp2;
temp1=pr[i];
pr[i]=pr[j];
pr[j]=temp1;
}
for(i=0;i<n;i++)
e[i]=a[i];
printf("After the priority scheduling:");
w[0]=0;
t[0]=a[0];
for(i=1;i<n;i++)
{
w[i]=t[i-1];
t[i]=t[i-1]+a[i];
}
printf("Process\t\tpriority\tBurst_Time\tWaiting_Time\tTurnAroundTime\n");
int wt=0,tt=0;
for(i=0;i<n;i++)
{
printf("\t\t\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",pos[i]+1,pr[i],e[i],w[i],t[i]);
tt=tt+t[i];
wt=wt+w[i];
}
printf("Average waiting time:%d",wt/n);
printf("Average turn around time:%d",tt/n);
}
Comments
Post a Comment