Problem of the day
You are given the arrival times, the burst times and the priority of ‘N’ processes in a CPU. You need to find the order of process scheduling according to Priority Based CPU Scheduling. Along with the order, also find the individual waiting and turnaround time for all the processes in order of their schedule.
Note:1. The processes are numbered from 1 to 'N'.
2. The process with lowest arrival time will be scheduled first, followed by the next lowest arrival time, and so on.
3. If any two processes have the same arrival time, then you have to run the process based on the priority of the process. The highest priority process will be scheduled first, followed by the next highest priority, and so on.
4. If the two processes have the same priority, then you have to run the process with the lowest process number first.
The first line contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains ‘N’ space-separated integers denoting the Arrival Times.
The second line of each test case contains ‘N’ space-separated integers denoting the Burst Times.
The third line of each test case contains ‘N’ space-separated integers denoting the Priorities of the processes.
Output Format:
For each test case, print three lines:
The first line should contain ‘N’ space-separated integers denoting the order of the processes in which they are scheduled.
The second line should contain ‘N’ space-separated integers denoting the waiting time of the processes.
The third line should contain ‘N’ space-separated integers denoting the turnaround time of the processes.
Print the output of each test case in three separate lines.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^4
1 <= X, Y, Z <= 10^4
Where ‘T’ is the number of test cases, ‘N’ denotes the number of processes, ‘X’, ‘Y’, Z’ denotes the arrival time, burst time and priority of a process respectively.
Time Limit: 1 sec
1
5
0 1 3 2 4
3 6 1 2 4
3 4 9 7 8
1 2 4 3 5
0 2 7 8 8
3 8 9 9 12
In the first test case,
The table of the processes is shown below and with the help of this table we can find the turnaround and waiting time of each individual process:
Gantt chart:
1
4
8 5 1 0
1 2 3 4
1 2 3 4
4 3 2 1
0 3 2 1
4 6 4 2
In the first test case,
The table of the processes is shown below and with the help of this table we can find the turnaround and waiting time of each individual process:
Gantt Chart:
Can you think of sorting processes according to requirement?
Approach: In order to implement priority based scheduling, let us first understand what priority based CPU scheduling actually means.
Priority Scheduling basically is a non-preemptive algorithm. As the name suggests it is based on priorities given to the processes. The processes with a higher priority are processed first and ones with the lowest priority are processed at last.
The flow of processes in priority scheduling is:
Now, let’s recall some terms:
So, the algorithm for priority scheduling:
O((N * log(N)), where N is the number of processes.
As we are sorting the processes based on different comparisons, the time complexity of this approach is O(N * log(N)).
O(N), where N is the number of processes.
As we are creating an extra array of the size ‘N’ to store the ‘burst time’ and ‘priority’ of each process. Hence, space complexity will be O(N).