Efficient Task Scheduling with Round Robin Algorithm in C#

Introduction

The Round Robin algorithm is a widely used scheduling algorithm in computer science, especially in operating systems. It is designed to handle time-sharing systems where each process is assigned a fixed time slice or quantum, ensuring that all processes get an equal share of the CPU time. This algorithm is particularly effective in scenarios where there are multiple processes that need to be executed without starvation. In this article, we will explore the Round Robin algorithm, understand its working principles, and implement it using C# with a practical use case example.

Round Robin Algorithm

The round-robin algorithm is a preemptive scheduling algorithm that cycles through all processes in the ready queue in a circular order. Each process is given a fixed time slice during which it can execute. If a process is not complete within its time slice, it is moved to the end of the queue, and the CPU scheduler picks the next process in line. This cycle continues until all processes are completed. The main advantage of the round-robin algorithm is its simplicity and fairness, as each process gets an equal opportunity to execute.

Key Features of Round Robin Algorithm

  1. Time Quantum: A fixed time slice assigned to each process.
  2. Preemption: The algorithm allows processes to be preempted and moved to the end of the queue.
  3. Fairness: Ensures all processes get an equal share of CPU time.
  4. Simple Implementation: Easy to implement and understand.

Round Robin Algorithm in C#

To implement the Round Robin algorithm in C#, we will follow these steps:

  1. Define a class to represent a process.
  2. Implement a method to simulate the Round Robin scheduling.
  3. Test the implementation with a practical use case example.

Step 1. Process Representation

Define a Process class to represent each process in the system. This class will have properties such as Process ID, Burst Time, and Remaining Time.

public class Process
{
    public int ProcessID { get; set; }
    public int BurstTime { get; set; }
    public int RemainingTime { get; set; }

    public Process(int processID, int burstTime)
    {
        ProcessID = processID;
        BurstTime = burstTime;
        RemainingTime = burstTime;
    }
}

Step 2. Round Robin Scheduling

Implement a method to simulate the Round Robin scheduling. This method will take a list of processes and a time quantum as input and simulate the execution of processes based on the Round Robin algorithm.

using System;
using System.Collections.Generic;

public class RoundRobinScheduler
{
    public static void Schedule(List<Process> processes, int timeQuantum)
    {
        int time = 0;
        Queue<Process> readyQueue = new Queue<Process>(processes);

        while (readyQueue.Count > 0)
        {
            Process currentProcess = readyQueue.Dequeue();
            if (currentProcess.RemainingTime <= timeQuantum)
            {
                time += currentProcess.RemainingTime;
                Console.WriteLine($"Process {currentProcess.ProcessID} completed at time {time}");
                currentProcess.RemainingTime = 0;
            }
            else
            {
                time += timeQuantum;
                currentProcess.RemainingTime -= timeQuantum;
                readyQueue.Enqueue(currentProcess);
                Console.WriteLine($"Process {currentProcess.ProcessID} executed for {timeQuantum} units; remaining time: {currentProcess.RemainingTime}");
            }
        }
    }
}

Step 3. Practical Use Case Example

We will create a sample use case to demonstrate the round-robin algorithm. We will define a list of processes with varying burst times and execute them using our round-robin scheduler.

class Program
{
    static void Main(string[] args)
    {
        List<Process> processes = new List<Process>
        {
            new Process(1, 10),
            new Process(2, 4),
            new Process(3, 5),
            new Process(4, 7)
        };

        int timeQuantum = 3;

        Console.WriteLine("Round Robin Scheduling:");
        RoundRobinScheduler.Schedule(processes, timeQuantum);
    }
}

Step 4. Output

Visual Studio Output

Conclusion

The Round Robin algorithm is a simple yet powerful scheduling algorithm that ensures fair allocation of CPU time among processes. By implementing the Round Robin algorithm in C#, we can simulate a time-sharing system where each process gets an equal opportunity to execute. This article provided an overview of the Round Robin algorithm, its key features, and a practical use case example to demonstrate its implementation. Understanding and applying the Round Robin algorithm can help in designing efficient and fair scheduling systems for various applications.


Similar Articles