#include #include #include #include // For setting precision in averages using namespace std; struct Process { int id; int burstTime; int remainingTime; int waitingTime = 0; int turnaroundTime = 0; int responseTime = -1; // -1 indicates response time hasn't been set yet }; void roundRobinScheduling(vector& processes, int quantum) { queue readyQueue; for (auto& process : processes) { process.remainingTime = process.burstTime; readyQueue.push(&process); } int time = 0; while (!readyQueue.empty()) { Process* current = readyQueue.front(); readyQueue.pop(); if (current->responseTime == -1) { current->responseTime = time; // Set response time on first execution } int execTime = min(quantum, current->remainingTime); time += execTime; current->remainingTime -= execTime; // Update waiting times of other processes for (auto& p : processes) { if (&p != current && p.remainingTime > 0) { p.waitingTime += execTime; } } // Check if process has completed execution if (current->remainingTime > 0) { readyQueue.push(current); } else { current->turnaroundTime = time; } } // Display results and calculate averages double totalWaitingTime = 0; double totalTurnaroundTime = 0; double totalResponseTime = 0; cout << "\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\tResponse Time\n"; for (const auto& process : processes) { cout << "P" << process.id << "\t\t" << process.burstTime << "\t\t" << process.waitingTime << "\t\t" << process.turnaroundTime << "\t\t" << process.responseTime << endl; totalWaitingTime += process.waitingTime; totalTurnaroundTime += process.turnaroundTime; totalResponseTime += process.responseTime; } int n = processes.size(); cout << fixed << setprecision(2); // Set precision to 2 decimal places cout << "\nAverage Waiting Time: " << totalWaitingTime / n << endl; cout << "Average Turnaround Time: " << totalTurnaroundTime / n << endl; cout << "Average Response Time: " << totalResponseTime / n << endl; } int main() { int n, quantum; cout << "Enter the number of processes: "; cin >> n; vector processes(n); for (int i = 0; i < n; i++) { processes[i].id = i + 1; cout << "Enter burst time for process " << i + 1 << ": "; cin >> processes[i].burstTime; } cout << "Enter time quantum: "; cin >> quantum; roundRobinScheduling(processes, quantum); return 0; }