a#include #include #include using namespace std; struct Process { int id; int cpuTime; int remainingTime; int waitingTime; int turnaroundTime; }; int main() { int n, timeQuantum; cout << "Enter the number of processes: "; cin >> n; vector processes(n); cout << "Enter the CPU times: "; for (int i = 0; i < n; i++) { processes[i].id = i + 1; cin >> processes[i].cpuTime; processes[i].remainingTime = processes[i].cpuTime; processes[i].waitingTime = 0; processes[i].turnaroundTime = 0; } cout << "Enter the time quantum: "; cin >> timeQuantum; queue readyQueue; for (int i = 0; i < n; i++) { readyQueue.push(&processes[i]); } int currentTime = 0; while (!readyQueue.empty()) { Process* currentProcess = readyQueue.front(); readyQueue.pop(); if (currentProcess->remainingTime > timeQuantum) { currentTime += timeQuantum; currentProcess->remainingTime -= timeQuantum; for (auto& process : processes) { if (process.id != currentProcess->id && process.remainingTime > 0) { process.waitingTime += timeQuantum; } } readyQueue.push(currentProcess); } else { currentTime += currentProcess->remainingTime; currentProcess->turnaroundTime = currentTime; currentProcess->waitingTime = currentTime - currentProcess->cpuTime; for (auto& process : processes) { if (process.id != currentProcess->id && process.remainingTime > 0) { process.waitingTime += currentProcess->remainingTime; } } currentProcess->remainingTime = 0; } } double totalWaitingTime = 0, totalTurnaroundTime = 0; cout << "\nProcess\tWaiting Time\tTurnaround Time\n"; for (const auto& process : processes) { cout << "P" << process.id << "\t" << process.waitingTime << "\t\t" << process.turnaroundTime << "\n"; totalWaitingTime += process.waitingTime; totalTurnaroundTime += process.turnaroundTime; } cout << "\nAverage Waiting Time: " << totalWaitingTime / n; cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl; return 0; }