Thursday 29 September 2011


                      IMPLEMENTATION OF ROUND ROBIN ALGORITHM

AIM:
          To implement the round robin algorithm.

 ALGORITHM:

  1. Start the program.
  2. Read the number of process and the time slice.
  3. Read the burst time for each process.
  4. Calculate the total turn around time, average waiting time based on the time slice.
  5. The Gantt chart is prepared based on the times the process gets executed.
  6. Display the result.
  7. Stop the execution.

PROGRAM:

#include<stdio.h>
int enqueue(int num,int y,int q[]);
int main()
{
int queue[20],i,pro[20];
int rear=0,num,tq,bur[20],wt[20],turn[20];
int flag,j=1,temp,burst[20],wait[20];
float avgwt=0,avgturn=0;
wt[0]=0;
printf("\n ROUND ROBIN SCHEDULING\n");
printf("\n\nEnter the number of process:");
scanf("%d",&num);
printf("\nProcess\tBurst time\n");
for(i=0;i<num;i++)
{
printf("\n p%d\t\t",i+1);
scanf("%d",&bur[i]);
burst[i]=bur[i];
}
printf("\n\nEnter the time quantum:");
scanf("%d",&tq);
do
{
flag=0;
for(i=0;i<num;i++)
{
if(bur[i]>=tq)
{
rear=enqueue(i+1,rear,queue);
bur[i]-=tq;
wt[j]=tq+wt[j-1];
j++;
}
else if((bur[i]>0)&&(bur[i]<tq))
{rear=enqueue(i+1,rear,queue);
wt[j]=bur[i]+wt[j-1];
bur[i]=0;
j++;
}
}
for(i=0;i<num;i++)
if(bur[i]==0)
flag++;
}while(flag!=num);
for(i=1;i<=rear;i++)
queue[i-1]=queue[i];
printf("\n\nGANTT CHART\n");

for(i=0;i<rear;i++)
printf(" p%d\t",queue[i]);
printf("\n");
for(i=0;i<=rear;i++)
printf("%d\t",wt[i]);
for(i=0;i<num;i++)
{
wait[i]=0;
temp=0;
for(j=0;j<rear;j++)
{
if(queue[j]==i+1)
{
wait[i]+=(wt[j]-temp);
temp=wt[j+1];
}
}
}
printf("\n\n**********************************************************");
printf("\nProcess\tBurst time \tWait time \tTurnaround time\n");
printf("**********************************************************\n");
for(i=0;i<num;i++)
{
turn[i]=wait[i]+burst[i];
printf("\np%d\t\t%d\t\t%d\t\t%d",i+1,burst[i],wait[i],turn[i]);
avgwt+=wait[i];
avgturn+=turn[i];
}
printf("\n\nAverage Waiting time is %f",avgwt/num);
printf("\n\nAverage Turnaround time is %f",avgturn/num);
}
}
int enqueue(int n,int rear,int queue[])
{
rear++;
queue[rear]=n;
return rear;
}

OUTPUT:

[root@localhost ~]# ./rr

 ROUND ROBIN SCHEDULING


Enter the number of process: 4

Process Burst time

 P1             3

 P2             6

 P3             4

 P4             2


Enter the time quantum: 2


GANTT CHART
        P1     P2      P3      P4      P1      P2      P3      P2
  0        2        4        6        8        9        11       13      15

**********************************************************
      Process                   Burst time      Wait time       Turnaround time
**********************************************************

           P1                         3                           6                      9
           P2                         6                           9                     15
           P3                         4                           9                     13
           P4                         2                           6                       8

Average waiting time is 7.500000

Average Turnaround time is 11.250000

0 comments:

Post a Comment