# Explaining Round Robin (RR) Scheduling Algorithms and Their Types

Feb 21, 2024

## Introduction to Round Robin Scheduling Algorithms

Round Robin (RR) scheduling is a widely used algorithm in computer operating systems and network systems. It is a preemptive scheduling algorithm that allows multiple processes to share the CPU (Central Processing Unit) fairly. In this article, we will delve into the details of Round Robin scheduling algorithms, their types, and their applications.

## How Round Robin Scheduling Works

In Round Robin scheduling, each process is assigned a fixed time quantum, often referred to as a time slice. The CPU scheduler allocates CPU time to each process in a circular manner. When a process is given the CPU, it is allowed to run for the time quantum, and then it is preempted, allowing the next process in the queue to run.

This preemptive nature of Round Robin scheduling ensures that no process monopolizes the CPU for an extended period. It provides fairness in CPU allocation, as each process gets an equal opportunity to execute. However, Round Robin scheduling may not be the most efficient algorithm in terms of response time or throughput.

## Types of Round Robin Scheduling Algorithms

There are several variations and enhancements to the basic Round Robin scheduling algorithm. Let’s explore some of the commonly used types:

### 1. Basic Round Robin

The basic Round Robin scheduling algorithm assigns an equal time quantum to each process in the ready queue. Once a process’s time quantum expires, it is moved to the back of the queue, and the next process gets a chance to run. This type of Round Robin scheduling ensures fairness but may result in higher waiting times for processes with longer execution times.

### 2. Weighted Round Robin

In Weighted Round Robin scheduling, each process is assigned a different time quantum based on its priority or importance. Processes with higher priority are given a longer time quantum, allowing them to execute more instructions before being preempted. This type of Round Robin scheduling is useful when certain processes require more CPU time due to their criticality or resource requirements.

### 3. Virtual Round Robin

Virtual Round Robin scheduling is an enhancement to the basic Round Robin algorithm. It aims to reduce the waiting time for I/O-bound processes. In this type of scheduling, each process is given a small initial time quantum. If a process completes its execution before the time quantum expires, it is moved to the front of the queue, allowing it to execute again immediately. This approach prioritizes processes that frequently perform I/O operations and reduces their waiting time.

### 4. Priority Round Robin

Priority Round Robin scheduling combines the concepts of Round Robin and priority scheduling. Each process is assigned a priority value, and the CPU scheduler allocates CPU time based on these priorities. Higher priority processes are given a longer time quantum, while lower priority processes receive shorter time quanta. This type of scheduling ensures that critical processes with higher priorities receive more CPU time.

## Applications of Round Robin Scheduling

Round Robin scheduling algorithms find applications in various domains, including:

### 1. Operating Systems

Round Robin scheduling is commonly used in operating systems to allocate CPU time among multiple processes. It ensures fairness and prevents any single process from monopolizing the CPU.

### 2. Network Systems

In network systems, Round Robin scheduling is used to distribute network traffic equally among multiple servers or devices. It ensures that each server or device gets a fair chance to handle incoming requests.

### 3. Real-Time Systems

Round Robin scheduling is also used in real-time systems where tasks need to be executed within specific time constraints. The preemptive nature of Round Robin allows for timely execution of tasks, ensuring that critical deadlines are met.

### 4. Web Servers

Web servers often employ Round Robin scheduling to balance the load among multiple server instances. This helps in handling high traffic and ensures that each server instance gets an equal share of requests.

## Example code in C language demonstrating the Round Robin scheduling algorithm

Problem Statement:

Implement the Round Robin (RR) scheduling algorithm in a simulated environment. The goal is to schedule a set of processes for execution on a single CPU, ensuring fairness and preventing any process from monopolizing CPU time.

Example Code:

Here’s a simple example code in C language demonstrating the Round Robin scheduling algorithm:

``````#include <stdio.h>
#include <stdbool.h>

// Define the maximum number of processes
#define MAX_PROCESSES 5

// Structure to represent a process
struct Process {
int id; // Process ID
int burstTime; // Burst time (time required for execution)
int remainingTime; // Remaining time for execution
};

// Function to execute processes using Round Robin scheduling algorithm
void roundRobin(struct Process processes[], int n, int quantum) {
int remainingProcesses = n;
int currentTime = 0;

// Continue until all processes are executed
while (remainingProcesses > 0) {
// Traverse each process in the list
for (int i = 0; i < n; i++) {
// If process still has remaining execution time
if (processes[i].remainingTime > 0) {
// Execute the process for the time quantum or remaining time, whichever is smaller
int executionTime = (processes[i].remainingTime < quantum) ? processes[i].remainingTime : quantum;
processes[i].remainingTime -= executionTime;
currentTime += executionTime;

// Print execution details
printf(“Process %d executed for %d units of time. Current Time: %d\n”, processes[i].id, executionTime, currentTime);

// If process has completed execution
if (processes[i].remainingTime == 0) {
remainingProcesses–;
}
}
}
}
}

int main() {
// Define an array of processes
struct Process processes[MAX_PROCESSES] = {
{1, 10, 10}, // Process ID, Burst Time, Remaining Time
{2, 5, 5},
{3, 8, 8},
{4, 3, 3},
{5, 12, 12}
};

// Define the time quantum for Round Robin scheduling
int quantum = 4;

// Execute processes using Round Robin scheduling algorithm
roundRobin(processes, MAX_PROCESSES, quantum);

return 0;
}``````

This code simulates the execution of a set of processes using the Round Robin scheduling algorithm. Each process is represented by its ID, burst time (time required for execution), and remaining time for execution. The `roundRobin()` function iterates through the list of processes, executing each process for a fixed time quantum or its remaining time, whichever is smaller. The execution details are printed, and the process is marked as completed when its remaining time becomes zero.

## Conclusion

Round Robin (RR) scheduling algorithms provide a fair and preemptive approach to CPU allocation in various systems. The different types of Round Robin scheduling algorithms offer flexibility and cater to specific requirements, such as priority-based execution or reducing waiting times for I/O-bound processes. Understanding the nuances of Round Robin scheduling algorithms is crucial for system designers, administrators, and developers to optimize resource allocation and ensure efficient system performance.