#include #include using namespace std; struct Process { int pid; // Process ID int arrivalTime; // Arrival Time int burstTime; // Burst Time int waitingTime; // Waiting Time int turnaroundTime; // Turnaround Time int responseTime; // Response Time }; void calculateTimes(Process processes[], int n) { int currentTime = 0; float totalWaitingTime = 0, totalTurnaroundTime = 0, totalResponseTime = 0; // Calculating waiting time, turnaround time, and response time for each process for (int i = 0; i < n; i++) { if (currentTime < processes[i].arrivalTime) { currentTime = processes[i].arrivalTime; } processes[i].responseTime = currentTime - processes[i].arrivalTime; processes[i].waitingTime = processes[i].responseTime; processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime; currentTime += processes[i].burstTime; totalWaitingTime += processes[i].waitingTime; totalTurnaroundTime += processes[i].turnaroundTime; totalResponseTime += processes[i].responseTime; } //results cout << "\nProcess ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\tResponse Time\n"; for (int i = 0; i < n; i++) { cout << "P" << processes[i].pid << "\t\t" << processes[i].arrivalTime << "\t\t" << processes[i].burstTime << "\t\t" << processes[i].waitingTime << "\t\t" << processes[i].turnaroundTime << "\t\t" << processes[i].responseTime << "\n"; } cout << "\nAverage Waiting Time: " << totalWaitingTime / n; cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n; cout << "\nAverage Response Time: " << totalResponseTime / n << endl; } bool compareArrivalTime(Process a, Process b) { return a.arrivalTime < b.arrivalTime; } int main() { int n; cout << "Enter the number of processes: "; cin >> n; Process processes[n]; for (int i = 0; i < n; i++) { processes[i].pid = i + 1; cout << "Enter arrival time for process P" << processes[i].pid << ": "; cin >> processes[i].arrivalTime; cout << "Enter burst time for process P" << processes[i].pid << ": "; cin >> processes[i].burstTime; } sort(processes, processes + n, compareArrivalTime); calculateTimes(processes, n); return 0; }