C Program to Simulate PRIORITY CPU Scheduling Algorithm

void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf(“Enter the number of process:”);
scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“Enter process name,arrivaltime,execution time & priority:”);
//flushall();
scanf(“%s%d%d%d”,pn[i],&at[i],&et[i],&p[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)

{

    if(i==0)
    {
        st[i]=at[i];
        wt[i]=st[i]-at[i];
        ft[i]=st[i]+et[i];
        ta[i]=ft[i]-at[i];
    }
    else
    {
        st[i]=ft[i-1];
        wt[i]=st[i]-at[i];
        ft[i]=st[i]+et[i];
        ta[i]=ft[i]-at[i];
    }
    totwt+=wt[i];
    totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0; i<n; i++)
    printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();

}
OUTPUT:

Enter the number of process:3
Enter process name,arrivaltime,execution time & priority:1 2 1 3
Enter process name,arrivaltime,execution time & priority:3 5 4 1
Enter process name,arrivaltime,execution time & priority:3 4 6 2

Pname arrivaltime executiontime priority waitingtime tatime
3 5 4 1 0 4
3 4 6 2 5 11
1 2 1 3 13 14
Average waiting time is:6.000000
Average turnaroundtime is:9.666667

Leave a Reply

Your email address will not be published. Required fields are marked *