#include #include using namespace std; void preemptivePriorityScheduling(int pid[], int arrival[], int burst[], int priority[], int n) { int remainingBurst[n]; int completionTime[n]; int firstExecutionTime[n]; int currentTime = 0; int completed = 0; bool isCompleted[n] = {false}; bool isFirstExecution[n] = {false}; for (int i = 0; i < n; i++) { remainingBurst[i] = burst[i]; } while (completed < n) { int idx = -1; int highestPriority = INT_MAX; for (int i = 0; i < n; i++) { if (arrival[i] <= currentTime && !isCompleted[i]) { if (priority[i] < highestPriority) { highestPriority = priority[i]; idx = i; } } } if (idx != -1) { if (!isFirstExecution[idx]) { firstExecutionTime[idx] = currentTime; isFirstExecution[idx] = true; } remainingBurst[idx]--; if (remainingBurst[idx] == 0) { isCompleted[idx] = true; completed++; completionTime[idx] = currentTime + 1; } } currentTime++; } int totalTurnaroundTime = 0; int totalWaitingTime = 0; int totalResponseTime = 0; for (int i = 0; i < n; i++) { int turnaroundTime = completionTime[i] - arrival[i]; int waitingTime = turnaroundTime - burst[i]; int responseTime = firstExecutionTime[i] - arrival[i]; totalTurnaroundTime += turnaroundTime; totalWaitingTime += waitingTime; totalResponseTime += responseTime; cout << "Process " << pid[i] << " - Turnaround Time: " << turnaroundTime << ", Waiting Time: " << waitingTime << ", Response Time: " << responseTime << endl; } double avgTurnaroundTime = (double)totalTurnaroundTime / n; double avgWaitingTime = (double)totalWaitingTime / n; double avgResponseTime = (double)totalResponseTime / n; cout << "\nAverage Turnaround Time: " << avgTurnaroundTime << endl; cout << "Average Waiting Time: " << avgWaitingTime << endl; cout << "Average Response Time: " << avgResponseTime << endl; } int main() { int n; cout << "Enter the number of processes: "; cin >> n; int pid[n], arrival[n], burst[n], priority[n]; for (int i = 0; i < n; i++) { pid[i] = i + 1; cout << "Enter arrival time, burst time, and priority for Process " << i + 1 << ": "; cin >> arrival[i] >> burst[i] >> priority[i]; } preemptivePriorityScheduling(pid, arrival, burst, priority, n); return 0; }