Operating Systems chapters (5)
  1. 01OS Structures & System Calls
  2. 02Process & Thread Management
  3. 03CPU Scheduling Algorithms
  4. 04Process Synchronization & Deadlocks
  5. 05Memory Management & Virtual Memory

Chapter 03 · OS

CPU Scheduling Algorithms

4 topics · 1 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

The CPU scheduler picks which ready process runs next.

This chapter defines the criteria (waiting, turnaround, response time) and works through FCFS, SJF/SRTF, Priority, Round Robin and multilevel queues with Gantt-chart numericals in GATE style.

Scheduling numericals (average waiting time, Gantt chart) are among the most repeated GATE question types.

Linux CFS, Windows priority scheduling, real-time systems, cloud schedulers and fairness tuning.

1

Scheduling Criteria

Arrival time (AT) is when a process enters the ready queue; burst time (BT) is CPU time needed; completion time (CT) is when it finishes. Turnaround time TAT = CT - AT. Waiting time WT = TAT - BT. Response time RT = first CPU time - AT.

In short: TAT is total time in the system, WT is time spent idle in the ready queue, RT is how long until the first reply.

CPU runs processes in turns

Think about it

Every numerical reduces to these formulae applied to a correct Gantt chart.

Goals

  • 1maximize CPU utilization and throughput
  • 2minimize turnaround, waiting and response time

Preemptive scheduling can take the CPU away (timer or higher-priority arrival); non-preemptive lets a process run until it blocks or terminates.

For CPU-only processes in non-preemptive scheduling, RT = WT.

Scheduling decisions occur when a process switches Running→Waiting, Running→Ready, Waiting→Ready or terminates; only the first and last are possible in a non-preemptive system.

The dispatcher’s delay is dispatch latency.

Always draw the Gantt chart, note idle CPU gaps, and compute averages last.

Key points

  • Gantt chart: Timeline showing which process runs during each interval.
  • Throughput: Number of processes completed per unit time.
  • Convoy effect: Short processes stuck behind a long CPU-bound process in FCFS.
  • TAT = CT - AT; WT = TAT - BT; RT = (first run start) - AT.
  • Average over all processes at the end; show units.
2

FCFS and SJF Scheduling

FCFS runs processes in order of arrival (non-preemptive). SJF picks the ready process with the smallest next CPU burst (non-preemptive). SRTF is the preemptive version: whenever a new process arrives with burst shorter than the remaining time of the running one, it preempts.

In short: FCFS is a queue at a ticket counter. SJF/SRTF always serves the shortest job first, which gives low average waiting.

Think about it

They illustrate the trade-off between simplicity, fairness and optimality.

FCFS is simple and starvation-free but suffers from the convoy effect and can have a high average waiting time.

SJF is provably optimal for minimum average waiting time among non-preemptive algorithms, and SRTF is optimal among preemptive ones.

The difficulty is knowing the next burst length; it is predicted with exponential averaging: τ(n+1) = α·t(n) + (1-α)·τ(n).

SJF/SRTF can starve long processes.

Ties are normally broken by earlier arrival (or lower process ID).

In SRTF a process that arrives with equal remaining time does not preempt, as it would only add a switch.

Key points

  • Exponential averaging: Estimate next burst from the last actual burst and the previous estimate.
  • Starvation: A ready process waits indefinitely because others keep getting priority.
  • SJF = non-preemptive shortest burst; SRTF = preemptive shortest remaining time.
3

Priority & Multilevel Feedback

Priority scheduling gives the CPU to the highest-priority ready process (preemptive or non-preemptive; read whether a smaller number means higher priority). Multilevel queue scheduling partitions the ready queue into separate queues with fixed priority between queues; the Multilevel Feedback Queue (MLFQ) additionally lets processes move between queues based on observed behaviour.

In short: VIPs go first; in the feedback version jobs that hog the CPU are demoted and jobs that wait long are promoted.

Think about it

Real systems need to favour interactive or real-time tasks.

MLFQ

new jobs enter the top queue with a short quantum (e.

g.

Q0: RR q=8, Q1: RR q=16, Q2: FCFS); a job that uses its whole quantum is demoted, a job that blocks for I/O stays high, and long-waiting jobs are promoted (aging).

This favours I/O-bound and interactive jobs and approximates SJF without knowing burst lengths.

An MLFQ is defined by: number of queues, algorithm per queue, rule to upgrade, rule to demote and rule for the initial queue.

A fixed multilevel queue (system > interactive > batch) keeps each process permanently in its queue and can starve lower queues unless time slices are shared between queues.

Priorities can be internal (burst length, memory needs) or external (importance, payment).

SJF is priority scheduling with priority = 1 / next burst.

The major problem is starvation of low-priority processes; aging gradually increases the priority of waiting processes to fix it.

Another issue is priority inversion: a high-priority task waits for a lock held by a low-priority one while a medium-priority task runs; priority inheritance temporarily boosts the lock holder.

