# Priority Scheduling Algorithm C Program

//priority scheduling
#include<stdio.h>
#include<conio.h>
#include<dos.h>

void main()
{
int n,b[10],w[10],i,j,h,t,tt;
int stime[10],a[10],p[10];
float avg=0;
clrscr();
printf("\n\tPRIORITY SCHEDULING ALGORITHM");
printf("\n\t*****************************************************\n");
printf("\nEnter howmany jobs:");
scanf("%d",&n);
printf("\nEnter burst time & priority for corresponding job....\n");
for(i=1;i<=n;i++)
{
printf("\nProcess %d:",i);
scanf("%d %d",&b[i],&p[i]);  a[i]=i;
}
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
if(p[i]<p[j])
{
t=b[i]; tt=a[i];
b[i]=b[j]; a[i]=a[j];
b[j]=t;     a[j]=tt;
}

w[1]=0;

printf("\nprocess %d waiting time is 0",a[1]);
for(i=2;i<=n;i++)
{
w[i]=b[i-1]+w[i-1];
printf("\nProcess %d waiting time: %d",a[i],w[i]);
avg+=w[i];
}
printf("\ntotal waiting time:%f",avg);
printf("\n\nthe average waiting time is:%f\n",avg/n);
printf("\nGaunt Chart\n***************************************\n\n\t");
h=22;

for(i=1;i<=n;i++)
{
printf("%d",b[i]);
for(j=1;j<=b[i];j++)
printf("%c",h);
}
printf("\n\t");
for(i=1;i<=n;i++)
{
printf("%d",w[i]);
for(j=1;j<=w[i];j++)
printf("%c",h);
delay(1000);
}

getch();
}

OUTPUT:
Enter howmany jobs:3
Enter burst time & priority for corresponding job....
Process 1:5 2
Process 2:7 1
Process 3:6 3
PRIORITY SCHEDULING ALGORITHM
*****************************************************
Enter howmany jobs:3
Enter burst time & priority for corresponding job....
Process 1:5 2
Process 2:7 1
Process 3:6 3
process 3 waiting time is 0
Process 1 waiting time: 6
Process 2 waiting time: 11
total waiting time:17.000000
the average waiting time is:5.666667