Friday, 8 May 2015

Priority Scheduling Algorithm Implementation Using C

Codings:

 #include<stdio.h>
#include<conio.h>
#include<math.h>

struct ProcShedu
{
int pid;
int servicetime;
int priority;
}process[100],temp;

main()
{
int Count=0,i=0,Wait=0,WaitTime=0,Turn=0,j=0;
clrscr();
printf("\n Enter No Of Proceses   ");
scanf("%d",&Count);

for(i=0;i<Count;i++)
{
printf("Enter No %d) Process ID   :",i+1);
scanf("%d",&process[i].pid);
printf("Enter Service Time        :");
scanf("%d",&process[i].servicetime);
printf("Enter Priority        :");
scanf("%d",&process[i].priority);
}

for(i=0;i<Count;i++)
{
for(j=0;j<Count;j++)
{
if(process[i].priority<process[j].priority)
{
temp=process[i];
process[i]=process[j];
process[j]=temp;
}
}
}

printf("\n ID     Service     Wait  Priority ");

for(i=0;i<Count;i++)
{
WaitTime+=Wait;
printf("\n %d        %d        %d        %d",process[i].pid,process[i].servicetime,Wait,process[i].priority);
Wait=process[i].servicetime+Wait;
}
printf("\n Average Waiting Time   :%.2f",((float)WaitTime/(float)Count));
getch();
}












Output:

 Enter No Of Proceses   3
Enter No 1) Process ID   :100
Enter Service Time        :24
Enter Priority        :2
Enter No 2) Process ID   :101
Enter Service Time        :3
Enter Priority        :1
Enter No 3) Process ID   :102
Enter Service Time        :3
Enter Priority        :3

 ID     Service     Wait Priority
 101        3          0         1
 100        24        3         2
 102        3          27       3


 Average Waiting Time   :10.00

No comments:

Post a Comment