Aim:To
implement Priority based cpu scheduling algorithm.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int
bt[10],wt[10],tt[10],pt[10],i,j,n,stt,swt=0,temp1,temp2;
float
awt,att;
clrscr();
printf("enter
no of processes \n");
scanf("%d",&n);
printf("enter
%d process times \n",n);
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
printf("enter
%d process priorites \n",n);
for(i=0;i<n;i++)
scanf("%d",&pt[i]);
printf("\n
CPU time and priorities before sorting");
for(i=0;i<n;i++)
printf("%d
\t %d \n",bt[i],pt[i]);
for(i=0;i<n;i++)
{
for(j=i;j<=n-1;j++)
{
if(pt[i]>pt[j])
{
temp1=bt[i];
bt[i]=bt[j];
bt[j]=temp1;
temp2=pt[i];
pt[i]=pt[j];
pt[j]=temp2;
}
}
}
printf("\n
CPU times and priorities after sorting \n");
for(i=0;i<n;i++)
printf("%d
\t %d \n",bt[i],pt[i]);
tt[0]=bt[0];
wt[0]=0;
stt=0;
for(i=1;i<n;i++)
{
wt[i]=tt[i-1];
tt[i]=wt[i]+bt[i];
stt+=tt[i];
swt+=wt[i];
}
awt=(float)swt/n;
att=(float)stt/n;
printf("\n
CPU time \t priority \t wtime \t ttime \n");
for(i=0;i<n;i++)
printf("%d
\t %d \t %d \t %d \n",bt[i],pt[i],wt[i],tt[i]);
printf("\n
average waiting time is %f",awt);
printf("\n
average turn around time is %f",att);
getch();
}
Output:
No comments:
Post a Comment