#include using namespace std; void findWaitingTime(int processes[], int n, int bt[], int wt[], int at[], int quantum, int firstExecTime[]) { int rem_bt[n]; for (int i = 0; i < n; i++) rem_bt[i] = bt[i]; int t = 0; int done = 0; while (done < n) { bool progress = false; for (int i = 0; i < n; i++) { if (at[i] <= t && rem_bt[i] > 0) { progress = true; if (firstExecTime[i] == -1) { firstExecTime[i] = t; } if (rem_bt[i] > quantum) { t += quantum; rem_bt[i] -= quantum; } else { t += rem_bt[i]; wt[i] = t - bt[i] - at[i]; rem_bt[i] = 0; done++; } } } if (!progress) break; } } void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) { for (int i = 0; i < n; i++) tat[i] = bt[i] + wt[i]; } void findAvgTime(int processes[], int n, int bt[], int at[], int quantum) { int wt[n], tat[n], total_wt = 0, total_tat = 0, total_resp = 0; int firstExecTime[n]; for (int i = 0; i < n; i++) { firstExecTime[i] = -1; } findWaitingTime(processes, n, bt, wt, at, quantum, firstExecTime); findTurnAroundTime(processes, n, bt, wt, tat); cout << "PN\tAT\tBT\tWT\tTAT\tRT\n"; for (int i = 0; i < n; i++) { total_wt += wt[i]; total_tat += tat[i]; total_resp += (firstExecTime[i] - at[i]); cout << " " << i + 1 << "\t" << at[i] << "\t" << bt[i] << "\t" << wt[i] << "\t" << tat[i] << "\t" << (firstExecTime[i] - at[i]) << endl; } cout << "Average waiting time = " << (float)total_wt / (float)n << endl; cout << "Average turnaround time = " << (float)total_tat / (float)n << endl; cout << "Average response time = " << (float)total_resp / (float)n << endl; } int main() { int n; cout << "Enter the number of processes: "; cin >> n; int processes[n], burst_time[n], arrival_time[n]; for (int i = 0; i < n; i++) { processes[i] = i + 1; cout << "Enter arrival time, burst time for Process " << i + 1 << ": "; cin >> arrival_time[i] >> burst_time[i]; } int quantum; cout << "Enter time quantum: "; cin >> quantum; findAvgTime(processes, n, burst_time, arrival_time, quantum); return 0; }