SwadhyAI Notes

All Operating Systems notes

Start here

On Linux, scheduler decisions are per-thread (TID), not “per PID.” Nice values, RT priorities, and cgroup CPU limits attach to thread fields, misreading top without thread mode hides the real story.

Thread scheduling (conceptual)

Runnable threads sit on runqueues; the kernel picks next to run based on policy (CFS for SCHED_OTHER, priority queues for RT). Affinity masks limit which CPUs a thread may use; cgroup quotas throttle groups of threads.

By the end of this page you should be able to

  1. Explain why TID matters for per-thread CPU stats.
  2. Name one reason to set CPU affinity for a latency-sensitive worker.
  3. Relate cgroup `cpu.max` / quota to scheduling delays.

Operating systems · Threads

Thread scheduling

How kernel picks among runnable threads

TL;DR

Thread scheduling is the same scheduler as processes on Linux, tasks compete by priority class (CFS nice, RT bands). User runtimes may add layers (GOMAXPROCS) but ultimately map runnable goroutines onto kernel threads.

Summary

Thread scheduling questions often test whether you separate language-level tasks from kernel tasks and whether you understand priority inversion and cgroup throttles as scheduling-adjacent controls.

Deeper narrative

Kernel schedules threads, not “processes,” as runnable units; nice values and cgroups apply per-thread fields on Linux. User runtimes can pin worker pools to cores to reduce migration and cache loss.

Mechanism (step by step)

Treat each step as a sentence in the story you tell on a whiteboard.
1

Per-thread priority

SCHED_OTHER nice is per-thread; RT priorities likewise per-thread.

2

Affinity

`sched_setaffinity` masks which CPUs may run the thread.

3

cgroup cpu

Throttling uses cfs_quota_us per cgroup containing threads.

4

Work conservation

Idle CPU pulls highest vruntime runnable from shared or hierarchical runqueues.

Key insights

Each card is a compression of the lesson, use them as spaced-repetition prompts.

vruntime fairness

CFS approximates fair CPU share using virtual runtime accounting, nice adjusts weight, not time slices explicitly.

Runqueue migration

Load balancing pulls tasks across CPUs, can hurt cache warmth; tunables exist for HPC.

Kernel preemption

Kernel code can be preempted except in non-preemptible sections, impacts latency of syscalls.

User runtime schedulers

They decide which runnable task becomes a syscall-running thread, still subject to kernel decisions.

Did you know?

Trivia with teeth: the kind of fact that changes how you reason about a design.
Real-time SCHED_FIFO can starve normal tasks, requires CAP_SYS_NICE and careful design.
Control groups can cap CPU bandwidth even if threads are runnable, fairness at infra layer.
`sched_setaffinity` pins threads to cores for cache locality or noisy neighbor isolation.

Common pitfalls

Where smart people still trip, read slowly.

Assuming PID is sched unit

Use TID for per-thread stats; `top -H` toggles thread mode.

Spinning without yielding

Busy-wait burns a core; pair with `sched_yield` only as last resort.

Comparison

Linux policy families (simplified)

PolicyIntent
SCHED_OTHER (CFS)Fair proportional sharing
SCHED_FIFO/RRReal-time preemption bands
SCHED_IDLEOnly if nothing else runnable

Glossary

Nice value

Static priority bias in CFS, higher nice means less CPU share.

Affinity mask

Bitmask of CPUs a thread may execute on.

CFS quota

cgroup v2 bandwidth cap implemented via CFS slices.

Interview follow-ups

  1. How does CFS treat two threads of the same process vs two processes?
  2. What happens when RT threads starve kworkers?
  3. Why set cpu affinity for latency-sensitive threads?

Callouts

Perf

Off-CPU flame graphs reveal lock waits mistaken for “bad CPU scheduling.”

Walkthrough & deep dive

Left: practical “how would I use this?” threads. Right: slower, code-leaning explanations, open one panel at a time so the page does not feel like a wall of sameness.

Walkthrough

Scenario

A low-latency audio thread and a batch gzip on one cgroup, what settings prevent glitches?

Deep dive

Nice vs RT

Nice adjusts CFS weights; RT priorities are absolute preemption layers, mixing requires extreme care.

Affinity peek

Terminal
taskset -cp $

Self-check

Misses here are cheap; misses in an interview are not. Treat wrong answers as a patch list for tomorrow.

1CFS primarily targets…

2CPU cgroup throttling…

3SCHED_FIFO threads…