CPU Scheduling

CPU Scheduling Diagrams: Gantt Charts for Each Algorithm

Text Gantt charts for FCFS, shortest job first, shortest remaining time, round robin and priority scheduling, using one set of example processes.

On this page
  1. The short answer
  2. How to read a Gantt chart
  3. The example processes
  4. First come, first served
  5. Shortest job first, non-preemptive
  6. Shortest remaining time first
  7. Round robin with a time slice of 4
  8. Priority scheduling, non-preemptive
  9. Multilevel queues
  10. Comparing the averages

The short answer

A Gantt chart is a timeline with one block for each stretch of time that a process holds the CPU. Reading left to right shows the order in which the processes ran, and any gap shows a period when the CPU sat idle. Below are text versions of Gantt charts for five scheduling methods, all run on the same four processes.

How to read a Gantt chart

  • Time runs from left to right, starting at 0.
  • Each block names the process that was running, with the times it started and stopped.
  • A process that appears in several blocks was interrupted and resumed. This happens when a process is preempted, as in round robin.
  • In these text charts, the block widths are not drawn to scale.

The example processes

Four processes arrive at different times:

Process Arrival time CPU burst Priority
P1 0 8 2
P2 1 4 4
P3 2 9 1
P4 3 5 3

A lower number means a higher priority. The priority column matters only for priority scheduling.

Waiting time here is turnaround time minus CPU burst, where turnaround time is completion time minus arrival time.

First come, first served

| P1 (0-8) | P2 (8-12) | P3 (12-21) | P4 (21-26) |

The waiting times are P1 0, P2 7, P3 10 and P4 18. The average is 8.75 time units.

Shortest job first, non-preemptive

At time 8, P2 (4 units), P3 (9 units) and P4 (5 units) are all ready, so the shortest, P2, runs. At time 12, P4 (5 units) is shorter than P3 (9 units), so P4 runs next.

| P1 (0-8) | P2 (8-12) | P4 (12-17) | P3 (17-26) |

The waiting times are P1 0, P2 7, P3 15 and P4 9. The average is 7.75 time units.

Shortest remaining time first

P2 arrives at time 1 needing 4 units, which is less than the 7 units P1 has left, so P2 takes the CPU. When P2 finishes at time 5, P4 needs 5 units, P1 has 7 left and P3 needs 9, so P4 runs next. P1 then finishes, followed by P3.

| P1 (0-1) | P2 (1-5) | P4 (5-10) | P1 (10-17) | P3 (17-26) |

The waiting times are P1 9, P2 0, P3 15 and P4 2. The average is 6.5 time units.

Round robin with a time slice of 4

Round robin does not interrupt a process when another one arrives. It switches only when a slice ends, and a process whose slice ends goes to the back of the queue behind the processes already waiting.

| P1 (0-4) | P2 (4-8) | P3 (8-12) | P4 (12-16) | P1 (16-20) | P3 (20-24) | P4 (24-25) | P3 (25-26) |

The waiting times are P1 12, P2 3, P3 15 and P4 17. The average is 11.75 time units.

Priority scheduling, non-preemptive

At time 8, P3 has the highest priority of the ready processes (P2, P3 and P4), so it runs to completion. P4 comes next, then P2.

| P1 (0-8) | P3 (8-17) | P4 (17-22) | P2 (22-26) |

The waiting times are P1 0, P2 21, P3 6 and P4 14. The average is 10.25 time units.

Multilevel queues

Multilevel queue and multilevel feedback queue scheduling do not have one fixed chart for these processes. The chart depends on how many queues there are, which algorithm each uses, and how the queues share the CPU. For example, with a foreground queue on round robin above a background queue on first come, first served, and strict priority between the queues, a background process runs only when the foreground queue is empty. A chart for these schemes has to start from those rules.

Comparing the averages

Method Average waiting time (time units)
Shortest remaining time first 6.5
Shortest job first, non-preemptive 7.75
First come, first served 8.75
Priority, non-preemptive 10.25
Round robin, time slice of 4 11.75

These averages hold only for this example. Another set of processes can change the order. Round robin had the highest average waiting time here, yet its first-response times of 0, 3, 6 and 9 are far shorter than those of first come, first served, which are 0, 7, 10 and 18. Response time matters for interactive work. A single average does not settle which scheduler is best.

The complete guide to cpu scheduling

CPU Scheduling Algorithms Explained
Read the guide

Keep exploring

Guides from other topics

All topics →