Round Robin Scheduling

Description

Round Robin scheduling is a pre-emptive scheduling algorithm where each process is assigned a fixed time slot (quantum) in a cyclic manner. If a process does not complete within its time slot, it is preempted and placed back in the ready queue. It ensures fairness in process execution and is commonly used in time-sharing systems.

C++ Code

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct Process {
    int pid;
    int burst;
    int remaining;
};

void roundRobin(vector<Process>& processes, int quantum) {
    queue<Process> q;
    int n = processes.size();
    vector<int> waitingTime(n, 0);

    for (auto& process : processes) {
        q.push(process);
    }

    int currentTime = 0;
    while (!q.empty()) {
        Process currentProcess = q.front();
        q.pop();

        if (currentProcess.remaining > quantum) {
            currentTime += quantum;
            currentProcess.remaining -= quantum;
            q.push(currentProcess);
        } else {
            currentTime += currentProcess.remaining;
            waitingTime[currentProcess.pid] = currentTime - processes[currentProcess.pid].burst;
            currentProcess.remaining = 0;
        }
    }

    cout << "Process\tWaiting Time\n";
    for (int i = 0; i < n; ++i) {
        cout << i << "\t" << waitingTime[i] << "\n";
    }
}

int main() {
    vector<Process> processes = {
        {0, 8, 0},
        {1, 4, 0},
        {2, 9, 0},
        {3, 5, 0}
    };

    int quantum = 3;
    roundRobin(processes, quantum);

    return 0;
}

Time and Space Complexity

Operation Time Complexity Space Complexity
Round Robin Scheduling O(n * max(burst)) O(n)

Where:

  • n: Number of processes.
  • burst: Maximum burst time among all processes.

← Back to Homepage