Operating Systems

CPU Scheduling Algorithms; Threads

C-CAT

CPU Scheduling Algorithms

17.1 FCFS — First-Come, First-Served

  • Non-preemptive
  • Processes scheduled in arrival order
  • Simple but can cause Convoy Effect (short jobs wait behind long jobs)

Example:

Process  Burst Time  Arrival Time
P1        24ms        0
P2         3ms        0
P3         3ms        0

Gantt Chart:    | P1 (0-24) | P2 (24-27) | P3 (27-30) |
Waiting Times:  P1=0, P2=24, P3=27
Average Waiting: (0+24+27)/3 = 17ms

17.2 SJF — Shortest Job First

  • Non-preemptive (or preemptive = SRTF)
  • Schedule process with shortest burst time next
  • Optimal for minimizing average waiting time
  • Problem: Requires knowing burst time in advance

SRTF (Shortest Remaining Time First): Preemptive version of SJF

Example:

Process  Burst Time  Arrival Time
P1        6ms        0
P2         8ms        0
P3         7ms        0
P4         3ms        0

Order: P4(3ms) → P1(6ms) → P3(7ms) → P2(8ms)
Average Waiting = (3+16+9+0)/4 = 7ms   (vs FCFS: 10.25ms)

17.3 Round Robin (RR)

  • Preemptive
  • Each process gets a fixed time quantum (time slice) — usually 10-100ms
  • After time quantum expires, process is preempted and moved to back of ready queue
  • Most common in time-sharing systems

Example (q=4ms):

Process  Burst Time
P1        24ms
P2         3ms
P3         3ms

Gantt: |P1(0-4)|P2(4-7)|P3(7-10)|P1(10-14)|P1(14-18)|P1(18-22)|P1(22-26)|P1(26-30)|

Tradeoff: Large quantum → behaves like FCFS; Small quantum → more context switches

17.4 Priority Scheduling

  • Each process has a priority number (lower number = higher priority)
  • Highest priority process runs first
  • Preemptive or Non-preemptive
  • Problem: Starvation — low priority processes may never run

Solution: Aging — gradually increase priority of waiting processes

17.5 Multilevel Queue Scheduling

Multiple queues for different process types:

  • Foreground (interactive) — Round Robin

Background (batch) — FCFS

  • Different queues have different priorities

17.6 Multilevel Feedback Queue

  • Processes can move between queues based on behavior
  • CPU-bound processes → lower priority queue
  • I/O-bound processes → higher priority queue
  • Most complex but most flexible

Algorithm Comparison

AlgorithmPreemptiveAvg WaitStarvationOverhead
FCFSNoHighNoLow
SJFNoOptimalYes (long jobs)Medium
SRTFYesOptimalYesHigh
Round RobinYesMediumNoMedium
PriorityBothMediumYes (low priority)Low
Multilevel FeedbackYesGoodNo (with aging)High

Threads

What is a Thread?

A thread is the smallest unit of CPU execution — a lightweight process.

Process vs Thread:

FeatureProcessThread
DefinitionProgram in executionLightweight sub-unit of a process
MemoryOwn address spaceShares process address space
Creation costExpensiveCheap
CommunicationIPC requiredShared memory directly
Context switchHeavyLight
FailureIsolatedCan crash other threads

Types of Threads

TypeDescription
User-level threadsManaged by user-space library (not OS)
Kernel-level threadsManaged by OS kernel
Hybrid threadsCombination

Multi-threading Models

ModelDescriptionExample
Many-to-OneMany user threads → one kernel threadOld Green threads
One-to-OneEach user thread = one kernel threadWindows, Linux
Many-to-ManyMany user → many kernel threadsSolaris, HP-UX

Thread Benefits

  • Faster creation than processes
  • Faster context switching within same process

Shared memory enables easy data sharing

  • Enables true parallelism on multi-core CPUs
  • Responsive UI (one thread handles UI, another does background work)

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.