One shared queue
A multicore chip has several cores. Each core is a CPU that runs one job at a time. Here there are two. Each has its own small, fast cache, and both share one big, slow main memory.
The simplest plan: one queue for all cores, single-queue scheduling (SQMS). Jobs A, B and C wait in it. Each free core takes the front job, A to core 0, B to core 1, and loads its data. The counter tallies slices and reloads.
After one time slice, A and B go to the back of the queue. Both cores want the queue at once, so it has a lock. Core 0 takes the lock first and gets C; core 1 waits, then gets A.
A ran on core 0 but now runs on core 1, whose cache lacks A's data, so it reloads. Every job that runs again lands on the other core and reloads. Running again where its data is warm, called cache affinity, is lost.
A queue per core
Start again with one queue per core: multi-queue scheduling (MQMS). Four jobs arrive, and each goes into one queue: A and C to core 0, B and D to core 1. Each core picks from its own queue, so no shared lock.
Each core runs round robin on its own queue: A, C, A, C on core 0 and B, D, B, D on core 1. Each job loads its data once, then finds it still in its core's cache while it stays there.
Keeping the balance
C finished after slice 4. Now core 0 has only A, while B and D still share core 1. A gets a whole core, twice as much CPU as B or D. Each queue only sees its own jobs.
A finished after slice 6. Core 0's queue is empty, so core 0 sits idle while B and D still take turns on core 1. Half the machine does nothing while work waits.
Core 0 peeks at queue 1, sees two jobs against its none, and steals D: a migration. D reloads its data once on core 0, then runs warm. All the work is done after slice 9, not 12.
Linux keeps a run queue for every CPU. On a regular tick each CPU checks the balance and pulls tasks from the busiest one. A program can also pin a thread to chosen cores with sched_setaffinity().
One shared queue balances load but needs a lock and loses cache affinity. A queue per core scales and keeps caches warm, but drifts out of balance, so it moves jobs now and then, not too often.
What happens
;; step 1/11 · cores core runs one job at a time cache small and fast, one per core memory big and slow, shared by every core a value already in the cache: a few nanoseconds a value fetched from memory: tens to hundreds of ns (OSTEP ch. 10) new question for the scheduler: not only who runs next and for how long, but also on WHICH core
The figure plays a slice-by-slice simulation. Taking the lock, switching jobs and reloading data are drawn as free; on a real machine each costs time, which is why affinity and fewer locks matter. Each cache is drawn holding the data of the last two jobs that ran on its core. A real cache holds small lines of data from many jobs, so a moved job loses much, not all, of its warm data. Hardware cache coherence keeps every copy correct either way: moving costs speed, never correctness.