Friday, 8 May 2015

Shortest Job First Scheduling Algorithm Implementation Using C

Coding:

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

struct ProcShedu
{
int pid;
int servicetime;
}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);
}

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

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

for(i=0;i<Count;i++)
{
WaitTime+=Wait;
printf("\n %d        %d        %d        %d",process[i].pid,process[i].servicetime,Wait,Wait+process[i].servicetime);
Wait=process[i].servicetime+Wait;
Turn+=Wait;
}

printf("\n Average Waiting Time   :%.2f",floor((float)WaitTime/(float)Count));
printf("\n Average Turnaround Time  :%.2f",floor((float)Turn/(float)Count));
getch();
}












Output:

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

 ID     Service     Wait    Total
 101        3        0        3
 102        3        3        6
 100        24        6        30

 Average Waiting Time   :3.00

 Average Turnaround Time  :13.00

No comments:

Post a Comment