Key points

  • Aging: Gradually raising priority of long-waiting processes to prevent starvation.
  • Priority inversion: High-priority process blocked by lower-priority one holding a resource.
  • Priority inheritance: Lock holder temporarily takes the priority of the blocked high-priority process.
  • Multilevel queue: Separate ready queues per process class with fixed priority between queues.
  • MLFQ: Multilevel feedback queue: processes move between queues by behaviour.
  • Demotion: Moving a process to a lower-priority queue after using its full quantum.
  • I/O-bound vs CPU-bound: Many short CPU bursts between I/O waits vs long CPU bursts with little I/O.
  • MLFQ example: Q0 RR q=8, Q1 RR q=16, Q2 FCFS. A 30-unit CPU-bound job runs 8 in Q0, 16 in Q1, then the remaining 6 in Q2; a short interactive burst of 5 finishes inside Q0 and keeps its high priority.

Example

Code
priority (smaller = higher), non-preemptive: P1 (AT0, BT4, pr2), P2 (AT1, BT3, pr1), P3 (AT2, BT2, pr3). P1 runs 0-4; then P2 (pr1) 4-7; then P3 7-9. WT: P1 0, P2 3, P3 5 → average 8/3 ≈ 2.67.
4

Round Robin Scheduling

Round Robin (RR) is preemptive FCFS with a fixed time quantum q. Each process gets the CPU for up to q units, then goes to the tail of the ready queue.

In short: Everyone gets a short turn in a circle.

Think about it

It is the basis of time-sharing and interactive scheduling.

With n ready processes and quantum q, no process waits more than (n-1)q before its next turn.

If q is very large, RR degenerates into FCFS; if q is very small, context-switch overhead dominates.

A good rule

q slightly larger than the typical interactive burst (about 80% of bursts should finish within q).

RR gives better response time than SJF but usually higher average turnaround.

For a numerical, keep the queue order carefully: a process that arrives at the same instant a quantum ends is normally queued before the preempted process (state the convention if the question is ambiguous).

Key points

  • Time quantum: Maximum CPU slice a process gets before preemption.
  • Ready queue (circular): FIFO queue of runnable processes.
  • Max wait before next turn = (n-1)·q for n processes in the queue.

Solved examples

Example 01 · Intermediate

Processes: P1 (AT 0, BT 8), P2 (AT 1, BT 4), P3 (AT 2, BT 9), P4 (AT 3, BT 5). Compute average waiting time and turnaround time for FCFS.

Given Data:
  • P1: AT0 BT8
  • P2: AT1 BT4
  • P3: AT2 BT9
  • P4: AT3 BT5
Required To Find:

Average WT and TAT

Formula Applied:—
Step-by-Step Solution:
Step 1: Gantt chart: P1 0-8, P2 8-12, P3 12-21, P4 21-26.
Step 2: Completion times: P1 = 8, P2 = 12, P3 = 21, P4 = 26.
Step 3: TAT = CT - AT: P1 = 8, P2 = 11, P3 = 19, P4 = 23. Sum = 61, average = 15.25.
Step 4: WT = TAT - BT: P1 = 0, P2 = 7, P3 = 10, P4 = 18. Sum = 35, average = 8.75.
Final Answer: Average TAT = 15.25 units, average WT = 8.75 units.

Approach note: Construct the Gantt chart first, then derive CT, TAT and WT in a table.

Example 02 · Intermediate

Same processes as above. Find the average waiting time using non-preemptive SJF and using preemptive SRTF.

Given Data:
  • P1: AT0 BT8
  • P2: AT1 BT4
  • P3: AT2 BT9
  • P4: AT3 BT5
Required To Find:

Average WT for SJF and SRTF

Formula Applied:—
Step-by-Step Solution:
Step 1: Non-preemptive SJF: only P1 is present at t=0, so P1 runs 0-8. At t=8 ready are P2(4), P3(9), P4(5): choose P2 8-12, then P4 12-17, then P3 17-26.
Step 2: SJF TAT: P1 = 8, P2 = 11, P4 = 14, P3 = 24. WT = TAT - BT: P1 = 0, P2 = 7, P4 = 9, P3 = 15. Sum = 31, average = 7.75.
Step 3: SRTF: P1 starts at 0. At t=1 P2 (4) < P1 remaining (7), so P2 preempts and runs 1-5 (arrivals of P3 at 2 and P4 at 3 do not beat P2’s remaining time).
Step 4: At t=5 ready: P1 rem 7, P3 9, P4 5. Run P4 5-10, then P1 10-17, then P3 17-26.
Step 5: SRTF completion: P1 = 17, P2 = 5, P3 = 26, P4 = 10. TAT: 17, 4, 24, 7. WT: P1 = 9, P2 = 0, P3 = 15, P4 = 2. Sum = 26, average = 6.5.
Final Answer: SJF average WT = 7.75; SRTF average WT = 6.5 (better than FCFS 8.75).

