#include #include #include #include #include using namespace std; struct Process { int id; int burstTime; int arrivalTime; int priority; int remainingTime; int completionTime; int waitingTime; int turnaroundTime; int responseTime; bool started; }; bool compareArrival(const Process &a, const Process &b) { return a.arrivalTime < b.arrivalTime; } void calculateTimes(vector &processes) { int n = processes.size(); int currentTime = 0; int completed = 0; int totalWaitingTime = 0; int totalTurnaroundTime = 0; int totalResponseTime = 0; while (completed < n) { // Find the process with the highest priority (smallest priority number) that has arrived int idx = -1; int highestPriority = INT_MAX; for (int i = 0; i < n; i++) { if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0) { if (processes[i].priority < highestPriority) { highestPriority = processes[i].priority; idx = i; } else if (processes[i].priority == highestPriority) { if (processes[i].arrivalTime < processes[idx].arrivalTime) { idx = i; } } } } if (idx != -1) { // 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 the selected process for one unit of time processes[idx].remainingTime--; currentTime++; // If process is completed 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++; } } else { // If no process is available, increment time currentTime++; } } cout << "\nProcess\tArrival\tBurst\tPriority\tCompletion\tTurnaround\tWaiting\tResponse\n"; for (const auto &p : processes) { cout << "P" << p.id << "\t" << p.arrivalTime << "\t" << p.burstTime << "\t" << p.priority << "\t\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() { vector processes = { {1, 10, 0, 2, 10, 0, 0, 0, 0, false}, {2, 5, 2, 0, 5, 0, 0, 0, 0, false}, {3, 8, 3, 1, 8, 0, 0, 0, 0, false}, {4, 4, 5, 3, 4, 0, 0, 0, 0, false} }; // Sort processes by arrival time sort(processes.begin(), processes.end(), compareArrival); calculateTimes(processes); return 0; }