Skip to content

Operating System - Lecture 4: CPU Scheduling

Introduction

  • CPU scheduling is fundamental in multiprogrammed systems — it determines which process uses the CPU at any given time.
  • A process execution cycle alternates between:
    • CPU burst – when the process uses the CPU.
    • I/O burst – when the process waits for I/O operations.
  • The final CPU burst usually ends with a system call to terminate execution.

CPU Scheduling Overview

  • CPU Scheduling optimizes process execution order for better system performance.
  • The short-term scheduler selects a process from the ready queue and allocates the CPU to it.
  • Scheduling decisions occur when a process:
    1. Switches from running -> waiting
    2. Switches from running -> ready
    3. Switches from waiting -> ready
    4. Terminates

Scheduling Types

TypeDescription
PreemptiveCPU can interrupt and switch to another process before completion
Non-preemptiveProcess retains CPU until it releases it voluntarily

Preemption

  • Definition: Temporarily removing a process from the CPU in favor of another (e.g., higher priority or shorter job).
  • Used to improve responsiveness and ensure fair CPU distribution.

Non-Preemptive vs Preemptive Scheduling

AspectNon-PreemptivePreemptive
CPU AllocationProcess keeps CPU until completion or I/O waitCPU assigned for a limited time
InterruptProcess runs until it voluntarily releases CPUCan interrupt a process at anytime
OverheadLow (No overhead of switching mid-execution)High (due to context switching)
StarvationPossible if long jobs block short onesPossible if high-priority tasks frequently arrive
SynchronizationEasierComplex with shared data and kernel activities
FlexibilityNone; cannot interrupt current taskFlexible; can prioritize urgent processes
CPU UtilizationLowerHigher
Waiting TimeHigherLower
Response TimeSlowerFaster, but risk of race conditions
ExamplesFCFS, Shortest Job FirstRound Robin, Shortest Remaining Time First

Dispatcher

  • The dispatcher transfers CPU control to the selected process.
    Steps:
    1. Context switch
    2. Switch to user mode
    3. Jump to the user program's next instruction
  • Dispatch latency: the time required to stop one process and start another.

Scheduling Criteria

MetricDescriptionGoal
CPU UtilizationKeep CPU busyMaximize
ThroughputNumber of processes completed per unit timeMaximize
Turnaround TimeTotal time from submission to completionMinimize
Waiting TimeTime spent waiting in the ready queueMinimize
Response TimeTime from request submission until first responseMinimize

Scheduling Algorithm Formulas

  • Turnaround Time = Completion Time − Arrival Time
  • Waiting Time = Turnaround Time − Burst Time
  • Throughput = Total Processes / Total Completion Time

Scheduling Algorithms

  1. First-Come, First-Served (FCFS)
  2. Shortest Job First (SJF)
  3. Priority Scheduling
  4. Round Robin (RR)
  5. Multilevel Queue Scheduling
  6. Multilevel Feedback Queue Scheduling

First-Come, First-Served (FCFS)

  • Processes are executed in order of arrival (FIFO queue).
  • Process Control Block (PCB) added to queue tail upon arrival.
  • When CPU is free, process at the head gets executed.
  • Simple to implement but inefficient for varying burst times.

Example

ProcessBurst Time
P124
P23
P33

Order: P1 -> P2 -> P3
Average Waiting Time = (0 + 24 + 27)/3 = 17
Gantt Chart:

| P1 |------------------------| P2 |---| P3 |---|
0    24                       27   30

Reordered (P2, P3, P1): Average Waiting Time = 3

Drawback: Convoy effect – long processes delay short ones.

Advantages and Disadvantages

AdvantagesDisadvantages
Easy to implementHigh waiting time for short processes
Fair (no starvation)Poor CPU utilization

2. Shortest Job First (SJF)

Concept

  • Each process is associated with the length of its next CPU burst.
  • The process with the shortest burst time is scheduled next.
  • Optimal for minimizing average waiting time but requires burst prediction.