Approach note: For SRTF re-evaluate at every arrival and compare against the remaining (not original) burst of the running process.

Example 03 · Advanced

Round Robin with quantum q = 2. All arrive at t = 0: P1 (BT 5), P2 (BT 3), P3 (BT 1). Find waiting, turnaround and response times.

Given Data:
  • q = 2
  • BT: P1 5, P2 3, P3 1
  • All AT = 0
Required To Find:

WT, TAT, RT per process and averages

Formula Applied:—
Step-by-Step Solution:
Step 1: Queue: P1, P2, P3. Gantt: P1 0-2 (rem 3), P2 2-4 (rem 1), P3 4-5 (done).
Step 2: Queue now: P1, P2. P1 5-7 (rem 1), P2 7-8 (done), P1 8-9 (done).
Step 3: Completion: P1 = 9, P2 = 8, P3 = 5. Since AT = 0, TAT = CT: 9, 8, 5; average = 22/3 ≈ 7.33.
Step 4: WT = TAT - BT: P1 = 4, P2 = 5, P3 = 4; average = 13/3 ≈ 4.33.
Step 5: Response time (first run - AT): P1 = 0, P2 = 2, P3 = 4; average = 2.
Final Answer: Average WT ≈ 4.33, average TAT ≈ 7.33, average RT = 2.

Approach note: Update the ready queue after each slice and put the preempted process at the tail.

Example 04 · Intermediate

Processes all arrive at 0: P1 BT=6, P2 BT=2, P3 BT=8. Quantum = 3. Find average waiting time for Round Robin.

Given Data:
    Required To Find:

    Average WT

    Formula Applied:—
    Step-by-Step Solution:
    Step 1: Gantt: P1 0-3, P2 3-5, P3 5-8, P1 8-11, P3 11-14, P3 14-16
    Step 2: CT: P1=11, P2=5, P3=16
    Step 3: TAT: 11, 5, 16; WT = TAT-BT: 5, 3, 8; average = 16/3 ≈ 5.33
    Final Answer: Average waiting time ≈ 5.33

    Approach note: Rebuild the ready queue after each quantum; do not reorder arbitrarily.

    Real-world applications

    OS

    Using CPU Scheduling Algorithms in real projects

    Linux CFS, Windows priority scheduling, real-time systems, cloud schedulers and fairness tuning.

    Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

    Common mistakes

    ❌ Myth: SJF is always practical because it is optimal.

    Why this is wrong: The OS cannot know the exact next CPU burst in advance; only estimates are possible.

    ✅ Verified Truth: SJF is optimal for average waiting time in theory, but it needs burst prediction and can starve long jobs.

    ⚠️ Exam Tip: Mention exponential averaging and starvation.

    ❌ Myth: Round Robin always gives the lowest average waiting time.

    Why this is wrong: RR often has higher average waiting and turnaround than SJF, but better response time.

    ✅ Verified Truth: RR is best for response time and fairness, not minimum waiting time.

    ⚠️ Exam Tip: Match the algorithm to the metric in the question.

    ❌ Myth: Waiting time is the same as response time.

    Why this is wrong: Waiting time counts all time in the ready queue; response time counts only until the first run.

    ✅ Verified Truth: They are equal only for non-preemptive CPU-only processes.

    ⚠️ Exam Tip: Use the formulae RT = first start - AT, WT = TAT - BT.

    Revision notes

    The CPU scheduler picks which ready process runs next. This chapter defines the criteria (waiting, turnaround, response time) and works through FCFS, SJF/SRTF, Priority, Round Robin and multilevel queues with Gantt-chart numericals in GATE style.

    High-Yield Quick Revision Points

    1. TAT = CT - AT; WT = TAT - BT; RT = first start - AT
    2. FCFS: convoy effect; SJF/SRTF: optimal avg WT but starvation
    3. Priority: starvation fixed by aging; inversion fixed by inheritance
    4. RR: (n-1)q max wait; large q → FCFS, tiny q → overhead
    5. MLFQ: demote CPU hogs, keep I/O-bound jobs high
    6. Always draw the Gantt chart and mark idle gaps

    Practice questions

    Question 1 · MCQ (GATE)

    Which scheduling algorithm gives minimum average waiting time for a given set of processes (all known in advance)?

    Question 2 · MCQ (GATE)

    The convoy effect is most associated with:

    Question 3 · MCQ (GATE)

    Starvation in priority scheduling is solved by:

    Question 4 · MCQ (GATE)

    In Round Robin with n processes in the ready queue and quantum q, the maximum time a process waits before getting the CPU again is:

    Question 5 · MCQ (GATE)

    A process has AT = 4, BT = 6 and completes at time 20. Its waiting time is:

    Solved questions from this chapter

    Real doubts students asked — full working and final answer, some with a video.