Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Round Robin Scheduling Program in C

Last Updated on June 8, 2023 by Mayank Dham

What is round-robin scheduling, how is it done, what are its features, what is the round-robin scheduling algorithm with an example, and what is the round-robin scheduling program in C?

## What is round-robin scheduling?

One of the CPU scheduling strategies is round-robin. The round-robin scheduling algorithm equally distributes each resource and processes each division in a circular sequence without giving any consideration to priority. The terms "time quantum" and "time slice" are used to distribute all resources equally. If a process is finished, it is removed from the ready queue in this round-robin method; otherwise, it will return to the ready queue for the remaining execution. The running queue’s processes that have arrived and are awaiting execution are in the ready queue. The processes that originated from the ready queue are carried out using the running queue. Let’s look at the round-robin scheduling method right now.

## 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[10], bt[10], temp[10];
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[1]
Enter Arrival time:    0
Enter Burst time:  5
Enter the Arrival and Burst time of the Process[2]
Enter Arrival time:    1
Enter Burst time:  4
Enter the Arrival and Burst time of the Process[3]
Enter Arrival time:    2
Enter Burst time:  2
Enter the Arrival and Burst time of the Process[4]
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[3] 2 4 2
Process No[4] 1 3 2
Process No[2] 4 10 6
Process No[1] 5 12 7

Conclusion
In conclusion, the Round Robin scheduling algorithm is a widely used algorithm for process scheduling in operating systems. It is a preemptive algorithm that allocates a fixed time quantum to each process in a circular manner. Round Robin ensures fairness by providing each process with an equal amount of CPU time. This algorithm is easy to implement and guarantees that no process starves CPU time. However, it may not be the most efficient choice for certain scenarios with long-running processes or high context-switching overhead.

Q1: What is the Round Robin scheduling algorithm?
The Round Robin scheduling algorithm is a preemptive CPU scheduling algorithm used in operating systems. It assigns a fixed time quantum to each process, allowing them to run for that amount of time. Once a time quantum expires, the process is preempted, and the CPU is given to the next process in line.

Q2: How does Round Robin scheduling work?
Round Robin scheduling works by maintaining a circular queue of processes. Each process is given a fixed time quantum to execute. When a process’s time quantum expires, it is moved to the back of the queue, and the next process in line is executed. This process continues until all processes are completed.

Q3: What is the advantage of Round Robin scheduling?
The main advantage of Round Robin scheduling is fairness. Each process is given an equal share of CPU time, preventing any single process from monopolizing the CPU. This ensures that all processes get a chance to execute and avoid starvation.

Q4: Are there any limitations of the Round Robin scheduling algorithm?
Yes, there are a few limitations of the Round Robin scheduling algorithm. It may not be the most efficient choice when dealing with long-running processes since frequent context switches can introduce overhead. Additionally, if the time quantum is set too short, the performance may be impacted due to frequent process switches.

Q5: Is Round Robin scheduling suitable for real-time systems?
Round Robin scheduling is not the ideal choice for real-time systems that require strict timing constraints. This is because Round Robin does not prioritize processes based on their deadlines or priorities. Real-time systems typically employ specialized scheduling algorithms that guarantee the meeting of deadlines and prioritization of critical tasks.