Friday 8 May 2015

Round Robin Scheduling Algorithm Implementation Using C

Coding:

#include<stdio.h>

#define FINISHED 0
#define WAITING 1
#define RUNNING 2

struct pro
{
 char ID[6];
 int burst_time;
 int wait_time;
 int turn_time;
 int state;
}process[10];

int no;

 void main()
 {
 int i,j,sum,time=0,time_slice=1,curr_proc,prev_time=0,sturn=0;
 int fin=0;

 printf("\nEnter the no of process:");
 scanf("%d",&no);

 for(i=1;i<=no;i++)
 {
   printf("\nEnter process ID:");
   scanf("%s",process[i].ID);
   printf("\nEnter cpu burst time:");
   scanf("%d",&process[i].burst_time);
   process[i].wait_time=0;
   process[i].turn_time=process[i].burst_time;
   process[i].state=WAITING;
   }
   curr_proc=1;
   process[curr_proc].state=RUNNING;

   while(1)
   {
   process[curr_proc].burst_time--;
   time++;

   for(j=1;j<=no;j++)
   {
     if(process[j].state==WAITING)
     {
       process[j].wait_time++;
       process[j].turn_time++;
       }
   }

   if(time_slice==5||process[curr_proc].burst_time==0)
   {
   if (process[curr_proc].burst_time==0)
   {
   process[curr_proc].state=FINISHED;
   fin++;
   }
   else
   process[curr_proc ].state=WAITING;

   printf("\n%s-%d to %d",process[curr_proc].ID,prev_time,time);



   if(fin==no)
   break;
   curr_proc=curr_proc%no+1;

   while(process[curr_proc].state==FINISHED)
   {
   curr_proc=curr_proc%no+1;
   }

   process[curr_proc].state=RUNNING;
   prev_time=time;
   time_slice=0;
   }
   time_slice++;
   }
   printf("\n\n Process Waitingtime Turnaround time\n\n");
   sum=0;

   for(i=1;i<=no;i++)
   {
   sum+=process[i].wait_time;
   sturn+=process[i].turn_time;
   printf("\n%s\t%d\t\t%d",process[i].ID,process[i].wait_time,process[i].turn_time);
   }
   printf("\n\nAverage waiting time:%.2f\n",(float)sum/(float)no);
   printf("\nAverage turn around time:%.2f\n",(float)sturn/(float)no);
 }













Output:

Enter process ID:P1
Enter cpu burst time:9
Enter process ID:P2
Enter cpu burst time:12
Enter process ID:P3
Enter cpu burst time:10

P1-0 to 5
P2-5 to 10
P3-10 to 15
P1-15 to 19
P2-19 to 24
P3-24 to 29
P2-29 to 31

 Process Waitingtime Turnaround time
P1      10              19
P2      19              31
P3      19              29
Average waiting time:16.00

Average turn around time:26.33

No comments:

Post a Comment