IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM
AIM:
          To implement the priority scheduling algorithm.
ALGORITHM:
- Start the program
 - Obtain the number of processes from the user
 - Obtain the CPU burst time and the priorities for each process.
 - Calculate the waiting time for each process such that the process with lower priority is executed first and the process with highest priority is executed last.
 - Display all the details for each process along with the average waiting time.
 - Stop the execution.
 
PROGRAM:
#include<stdio.h>
int enqueue(int num,int y,int q[]);
int main()
{
int p[5],b[5],w[5],f[5],n,tb,t[5],i,j,tw,tt,temp,wt,pr[10],c[10];
float aw,at;
printf("\n Enter the no. of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time & priority:");
scanf("%d",&b[i]);
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
    for(j=i+1;j<n;j++)
         if(pr[i]>pr[j])
         {
            temp=pr[i];
            pr[i]=pr[j];
            pr[j]=temp;
            temp=b[i];
            b[i]=b[j];
            b[j]=temp;
            temp=p[i];
            p[i]=p[j];
            p[j]=temp;
         }
tw=w[0]=tt=t[0]=tb=0;
for(i=0;i<n;i++)
{
w[i]=tb;
tb=tb+b[i];
t[i]=b[i]+w[i];
tw=tw+w[i];
tt=tt+t[i];
}
aw=(float)tw/n;
at=(float)tt/n;
for(i=0;i<n;i++)
c[i]=w[i];
c[n]=t[n-1];
printf("\n Process time Burst time Waiting time Turnaround time\n");
for(i=0;i<n;i++)
printf("\n\t%d\t\t%d\t\t%d\t\t%d\n",p[i],b[i],w[i],t[i]);
printf("\nGantt chart\n");
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
for(i=0;i<n;i++)
printf("\t p%d",p[i]);
printf("\n");
for(i=0;i<=n;i++)
printf("\t%d",c[i]);
printf("\n The total waiting time=%d",tw);
printf("\n Total turnaround time is=%d",tt);
printf("\n Average waiting time=%f",aw);
printf("\n Average turnaround time=%f",at);
printf("\n");
}
OUTPUT:
Enter the no. of   process:        4
Enter the burst time for P1:    4
Enter the priority for P1:      2
Enter the burst time for P2:    5
Enter the priority for P2:      3
Enter the burst time for P3:    6
Enter the priority for P3:      1
Enter the burst time for P4:    7
Enter the priority for P4:      4
 Process time   Burst time      Waiting time    Turnaround time
        3                     6                         0                       6
        1                    4                          6                       10
        2                    5                         10                      15
        4                     7                         15                      22
            P3      P1      P2      P4
        0       6       10      15      22
 The total waiting time=31
 Total turnaround time is=53
 Average waiting time=7.750000
 Average turnaround time=13.250000
RESULT:
Thus the program using priority scheduling was done and the output was verified.
No comments:
Post a Comment