VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/cpu-scheduling-in-operating-systems-using-priority-queue-with-gantt-chart/

⇱ CPU Scheduling in Operating Systems using priority queue with gantt chart - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CPU Scheduling in Operating Systems using priority queue with gantt chart

Last Updated : 12 Jul, 2025

Prerequisite:

CPU Scheduling in Operating Systems

Different Scheduling Algorithms:

  1. First Come First Serve CPU Scheduling: Simplest scheduling algorithm that schedules according to arrival times of processes. First come first serve scheduling algorithm states that the process that requests the CPU first is allocated the CPU first. It is implemented by using the FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the process at the head of the queue. The running process is then removed from the queue. FCFS is a non-preemptive scheduling algorithm.
  2. Shortest Job First(Preemptive): In Preemptive Shortest Job First Scheduling, jobs are put into the ready queue as they arrive, but as a process with short burst time arrives, the existing process is preempted or removed from execution, and the shorter job is executed first.
  3. Shortest Job First(Non-Preemptive): In Non-Preemptive Shortest Job First, a process which has the shortest burst time is scheduled first. If two processes have the same bust time then FCFS is used to break the tie.
  4. Longest Job First(Preemptive): It is similar to an SJF scheduling algorithm. But, in this scheduling algorithm, we give priority to the process having the largest burst time remaining.
  5. Longest Job First(Non-Preemptive): It is similar to an SJF scheduling algorithm. But, in this scheduling algorithm, we give priority to the process having the longest burst time. This is non-preemptive in nature i.e., when any process starts executing, can’t be interrupted before complete execution.
  6. Round Robin Scheduling: To implement Round Robin scheduling, we keep the ready queue as a FIFO queue of processes. New processes are added to the tail of the ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer to interrupt after 1-time quantum, and dispatches the process. One of two things will then happen. The process may have a CPU burst of less than 1-time quantum. In this case, the process itself will release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue. Otherwise, if the CPU burst of the currently running process is longer than 1-time quantum, the timer will go off and will cause an interrupt to the operating system. A context switch will be executed, and the process will be put at the tail of the ready queue. The CPU scheduler will then select the next process in the ready queue.
  7. Priority Based(Preemptive) Scheduling: In Preemptive Priority Scheduling, at the time of arrival of a process in the ready queue, its priority is compared with the priority of the other processes present in the ready queue as well as with the one which is being executed by the CPU at that point of time. The One with the highest priority among all the available processes will be given the CPU next. In this program, we have both options, whether to consider highest number as highest priority or lowest number as highest priority.
  8. Priority Based(Non-Preemptive) Scheduling: In the Non Preemptive Priority scheduling, The Processes are scheduled according to the priority number assigned to them. Once the process gets scheduled, it will run till the completion. Generally, the lower the priority number, the higher is the priority of the process. The people might get confused with the priority numbers, hence in the GATE, there clearly mention which one is the highest priority and which one is the lowest one. In this program, we have both options, whether to consider highest number as highest priority or lowest number as highest priority.
  9. Highest Response Ratio Next(HRRN) Scheduling: Highest Response Ratio Next (HRRN) is one of the most optimal scheduling algorithms. This is a non-preemptive algorithm in which, the scheduling is done on the basis of an extra parameter called Response Ratio. A Response Ratio is calculated for each of the available jobs and the Job with the highest response ratio is given priority over the others.

Consider the following table:

πŸ‘ Image

Below is the implementation of the above algorithms using a

priority queue

:

OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| 1 | 0 | 4 | 4 | 4 | 0 | 0 |
| 2 | 1 | 2 | 6 | 5 | 3 | 3 |
| 3 | 2 | 3 | 9 | 7 | 4 | 4 |
| 4 | 3 | 5 | 14 | 11 | 6 | 6 |
| 5 | 4 | 1 | 15 | 11 | 10 | 10 |
| 6 | 5 | 4 | 19 | 14 | 10 | 10 |
| 7 | 6 | 6 | 25 | 19 | 13 | 13 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+

Total completion time :- 92
Average completion time :- 13.1429

Total turnaround time :- 71
Average turnaround time :- 10.1429

Total waiting time :- 46
Average waiting time :- 6.57143

Total response time :- 46
Average response time :- 6.57143


Gantt Chart (IS indicates ideal state) :-

+----------+------+--------+------------+----+----------+--------------+
| P1 | P2 | P3 | P4 | P5 | P6 | P7 |
+----------+------+--------+------------+----+----------+--------------+
0 4 6 9 14 15 19 25



OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| 1 | 0 | 4 | 7 | 7 | 3 | 0 |
| 2 | 1 | 2 | 3 | 2 | 0 | 0 |
| 3 | 2 | 3 | 10 | 8 | 5 | 5 |
| 4 | 3 | 5 | 19 | 16 | 11 | 11 |
| 5 | 4 | 1 | 5 | 1 | 0 | 0 |
| 6 | 5 | 4 | 14 | 9 | 5 | 5 |
| 7 | 6 | 6 | 25 | 19 | 13 | 13 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+

Total completion time :- 83
Average completion time :- 11.8571

Total turnaround time :- 62
Average turnaround time :- 8.85714

Total waiting time :- 37
Average waiting time :- 5.28571

Total response time :- 34
Average response time :- 4.85714


Gantt Chart (IS indicates ideal state) :-

+----+------+----+----+------+--------+----------+------------+--------------+
| P1 | P2 | P1 | P5 | P1 | P3 | P6 | P4 | P7 |
+----+------+----+----+------+--------+----------+------------+--------------+
0 1 3 4 5 7 10 14 19 25



OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| 1 | 0 | 4 | 4 | 4 | 0 | 0 |
| 2 | 1 | 2 | 7 | 6 | 4 | 4 |
| 3 | 2 | 3 | 10 | 8 | 5 | 5 |
| 4 | 3 | 5 | 19 | 16 | 11 | 11 |
| 5 | 4 | 1 | 5 | 1 | 0 | 0 |
| 6 | 5 | 4 | 14 | 9 | 5 | 5 |
| 7 | 6 | 6 | 25 | 19 | 13 | 13 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+

Total completion time :- 84
Average completion time :- 12

Total turnaround time :- 63
Average turnaround time :- 9

Total waiting time :- 38
Average waiting time :- 5.42857

Total response time :- 38
Average response time :- 5.42857


Gantt Chart (IS indicates ideal state) :-

+----------+----+------+--------+----------+------------+--------------+
| P1 | P5 | P2 | P3 | P6 | P4 | P7 |
+----------+----+------+--------+----------+------------+--------------+
0 4 5 7 10 14 19 25


OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| 1 | 0 | 4 | 23 | 23 | 19 | 0 |
| 2 | 1 | 2 | 22 | 21 | 19 | 12 |
| 3 | 2 | 3 | 24 | 22 | 19 | 0 |
| 4 | 3 | 5 | 19 | 16 | 11 | 0 |
| 5 | 4 | 1 | 20 | 16 | 15 | 15 |
| 6 | 5 | 4 | 21 | 16 | 12 | 0 |
| 7 | 6 | 6 | 25 | 19 | 13 | 0 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+

Total completion time :- 154
Average completion time :- 22

Total turnaround time :- 133
Average turnaround time :- 19

Total waiting time :- 108
Average waiting time :- 15.4286

Total response time :- 27
Average response time :- 3.85714


Gantt Chart (IS indicates ideal state) :-

+------+----+------+----+----------+----+------+----+----+----+----+------+----+----+----+----+----+----+
| P1 | P3 | P4 | P6 | P7 | P4 | P6 | P2 | P1 | P3 | P7 | P4 | P5 | P6 | P2 | P1 | P3 | P7 |
+------+----+------+----+----------+----+------+----+----+----+----+------+----+----+----+----+----+----+
0 2 3 5 6 10 11 13 14 15 16 17 19 20 21 22 23 24 25


OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| 1 | 0 | 4 | 4 | 4 | 0 | 0 |
| 2 | 1 | 2 | 24 | 23 | 21 | 21 |
| 3 | 2 | 3 | 22 | 20 | 17 | 17 |
| 4 | 3 | 5 | 9 | 6 | 1 | 1 |
| 5 | 4 | 1 | 25 | 21 | 20 | 20 |
| 6 | 5 | 4 | 19 | 14 | 10 | 10 |
| 7 | 6 | 6 | 15 | 9 | 3 | 3 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+

Total completion time :- 118
Average completion time :- 16.8571

Total turnaround time :- 97
Average turnaround time :- 13.8571

Total waiting time :- 72
Average waiting time :- 10.2857

Total response time :- 72
Average response time :- 10.2857


Gantt Chart (IS indicates ideal state) :-

+----------+------------+--------------+----------+--------+------+----+
| P1 | P4 | P7 | P6 | P3 | P2 | P5 |
+----------+------------+--------------+----------+--------+------+----+
0 4 9 15 19 22 24 25


OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| 1 | 0 | 4 | 8 | 8 | 4 | 0 |
| 2 | 1 | 2 | 4 | 3 | 1 | 1 |
| 3 | 2 | 3 | 16 | 14 | 11 | 2 |
| 4 | 3 | 5 | 23 | 20 | 15 | 5 |
| 5 | 4 | 1 | 11 | 7 | 6 | 6 |
| 6 | 5 | 4 | 20 | 15 | 11 | 6 |
| 7 | 6 | 6 | 25 | 19 | 13 | 7 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+

Total completion time :- 107
Average completion time :- 15.2857

Total turnaround time :- 86
Average turnaround time :- 12.2857

Total waiting time :- 61
Average waiting time :- 8.71429

Total response time :- 27
Average response time :- 3.85714

Time Quantum for round robin :- 2


Gantt Chart (IS indicates ideal state) :-

+------+------+------+------+------+----+------+------+----+------+------+------+----+------+
| P1 | P2 | P3 | P1 | P4 | P5 | P6 | P7 | P3 | P4 | P6 | P7 | P4 | P7 |
+------+------+------+------+------+----+------+------+----+------+------+------+----+------+
0 2 4 6 8 10 11 13 15 16 18 20 22 23 25


OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+----------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time | Priority |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+----------+
| 1 | 0 | 4 | 25 | 25 | 21 | 0 | 2 |
| 2 | 1 | 2 | 22 | 21 | 19 | 0 | 4 |
| 3 | 2 | 3 | 21 | 19 | 16 | 0 | 6 |
| 4 | 3 | 5 | 12 | 9 | 4 | 0 | 10 |
| 5 | 4 | 1 | 19 | 15 | 14 | 14 | 8 |
| 6 | 5 | 4 | 9 | 4 | 0 | 0 | 12 |
| 7 | 6 | 6 | 18 | 12 | 6 | 6 | 9 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+----------+

Total completion time :- 126
Average completion time :- 18

Total turnaround time :- 105
Average turnaround time :- 15

Total waiting time :- 80
Average waiting time :- 11.4286

Total response time :- 20
Average response time :- 2.85714


Gantt Chart (IS indicates ideal state) :-

+----+----+----+------+----------+--------+--------------+----+------+----+--------+
| P1 | P2 | P3 | P4 | P6 | P4 | P7 | P5 | P3 | P2 | P1 |
+----+----+----+------+----------+--------+--------------+----+------+----+--------+
0 1 2 3 5 9 12 18 19 21 22 25

OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+----------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time | Priority |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+----------+
| 1 | 0 | 4 | 4 | 4 | 0 | 0 | 2 |
| 2 | 1 | 2 | 25 | 24 | 22 | 22 | 4 |
| 3 | 2 | 3 | 23 | 21 | 18 | 18 | 6 |
| 4 | 3 | 5 | 9 | 6 | 1 | 1 | 10 |
| 5 | 4 | 1 | 20 | 16 | 15 | 15 | 8 |
| 6 | 5 | 4 | 13 | 8 | 4 | 4 | 12 |
| 7 | 6 | 6 | 19 | 13 | 7 | 7 | 9 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+----------+

Total completion time :- 113
Average completion time :- 16.1429

Total turnaround time :- 92
Average turnaround time :- 13.1429

Total waiting time :- 67
Average waiting time :- 9.57143

Total response time :- 67
Average response time :- 9.57143


Gantt Chart (IS indicates ideal state) :-

+----------+------------+----------+--------------+----+--------+------+
| P1 | P4 | P6 | P7 | P5 | P3 | P2 |
+----------+------------+----------+--------------+----+--------+------+
0 4 9 13 19 20 23 25


OUTPUT :-

+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| Process No. | Arrival Time | Burst Time | Completion Time | Turnaround Time | Waiting Time | Response Time |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+
| 1 | 0 | 4 | 4 | 4 | 0 | 0 |
| 2 | 1 | 2 | 6 | 5 | 3 | 3 |
| 3 | 2 | 3 | 10 | 8 | 5 | 5 |
| 4 | 3 | 5 | 15 | 12 | 7 | 7 |
| 5 | 4 | 1 | 7 | 3 | 2 | 2 |
| 6 | 5 | 4 | 19 | 14 | 10 | 10 |
| 7 | 6 | 6 | 25 | 19 | 13 | 13 |
+-------------+--------------+------------+-----------------+-----------------+--------------+---------------+

Total completion time :- 86
Average completion time :- 12.2857

Total turnaround time :- 65
Average turnaround time :- 9.28571

Total waiting time :- 40
Average waiting time :- 5.71429

Total response time :- 40
Average response time :- 5.71429


Gantt Chart (IS indicates ideal state) :-

+----------+------+----+--------+------------+----------+--------------+
| P1 | P2 | P5 | P3 | P4 | P6 | P7 |
+----------+------+----+--------+------------+----------+--------------+
0 4 6 7 10 15 19 25

Comment