Scheduling

Overview

Scheduling in the Windows kernel determines which thread runs on a processor at any given time. It balances responsiveness, fairness, and efficiency across system workloads, taking into account thread priority, affinity, and CPU load.

Thread Priorities

Each thread has a priority ranging from 0 (Idle) to 31 (Time Critical). The scheduler prefers higher‑priority threads but also incorporates quantum and aging mechanisms.

Priority LevelDescription
0-15Background and low‑priority work
16-23Normal application threads
24-30Real‑time work
31Time‑critical, highest pre‑emptive priority

Scheduling Algorithms

Windows employs a hybrid, pre‑emptive, priority‑based scheduler that combines:

Processor Affinity

Affinities constrain a thread to run on specific logical processors. This can optimize cache usage and reduce cross‑CPU migration.

SetThreadAffinityMask(hThread, 0x0000000F); // restrict to CPUs 0‑3

Quantum & Timeslices

The scheduler assigns a time quantum (in milliseconds) based on thread priority. Higher‑priority threads receive longer quanta.

// Example: retrieving the quantum for the current thread
DWORD quantum = GetThreadTimeslice(hThread);

Related Topics