#include #include // For INT_MAX using namespace std; struct Process { int pid; // Process ID int bt; // Burst Time int art; // Arrival Time }; // Function to find the waiting time, response time, and completion time void findWaitingTimeAndResponseTime(Process proc[], int n, int wt[], int rt[]) { int remainingTime[n]; // Remaining burst time for each process int firstRunTime[n]; // To track the first time a process runs bool isFirstRun[n]; // To check if a process is running for the first time // Initialize remaining times and first run time for each process for (int i = 0; i < n; i++) { remainingTime[i] = proc[i].bt; isFirstRun[i] = true; // Mark as true because no process has run yet } int complete = 0, t = 0, minm = INT_MAX; int shortest = 0, finish_time; bool check = false; // Process until all processes are completed while (complete != n) { // Find the process with the minimum remaining time that has arrived for (int j = 0; j < n; j++) { if ((proc[j].art <= t) && (remainingTime[j] < minm) && remainingTime[j] > 0) { minm = remainingTime[j]; shortest = j; check = true; } } if (check == false) { t++; // Increment time if no process is found continue; } // If this is the first time the process is being scheduled, record the response time if (isFirstRun[shortest]) { rt[shortest] = t - proc[shortest].art; // Response time = First CPU time - Arrival time isFirstRun[shortest] = false; // Mark that this process has now started running } // Reduce remaining time of the shortest process by one remainingTime[shortest]--; // Update minimum remaining time minm = remainingTime[shortest]; if (minm == 0) minm = INT_MAX; // If the process finishes execution if (remainingTime[shortest] == 0) { complete++; check = false; // Find the finish time of the current process finish_time = t + 1; // Calculate waiting time wt[shortest] = finish_time - proc[shortest].bt - proc[shortest].art; if (wt[shortest] < 0) wt[shortest] = 0; } // Increment time t++; } } // Function to calculate turn around time void findTurnAroundTime(Process proc[], int n, int wt[], int tat[]) { // Calculating turnaround time by adding bt[i] + wt[i] for (int i = 0; i < n; i++) tat[i] = proc[i].bt + wt[i]; } // Function to calculate average time and display results void findavgTime(Process proc[], int n) { int wt[n], tat[n], rt[n]; // Arrays for waiting time, turnaround time, and response time int total_wt = 0, total_tat = 0, total_rt = 0; // Find waiting time and response time for all processes findWaitingTimeAndResponseTime(proc, n, wt, rt); // Find turn around time for all processes findTurnAroundTime(proc, n, wt, tat); // Display processes along with their details cout << "P\t\tBT\t\tAT\t\tWT\t\tTAT\t\tRT\n"; // Calculate total waiting time, turnaround time, and response time for (int i = 0; i < n; i++) { total_wt += wt[i]; total_tat += tat[i]; total_rt += rt[i]; cout << proc[i].pid << "\t\t" << proc[i].bt << "\t\t" << proc[i].art << "\t\t" << wt[i] << "\t\t" << tat[i] << "\t\t" << rt[i] << endl; } cout << "\nAverage Waiting Time: " << (float)total_wt / (float)n; cout << "\nAverage Turn Around Time: " << (float)total_tat / (float)n; cout << "\nAverage Response Time: " << (float)total_rt / (float)n << endl; } // Driver code int main() { int n; cout << "Enter the number of processes: "; cin >> n; Process proc[n]; // Input burst time and arrival time for each process for (int i = 0; i < n; i++) { proc[i].pid = i + 1; // Assign process ID cout << "Enter burst time for Process P" << proc[i].pid << ": "; cin >> proc[i].bt; cout << "Enter arrival time for Process P" << proc[i].pid << ": "; cin >> proc[i].art; } findavgTime(proc, n); // Calculate and display the results return 0; }