Linux Scheduler — Policies and CPU Affinity
The scheduler chooses runnable tasks for available CPUs subject to scheduling class, priority, affinity and resource controls. A blocked task is not competing for CPU until it becomes runnable. Normal scheduling policies, real-time policies and deadline scheduling have different semantics; internal fair-scheduler algorithms evolve across kernel releases. Nice values influence normal-task weights; they do not reserve CPU bandwidth, and their effects interact with grouping and competing workloads. Affinity limits eligible CPUs, not exclusive ownership. Inspect a workload's policy, allowed CPU mask and runnable/blocked state before interpreting load or latency. The example queries the caller thread's policy and nonempty affinity mask; it does not change priorities, set affinity, launch CPU load or claim to measure scheduler fairness. Example prerequisites: Linux, Bash and Python 3 standard library; run as a non-root user in a private temporary working directory. Example scope: native read-only observation. Expected output: own scheduling policy and affinity inspected. No system configuration changes.
Formal Syntax & Structure
sched_getscheduler(0); sched_getaffinity(0)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| policy | integer enum | Scheduling policy of the specified thread. |
| affinity | CPU set | CPUs on which that thread may execute. |
| nice | integer | Relative priority input for normal scheduling policies. |
Return Value
Queries return a policy and CPU set or raise an OS error. Example normalizes their successful inspection.
Working Example
python3 - <<'PY'import ospolicy=os.sched_getscheduler(0)allowed=os.sched_getaffinity(0)assert isinstance(policy,int) and policy>=0assert allowed and all(isinstance(cpu,int) and cpu>=0 for cpu in allowed)print('own scheduling policy and affinity inspected')PY