C program of round robin scheduling algorithm

 What is round robin scheduling :-

  • In this technique ready queue is treated as circular queue.
  • In this technique each process is provided a fix time execute which is called time quantum (or time slice). 
  • CPU goes around the ready queue allocating the CPU to each process for a time interval up to 1 time quantum.   
  • It is only preemptive.
  • This algorithm gives minimum average response time for a given set of process.
  • Widely used scheduling method in traditional OS.
  • It is designed especially for time sharing system or multi-tasking system.

Code:-

#include<stdio.h>
struct process
{
int id,AT,BT,WT,TAT;
};

struct process a[10];

// declaration of the ready queue
int queue[100];
int front=-1;
int rear=-1;

// function for insert the element
// into queue
void insert(int n)
{
if(front==-1)
front=0;
rear=rear+1;
queue[rear]=n;
}

// function for delete the
// element from queue
int delete()
{
int n;
n=queue[front];
front=front+1;
return n;
}
int main()
{
int n,TQ,p,TIME=0;
int temp[10],exist[10]={0};
float total_wt=0,total_tat=0,Avg_WT,Avg_TAT;
printf(“Enter the number of the processn”);
scanf(“%d”,&n);
printf(“Enter the arrival time and burst time of the processn”);
printf(“AT BTn”);
for(int i=0;i<n;i++)
{
scanf(“%d%d”,&a[i].AT,&a[i].BT);
a[i].id=i;
temp[i]=a[i].BT;
}
printf(“Enter the time quantumn”);
scanf(“%d”,&TQ);
// logic for round robin scheduling
// insert first process
// into ready queue
insert(0);
exist[0]=1;
// until ready queue is empty
while(front<=rear)
{
p=delete();
if(a[p].BT>=TQ)
{
a[p].BT=a[p].BTTQ;
TIME=TIME+TQ;
}
else
{
TIME=TIME+a[p].BT;
a[p].BT=0;
}
//if process is not exist
// in the ready queue even a single
// time then insert it if it arrive
// at time ‘TIME’
for(int i=0;i<n;i++)
{
if(exist[i]==0 && a[i].AT<=TIME)
{
insert(i);
exist[i]=1;
}
}
// if process is completed
if(a[p].BT==0)
{
a[p].TAT=TIMEa[p].AT;
a[p].WT=a[p].TATtemp[p];
total_tat=total_tat+a[p].TAT;
total_wt=total_wt+a[p].WT;
}
else
{
insert(p);
}
}
Avg_TAT=total_tat/n;
Avg_WT=total_wt/n;
// printing of the answer
printf(“ID WT TATn”);
for(int i=0;i<n;i++)
{
printf(“%d %d %dn”,a[i].id,a[i].WT,a[i].TAT);
}
printf(“Average waiting time of the processes is : %fn”,Avg_WT);
printf(“Average turn around time of the processes is : %fn”,Avg_TAT);
return 0;
}

Output:-

Enter the number of the process
3
Enter the arrival time and burst time of the process
AT BT
0 5
2 7
4 6
Enter the time quantum
3
ID WT TAT
0 3 8
1 9 16
2 7 13
Average waiting time of the processes is : 6.333333
Average turn around time of the processes is : 12.333333

Recommended post:-

Hackerearth Problems:-

Data structure:-

Key points:-



Leave a Reply

Your email address will not be published.