Add os/round-robin: Round Robin scheduler in C++ and C with Gantt chart, quantum sweep and captured output
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
// ============================================================
|
||||
// Round Robin CPU Scheduling Algorithm in C++
|
||||
// Preemptive: each process gets at most Q time units per round
|
||||
// ============================================================
|
||||
// LEGACY: the earlier version published on ankurm.com, kept for comparison (this header comment is the only addition).
|
||||
// Its waiting times are right only when every process arrives at time 0.
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// Holds all attributes of a single process for Round Robin simulation
|
||||
struct Process {
|
||||
int processNo; // Process identifier (P1, P2, ...)
|
||||
int totalBurstTime; // Original burst time (kept for final output and TT)
|
||||
int remainingBurstTime; // Burst time still left to execute (decremented each tick)
|
||||
int waitingTime; // Accumulated waiting time across all rounds
|
||||
int turnaroundTime; // Total time from start to completion
|
||||
int quantumRoundCount; // How many full time-quanta this process has already used
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
Process processes[99];
|
||||
int processCount;
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Step 1: Read number of processes and their burst times
|
||||
// -------------------------------------------------------
|
||||
cout << "\n Enter No of Processes: ";
|
||||
cin >> processCount;
|
||||
|
||||
for (int i = 0; i < processCount; i++) {
|
||||
cout << "\n Enter Burst Time of Process " << i + 1 << ": ";
|
||||
cin >> processes[i].totalBurstTime;
|
||||
|
||||
processes[i].remainingBurstTime = processes[i].totalBurstTime; // Copy for simulation
|
||||
processes[i].waitingTime = 0;
|
||||
processes[i].turnaroundTime = 0;
|
||||
processes[i].quantumRoundCount = 0;
|
||||
processes[i].processNo = i + 1;
|
||||
}
|
||||
|
||||
int timeQuantum;
|
||||
cout << "\n Enter Time Quantum: ";
|
||||
cin >> timeQuantum;
|
||||
|
||||
// Display entered data
|
||||
cout << "\n\n Entered Data";
|
||||
cout << "\n ProcesstBurst Time";
|
||||
for (int i = 0; i < processCount; i++) {
|
||||
cout << "\n " << processes[i].processNo
|
||||
<< "\t\t" << processes[i].totalBurstTime;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Step 2: Compute total simulation time (sum of all burst times)
|
||||
// -------------------------------------------------------
|
||||
int totalSimTime = 0;
|
||||
for (int i = 0; i < processCount; i++) totalSimTime += processes[i].totalBurstTime;
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Step 3: Round Robin Simulation
|
||||
// currentProc = index of process currently on CPU
|
||||
// quantumTick = how many time units used in current quantum slot
|
||||
// -------------------------------------------------------
|
||||
int currentProc = 0;
|
||||
int quantumTick = 0;
|
||||
|
||||
for (int tick = 0; tick < totalSimTime; tick++) {
|
||||
|
||||
// When a process begins a new round (quantum slot starts),
|
||||
// record the current time as the start of its wait period
|
||||
if (quantumTick == 0 && processes[currentProc].remainingBurstTime != 0) {
|
||||
processes[currentProc].waitingTime = tick;
|
||||
// Subtract all time this process has already spent executing in previous rounds
|
||||
if (processes[currentProc].quantumRoundCount != 0) {
|
||||
processes[currentProc].waitingTime -=
|
||||
timeQuantum * processes[currentProc].quantumRoundCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (processes[currentProc].remainingBurstTime != 0 && quantumTick != timeQuantum) {
|
||||
// Process still has work to do and quantum not yet exhausted: run for 1 tick
|
||||
processes[currentProc].remainingBurstTime--;
|
||||
quantumTick++;
|
||||
} else {
|
||||
// Either quantum exhausted OR process finished — switch to next process
|
||||
if (quantumTick == timeQuantum && processes[currentProc].remainingBurstTime != 0) {
|
||||
// Process used a full quantum but is not finished yet
|
||||
processes[currentProc].quantumRoundCount++;
|
||||
}
|
||||
// Move to next process in circular order
|
||||
currentProc = (currentProc + 1) % processCount;
|
||||
quantumTick = 0;
|
||||
tick--; // Redo this tick with the newly selected process
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Step 4: Compute Turnaround Time and display results
|
||||
// -------------------------------------------------------
|
||||
int totalWaiting = 0, totalTurnaround = 0;
|
||||
|
||||
cout << "\n\n Result of Round Robin Scheduling";
|
||||
cout << "\n PNo\tBT\tWT\tTT";
|
||||
|
||||
for (int i = 0; i < processCount; i++) {
|
||||
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].totalBurstTime;
|
||||
totalWaiting += processes[i].waitingTime;
|
||||
totalTurnaround += processes[i].turnaroundTime;
|
||||
|
||||
cout << "\n " << processes[i].processNo
|
||||
<< "\t" << processes[i].totalBurstTime
|
||||
<< "\t" << processes[i].waitingTime
|
||||
<< "\t" << processes[i].turnaroundTime;
|
||||
}
|
||||
|
||||
cout << "\n Average Waiting Time : " << (float)totalWaiting / processCount;
|
||||
cout << "\n Average Turnaround Time : " << (float)totalTurnaround / processCount;
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user