#include #include using namespace std; struct process { int pid; // Process ID int burst_time; // CPU Burst Time int working_time; // Time this process executed, if working_time==burst_time, process is complete int t_round; // turn around time, time needed for the process to complete int arrivalTime; // time for a process to arrive to queue, which is zero in this case int remainTime; // time remaining for a process to complete execution }; main() { int numProcesses; cout << "Enter the number of processes: "; cin >> numProcesses; struct process pr[numProcesses]; int i, totalExecutionTime = 0, timeQuantum, flag = 0, n; float totalWaitingTime = 0; for (i = 0; i < numProcesses; i++) { cout << "\nEnter PID for process " << i + 1 << " (ms): "; cin >> pr[i].pid; cout << "\nEnter Burst Time for process " << i + 1 << " (ms): "; cin >> pr[i].burst_time; } // arrival time is zero for (int i = 0; i < numProcesses; i++) { pr[i].arrivalTime = 0; } printf("\nEnter Time Quantum: "); scanf("%d", &timeQuantum); printf("\n"); for (i = 0; numProcesses != 0;) { /** * this below condition check the remain time for any process is less than or equal with the time quantum * or not and also check the remain time is greater than 0 or not. if both condition are true that means * the process can execute fully at one time. */ if (pr[i].remainTime <= timeQuantum && pr[i].remainTime > 0) { totalExecutionTime += pr[i].remainTime; pr[i].remainTime = 0; flag = 1; } else if (pr[i].remainTime > 0) { pr[i].remainTime -= timeQuantum; totalExecutionTime += timeQuantum; } if (flag == 1 && pr[i].remainTime == 0) { printf("P[%d] | waiting Time : %d\n", i + 1, totalExecutionTime - pr[i].burst_time); totalWaitingTime += totalExecutionTime - pr[i].burst_time; flag = 0; numProcesses--; } if (i == n - 1) i = 0; else if (pr[i + 1].arrivalTime <= totalExecutionTime) { i++; } else i = 0; } totalWaitingTime = (float)totalWaitingTime / n; printf("\nThe Average Waiting Time : %.2f \n", totalWaitingTime); }