# Round Robin Scheduling Program in C In this article, what is round-robin scheduling, the Algorithm of round-robin scheduling, characteristics of the round robin scheduling, the round-robin scheduling algorithm with an example, round robin scheduling program in c?

## What is round-robin scheduling?

A round-robin is one of the CPU scheduling algorithms. The round-robin algorithm divides all the resources into equal parts and processes all the partitions into a circular order without prioritization. A term that is used to divide all the resources into equal parts is called time quantum or time slice. In this round-robin algorithm, if a process is completed it is removed from the ready queue otherwise, the process will again go to the ready queue for remaining execution. The ready queue is a queue that contains processes that are arrived and waiting for execution in the running queue. The running queue is used to execute the processes which came from the ready queue. Now, let’s see the round-robin scheduling algorithm.

## Round-robin scheduling algorithm:

Below are the steps to perform the round-robin scheduling algorithm.

• Add all the processes in the ready queue by their arrival time. The ready queue will follow the FIFO structure thus, it will return the first entered process first.
• We will now push all the processes in the queue if their arrival time is less than or equal.
• After that, we will pop out the first process from the ready queue and push it into the running queue for execution till the fixed time (time quantum).
• Now, if the process will be executed completely then we will remove it otherwise we will push it into the end of the ready queue again.
• We will now select another process from the ready queue and execute it. If a process finishes its task then we will not push it into the ready queue again as the burst time of the process is completed.
• Likewise, we will run all the steps until all the processes are executed.

### Round-robin scheduling algorithm with an example.

Let’s see how the round-robin scheduling algorithm works with an example. Here, we have taken an example to understand the working of the algorithm. We will also do a dry run to understand it better. In the above example, we have taken 4 processes P1, P2, P3, and P4 with an arrival time of 0,1,2, and 4 respectively. They also have burst times 5, 4, 2, and 1 respectively. Now, we need to create two queues the ready queue and the running queue which is also known as the Gantt chart. Step 1: first, we will push all the processes in the ready queue with an arrival time of 0. In this example, we have only P1 with an arrival time of 0. This is how queues will look after the completion of the first step.

Step 2: Now, we will check in the ready queue and if any process is available in the queue then we will remove the first process from the queue and push it into the running queue. Let’s see how the queue will be after this step. In the above image, we can see that we have pushed process P1 from the ready queue to the running queue. We have also decreased the burst time of process P1 by 2 units as we already executed 2 units of P1.

Step 3: Now we will push all the processes arrival time within 2 whose burst time is not 0. In the above image, we can see that we have two processes with an arrival time within 2 P2 and P3 so, we will push both processes into the ready queue. Now, we can see that process P1 also has remaining burst time so we will also push process P1 into the ready queue again.

Step 4: Now we will see if there are any processes in the ready queue waiting for execution. If there is any process then we will add it to the running queue. In the above image, we can see that we have pushed process P2 from the ready queue to the running queue. We also decreased the burst time of the process P2 as it already executed 2 units.

Step 5: Now we will push all the processes arrival time within 4 whose burst time is not 0. In the above image, we can see that we have one process with an arrival time within 4 P4 so, we will push that process into the ready queue. Now, we can see that process P2 also has remaining burst time so we will also push process P2 into the ready queue again.

Step 6: Now we will see if there are any processes in the ready queue waiting for execution. If there is any process then we will add it to the running queue. In the above image, we can see that we have pushed process P3 from the ready queue to the running queue. We also decreased the burst time of the process P3 as it already executed 2 units. Now, process P3’s burst time becomes 0 so we will not consider it further.

Step 7: Now we will see if there are any processes in the ready queue waiting for execution. If there is any process then we will add it to the running queue. In the above image, we can see that we have pushed process P1 from the ready queue to the running queue. We also decreased the burst time of the process P1 as it already executed 2 units.

Step 8: Now we will push all the processes arrival time within 8 whose burst time is not 0. In the above image, we can see that process P1 also has a remaining burst time so we will also push process P1 into the ready queue again.

Step 9: Now we will see if there are any processes in the ready queue waiting for execution. If there is any process then we will add it to the running queue. In the above image, we can see that we have pushed process P4 from the ready queue to the running queue. We also decreased the burst time of the process P4 as it already executed 1 unit. Now, process P4’s burst time becomes 0 so we will not consider it further.

Step 10: Now we will see if there are any processes in the ready queue waiting for execution. If there is any process then we will add it to the running queue. In the above image, we can see that we have pushed process P2 from the ready queue to the running queue. We also decreased the burst time of the process P2 as it already executed 2 units. Now, process P2’s burst time becomes 0 so we will not consider it further.

Step 11: Now we will see if there are any processes in the ready queue waiting for execution. If there is any process then we will add it to the running queue. In the above image, we can see that we have pushed process P1 from the ready queue to the running queue. We also decreased the burst time of the process P1 as it already executed 1 unit. Now, process P1’s burst time becomes 0 so we will not consider it further. Now our ready queue is empty so we will not perform any task now.

After performing all the operations, our running queue also known as the Gantt chart will look like the below. Let’s calculate the other terms like Completion time, Turn Around Time (TAT), Waiting Time (WT), and Response Time (RT). Below are the equations to calculate the above terms.

Turn Around Time = Completion Time – Arrival Time
Waiting Time = Turn Around Time – Burst Time
Response Time = CPU first time – Arrival Time

Let’s calculate all the details for the above example. ## Characteristics of round robin scheduling:

• All the processes get an equal share of the CPU.
• It is one of the easy scheduling algorithms and it is also easy to implement
• It is one of the widely used algorithms for core CPU scheduling.
• All the processes get fixed quantum time that’s why it is preemptive.

## Round robin scheduling program in c:

We have already seen what is round-robin scheduling algorithm and how it works. We will see how to write round robin scheduling program in c. We will use the above algorithm to write the round robin scheduling program in c.

```// round robin scheduling program in c
#include<stdio.h>

void main()
{
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at, bt, temp;
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;

for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Enter Arrival time: \t");
scanf("%d", &at[i]);
printf(" \nEnter Burst time: \t");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}

printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
}```

Output

``````Total number of processes in the system: 4
Enter the Arrival and Burst time of the Process
Enter Arrival time:    0
Enter Burst time:  5
Enter the Arrival and Burst time of the Process
Enter Arrival time:    1
Enter Burst time:  4
Enter the Arrival and Burst time of the Process
Enter Arrival time:    2
Enter Burst time:  2
Enter the Arrival and Burst time of the Process
Enter Arrival time:    4
Enter Burst time:  1
Enter the Time Quantum for the process:    2``````
Process No Burst Time TAT Waiting Time
Process No 2 4 2
Process No 1 3 2
Process No 4 10 6
Process No 5 12 7