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 Level | Description |
|---|---|
| 0-15 | Background and low‑priority work |
| 16-23 | Normal application threads |
| 24-30 | Real‑time work |
| 31 | Time‑critical, highest pre‑emptive priority |
Scheduling Algorithms
Windows employs a hybrid, pre‑emptive, priority‑based scheduler that combines:
- Round‑Robin for threads at the same priority level.
- Priority Boosting for I/O and GUI responsiveness.
- Multicore Load Balancing to distribute work across CPUs.
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);