#include #include #include #include using namespace std; struct Process { int id; int arrivalTime; int burstTime; int remainingTime; int completionTime; int waitingTime; int turnaroundTime; int responseTime; bool started; }; void calculateTimes(vector &processes, int quantum) { int n = processes.size(); int currentTime = 0; int completed = 0; int totalWaitingTime = 0; int totalTurnaroundTime = 0; int totalResponseTime = 0; queue q; // Initialize remaining time and set started flag to false for (auto &p : processes) { p.remainingTime = p.burstTime; p.started = false; } // Start by adding the first process that arrives at time 0 q.push(0); processes[0].started = true; while (completed < n) { int idx = q.front(); q.pop(); // Check if this process starts for the first time if (!processes[idx].started) { processes[idx].responseTime = currentTime - processes[idx].arrivalTime; processes[idx].started = true; totalResponseTime += processes[idx].responseTime; } // Process executes for a time quantum or remaining time, whichever is smaller int execTime = min(quantum, processes[idx].remainingTime); processes[idx].remainingTime -= execTime; currentTime += execTime; // If process completes execution if (processes[idx].remainingTime == 0) { processes[idx].completionTime = currentTime; processes[idx].turnaroundTime = processes[idx].completionTime - processes[idx].arrivalTime; processes[idx].waitingTime = processes[idx].turnaroundTime - processes[idx].burstTime; totalWaitingTime += processes[idx].waitingTime; totalTurnaroundTime += processes[idx].turnaroundTime; completed++; } // Add all newly arrived processes to the queue for (int i = 0; i < n; i++) { if (!processes[i].started && processes[i].arrivalTime <= currentTime) { q.push(i); processes[i].started = true; } } // Re-add the current process to the queue if it still has remaining time if (processes[idx].remainingTime > 0) { q.push(idx); } } // Display results cout << "\nProcess\tArrival\tBurst\tCompletion\tTurnaround\tWaiting\tResponse\n"; for (const auto &p : processes) { cout << "P" << p.id << "\t" << p.arrivalTime << "\t" << p.burstTime << "\t" << p.completionTime << "\t\t" << p.turnaroundTime << "\t\t" << p.waitingTime << "\t" << p.responseTime << "\n"; } cout << fixed << setprecision(2); cout << "\nAverage Waiting Time: " << (float)totalWaitingTime / n << "\n"; cout << "Average Turnaround Time: " << (float)totalTurnaroundTime / n << "\n"; cout << "Average Response Time: " << (float)totalResponseTime / n << "\n"; } 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 arrival time for process P" << processes[i].id << ": "; cin >> processes[i].arrivalTime; cout << "Enter burst time for process P" << processes[i].id << ": "; cin >> processes[i].burstTime; } cout << "Enter the time quantum: "; cin >> quantum; calculateTimes(processes, quantum); return 0; }