Burst Length Prediction

τn+1=αtn+(1α)τn

Where:

  • τn+1: predicted burst time for next cycle
  • tn: actual burst time for current cycle
  • α: smoothing factor (0<=α<=1), often 0.5

Advantages

  • Efficent and minimizes average waiting time.

Disadvantages

  • Difficult to predict burst length.
  • Starvation possible for longer processes.

3. Shortest Remaining Time First (SRTF)

  • Preemptive version of SJF.
  • Chooses process with the least remaining CPU burst time.

Advantages

Lower average waiting time than non-preemptive.

Disadvantages

  • Complex to estimate burst times.
  • Risk of starvation and excessive context switches.

4. Priority Scheduling

Concept

  • Each process has a priority number; lower numbers indicate higher priority.
  • The CPU is allocated to the highest-priority process.
  • Can be preemptive or non-preemptive.
  • SJF is a special case of priority scheduling where priority = 1 / (next CPU burst time).

Issues

  • Starvation: low-priority processes may never execute.
  • Solution: Aging – increase process priority the longer it waits.

Example

ProcessBurstPriority
P1103
P211
P324
P415
P552

Average Waiting Time ≈ 8.2 ms

Advantages

  • Flexibility based on importance

Disadvantages

  • Starvation of low-priority jobs
  • Complex to manage dynamically

5. Round Robin (RR)

Concept

  • Each process gets a fixed time quantum (q) (10–100 ms typical).
  • After the quantum expires, the process is preempted and placed at the end of the ready queue.
  • Suitable for time-sharing systems.

Example (q = 4)

ProcessBurst Time
P124
P23
P33

Order: P1 -> P2 -> P3 -> P1 -> …
Average Waiting Time = 3.6 ms

Formulas

  • Turnaround Time: End Time - Arrival Time
  • Waiting Time: Turnaround Time - Burst Time

Characteristics

  • Fairness: all processes get CPU time, no starvation.
  • Performance depends on quantum size:
    • Large q -> behaves like FCFS.
    • Small q -> high overhead due to frequent context switches.

Comparison of Scheduling Algorithms

  • Preemptive algorithms yield better performance, but risk starvation.
  • FCFS is simplest but performs poorly for interactive systems.
  • Round Robin ensures fairness and avoids starvation in time-shared systems, but depends heavily on quantum size.

Multilevel Queue Scheduling

  • Ready queue divided into multiple queues.
  • Each queue has its own scheduling algorithm:
    • Foreground (interactive) -> Round Robin
    • Background (batch) -> FCFS

Scheduling Between Queues

  • Fixed Priority: serve all foreground before background (risk of starvation).
  • Time Slice: allocate CPU time proportionally (e.g., 80% to foreground, 20% to background).

Multilevel Feedback Queue Scheduling

  • Processes can move between queues based on behavior and aging.

  • Implements aging to prevent starvation.

  • Parameters:

    • Number of queues
    • Scheduling per queue
    • Promotion/demotion policies
    • Queue entry rules

Example:

QueueAlgorithmTime Quantum
Q0Round Robin8 ms
Q1Round Robin16 ms
Q2FCFS

Evaluation of Scheduling Algorithms

Evaluation methods help determine the most effective algorithm per use case.

1. Deterministic Modeling

  • Evaluates performance for fixed workloads.
  • Simple, fast, but only valid for specific input values.

2. Queueing Models

  • Represents system as a network of queues (CPU, I/O).
  • Uses Little's Formula:n=λ×W
    • n: average number of processes in queue
    • λ: arrival rate
    • W: average waiting time

3. Simulation

  • Emulates system behavior using random input or real trace data.
  • Allows more accurate, flexible analysis.

4. Implementation

  • Most accurate but costly and environment-dependent.
  • Requires modifying and testing the OS itself.
  • Environment and workload variations affect results.