Priority Scheduling in C

Simulation of CPU Scheduling Algorithm - Priority Scheduling in C

code goes like that

#include< stdio.h>
#include< conio.h>
struct process
{
int pid;
int priority;
int comp;
int burst;
};
void main()
{
struct process p[5], temp;
int i,j, comp=0;
float tcomp;
clrscr();
for(i=0;i<5;i++)
{
p[i].pid = i+1;
printf("\nprocess p %d",i+1);
printf("\nenter burst time");
scanf("\n%d",&p[i].burst);
printf("\nenter priority ");
scanf("\n%d",&p[i].priority);
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(p[j].priority>p[i].priority)
{ temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("\nprocess\t\tburst time\t\tpriority\t\tcompletion time\n");
for(i=0;i<5;i++)
{
{
comp=comp+p[i].burst;
}
printf("p%d\t\t %d\t\t\t %d\t\t\t %d\n",p[i].pid,p[i].burst,p[i].priority,comp);
}
tcomp=comp;
printf("\n\naverage waiting time=%f",(tcomp/5));
getch();
}


output for the above code is:

1 comment: