Facebook Pixel

Starvation and Aging

In CPU scheduling, algorithms often make decisions based on specific metrics like the shortest burst time or the highest priority. While this optimizes overall system performance, it can lead to a severe problem called Starvation.

Starvation (also known as indefinite blocking) occurs when a process is ready to run, but the CPU is continuously allocated to other processes. The starving process is indefinitely denied the CPU because higher-priority or shorter processes keep cutting in front of it in the ready queue.

Real-Life Example

Imagine a busy restaurant that doesn't take reservations and seats parties based on "Shortest Party First" to maximize throughput. If you arrive with a massive party of 15 people, you will have to wait for a very large table. If groups of 2 and 4 people keep walking in the door continuously, the host will keep seating them ahead of you. You and your 14 friends might literally starve waiting for your table!

Example 1: The Anatomy of Starvation

Let's see how starvation happens mathematically using Preemptive Priority Scheduling. Remember, a lower number means a higher priority.

Consider the following processes:

ProcessArrival Time (ms)Burst Time (ms)Priority
P10510 (Lowest)
P2131 (Highest)
P3342
P4621 (Highest)

Here is what happens if we do NOT use Aging:

1. At 0, P1 starts running.

2. At 1, P2 (Priority 1) arrives and immediately preempts P1! P2 runs until time 4.

3. At 4, P2 is done. P3 (Priority 2) arrived at 3. Since P3 beats P1, P3 runs.

4. At 6, P4 (Priority 1) arrives and preempts P3! P4 runs until time 8.

5. At 8, P4 is done. P3 resumes and finishes its last 2 ms at time 10.

6. Finally, at time 10, P1 resumes and finishes its remaining 4 ms.

Here is the Gantt chart:

text
+----+---------+----+----+----+---------+
| P1 |   P2    | P3 | P4 | P3 |   P1    |
+----+---------+----+----+----+---------+
0    1         4    6    8    10        14

Let's calculate the Waiting Time and Turnaround Time for the Starving Process (P1):

- P1 Turnaround Time: 14 (Completion) - 0 (Arrival) = 14 ms.

- P1 Waiting Time: 14 (Turnaround) - 5 (Burst) = 9 ms.

Even though P1 arrived first, it suffered a massive 9 ms wait time because P2, P3, and P4 continuously cut in line! If a steady stream of P2s kept arriving, P1 would be blocked forever.

Example 2: The Cure is Aging

The solution to starvation is Aging. Aging is a technique that gradually increases the priority of a process based on how long it has been waiting in the queue.

Let's apply a simple Aging Rule to the exact same processes: For every 4 ms a process spends waiting, its Priority Number decreases by 5 (making it much higher priority).

ProcessArrival Time (ms)Burst Time (ms)Priority
P10510 (Lowest)
P2131 (Highest)
P3342
P4621 (Highest)

Here is how Aging saves P1:

1. At 0, P1 starts running.

2. At 1, P2 preempts P1. P2 runs until time 4. (P1 starts accumulating wait time).

3. At 4, P3 runs.

4. At 5, P1 has been waiting for exactly 4 ms (from time 1 to 5). AGING ACTIVATES! P1's Priority drops from 10 to 5.

5. At 6, P4 arrives and preempts P3. P4 runs until time 8.

6. At 8, P4 finishes. P3 resumes running.

7. At 9, P1 has now been waiting for exactly 8 ms (from time 1 to 9). AGING ACTIVATES AGAIN! P1's Priority drops from 5 to 0!

8. At time 9, P1 (Priority 0) now beats P3 (Priority 2). P1 preempts P3!

9. P1 runs to completion at time 13. P3 runs its final 1 ms from 13 to 14.

Here is the new Gantt chart:

text
+----+---------+----+----+----+---------+----+
| P1 |   P2    | P3 | P4 | P3 | P1(Age) | P3 |
+----+---------+----+----+----+---------+----+
0    1         4    6    8    9         13   14

Let's recalculate the Waiting Time and Turnaround Time for P1 after Aging:

- P1 Turnaround Time: 13 (Completion) - 0 (Arrival) = 13 ms.

- P1 Waiting Time: 13 (Turnaround) - 5 (Burst) = 8 ms.

Thanks to Aging, P1 forcibly claimed the CPU at time 9, reducing its waiting time and preventing it from being endlessly blocked by newly arriving high-priority processes!

Algorithms Prone to Starvation

  • Shortest Job First (SJF): Long jobs starve if short jobs keep arriving.
  • Priority Scheduling: Low priority jobs starve if high priority jobs keep arriving.
  • Multilevel Queue: Background queues starve if foreground queues are constantly active.

Starvation-Free Algorithms

  • First-Come, First-Served (FCFS): Everyone gets served eventually.
  • Round Robin (RR): Everyone gets an equal slice of the CPU.
  • Highest Response Ratio Next (HRRN): Has built-in aging via the response ratio formula.
  • Lottery Scheduling: Every process with >0 tickets will eventually win.

Identifying Starvation Risks

Question 1 of 1

Test your knowledge of which algorithms require aging.

Which of the following scheduling algorithms absolutely REQUIRES an aging mechanism to be considered fair for all processes?
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.