#include #include #include #include using namespace std; struct Process { int id; int burstTime; int arrivalTime; int waitingTime; int turnaroundTime; int responseTime; int remainingTime; // To track remaining burst time for preemption }; void calculateTimes(vector &processes) { int currentTime = 0; int completed = 0; int n = processes.size(); vector isCompleted(n, false); while (completed < n) { int idx = -1; int minRemainingTime = INT_MAX; // Find the process with the smallest remaining time that has arrived for (int i = 0; i < n; i++) { if (processes[i].arrivalTime <= currentTime && !isCompleted[i] && processes[i].remainingTime < minRemainingTime) { minRemainingTime = processes[i].remainingTime; idx = i; } } if (idx != -1) { processes[idx].remainingTime--; currentTime++; // If the process is completed if (processes[idx].remainingTime == 0) { isCompleted[idx] = true; completed++; processes[idx].turnaroundTime = currentTime - processes[idx].arrivalTime; processes[idx].waitingTime = processes[idx].turnaroundTime - processes[idx].burstTime; processes[idx].responseTime = processes[idx].waitingTime; // Response time same as waiting time for SJF } } else { currentTime++; // If no process is ready, move time forward } } } void displayTimes(const vector &processes) { float totalWaitingTime = 0, totalTurnaroundTime = 0, totalResponseTime = 0; int n = processes.size(); cout << "Process ID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\tResponse Time\n"; for (const auto &process : processes) { cout << "P" << process.id << "\t\t" << process.burstTime << "\t\t" << process.arrivalTime << "\t\t" << process.waitingTime << "\t\t" << process.turnaroundTime << "\t\t" << process.responseTime << endl; totalWaitingTime += process.waitingTime; totalTurnaroundTime += process.turnaroundTime; totalResponseTime += process.responseTime; } cout << "\nAverage Waiting Time: " << totalWaitingTime / n; cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n; cout << "\nAverage Response Time: " << totalResponseTime / n << endl; } int main() { int n; 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 P" << processes[i].id << ": "; cin >> processes[i].burstTime; cout << "Enter arrival time for Process P" << processes[i].id << ": "; cin >> processes[i].arrivalTime; processes[i].remainingTime = processes[i].burstTime; // Initialize remaining time } calculateTimes(processes); displayTimes(processes); return 0; }