![]() |
VOOZH | about |
Given an array arr[] representing burst time of N processes scheduled using the Round Robin Algorithm with given quantum time Q. Assuming that all the process arrive at time t = 0, the task is to find the order in which the process execute.
Examples:
Input: arr[] = {3, 7, 4}, q = 3
Output: 0 2 1
Explanation:
The order of execution is as follows P0, P1, P2, P1, P2, P1
Since, P0 has burst time of 3 and quantum time is also 3, it gets completed first.
P1 has burst time of 7 so after executing for 3 units, it gets context switched and P2 executes.
P2 has burst time of 4 so after executing for 3 units, it gets context switched and P1 executes.
Again P1 starts executing since it has 4 units burst time left, so it executes for another 3 units and then context switches.
Now process P2 executes for 1 unit and gets completed.
In the end process P1 is completed.
They complete the execution in the order P0, P2, P1.Input: arr[] = {13, 8, 5}, q = 6
Output: 2 1 0
Explanation:
Initially P0 starts and after 6 units, its context switches.
P1 executes for 6 units and context switches.
Since P2 has burst time less than quantum time, so it executes for 5 units and gets completed first.
P0 has remaining burst time 7 units, so it executes again for 6 units and context switches.
P1 has remaining burst time as 2 units and it gets completed second.
In the end process P0 gets completed.
They complete the execution in the order P2, P1, P0.
Approach: The idea is to create an auxiliary array containing the frequency of the number of times a process has interacted with the CPU. Then freq[] array can be sorted in the increasing order of frequencies. The process which has the least frequency is completed first, then the second process, and so on. The sorted array gives the order completion of the process. Below are the steps:
Below is the implementation of the above approach:
0 2 1
Time Complexity: O(N*log N)
Auxiliary Space: O(N)