4.2.12 · HinglishOperating Systems

Scheduling on multiprocessors — load balancing, affinity

2,323 words11 min readRead in English

4.2.12 · Coding › Operating Systems


WHY karta hai single-CPU scheduling fail?


Do core ideas


Queue design: ek global vs. ek per CPU

Figure — Scheduling on multiprocessors — load balancing, affinity

HOW decide karte hain migrate karna hai ya nahi? (Rule derive karo)

Hum koi formula yaad nahi karte — hum migration decision ko costs se build karte hain.


Worked examples


Common mistakes (steel-manned)


Active recall

Recall Quick self-test (expand se pehle answer karo)
  • Multiprocessor scheduling mein kaun se do goals conflict karte hain? → load balancing vs. cache affinity.
  • Costs se derive kiya hua migrate-or-stay inequality? → migrate iff (bachaayi gayi wait > migration+cache cost).
  • Push vs pull migration? → push: periodic task busy CPUs se tasks offload karta hai; pull: idle CPU busy CPU ke queue se kaam grab karta hai.
  • Imbalance par threshold kyun? → migration thrashing / cold caches se bachne ke liye.
  • SQMS ki weakness, MQMS ki weakness? → SQMS: lock contention + koi affinity nahi; MQMS: queues drift karte hain, balancing chahiye.
Recall Feynman: ek 12-saal ke bachche ko explain karo

Imagine karo ek shop mein 4 checkout lines (CPUs) hain. Har shopper (task) ka saaman already ek specific lane ke paas rakha hua hai (yeh cache hai — unka data wahan "warm" hai). Agar ek lane mein badi bheed hai aur doosri khaali hai, toh aap logon ko khaali lane mein bhej sakte ho — lekin unhe apna sara saaman carry karna padega, jisme time lagta hai. Toh rule simple hai: kisi ko nayi lane mein tabhi bhejo jab purani lane mein unka wait nayi lane tak jaane ki jhanjhat se zyada ho. Yahi hai load balancing (khaali lanes mein bhejo) aur affinity (saaman wahan warm hai jahan ho) ka aapas mein ladna.


Connections

  • CPU Scheduling Basics — single-CPU algorithms jinhe yeh generalize karta hai (RR, priority).
  • Cache Memory and Locality — affinity kyun matter karta hai (hot vs cold cache, working set).
  • NUMA Architectures — affinity memory-node locality tak extend hoti hai, sirf cache tak nahi.
  • Linux CFS Scheduler — real MQMS + periodic balancing (sched_domains, imbalance_pct).
  • Spinlocks and Lock Contention — SQMS ke single queue lock ke scale na karne ki wajah.
  • Context Switching Cost — migration cost , switch + cache reload par build karta hai.
Multiprocessor scheduling mein do competing goals kaun se hain?
Load balancing (sab CPUs equally busy rakho) vs. cache/processor affinity (task ko usi CPU par rakho taaki uska data cache mein hot rahe).
Processor affinity define karo, soft vs hard.
Affinity = preference ki task us CPU par rahe jis par last chala tha, cache warmth ke liye. Soft: scheduler koshish karta hai lekin migrate kar sakta hai. Hard: program ek CPU set force karta hai (jaise sched_setaffinity); scheduler ko maanna padta hai.
Costs se derive kiya hua migrate-or-stay rule kya hai?
Migrate iff W > M, jahan W = busy CPU par bachaayi gayi wait time aur M = migration cost including cold-cache reload penalty.
Push vs pull migration?
Push: ek periodic task loads check karta hai aur overloaded CPUs se tasks push karta hai. Pull: ek idle CPU busy CPU ke queue se ek waiting task pull karta hai.
SQMS — kya hai aur iske drawbacks?
Single-Queue Multiprocessor Scheduling: ek global ready queue. Drawbacks: lock contention (poor scaling) aur koi affinity nahi.
MQMS — kya hai aur iska drawback?
Multi-Queue: har CPU ka ek ready queue; scalable + affinity-friendly lekin queues balance se drift ho jaate hain, explicit load balancing chahiye.
Imbalance Δ = 0 tak balance kyun nahi karte?
Isse migration thrashing hoti hai — tasks CPUs ke beech ping-pong karte hain, caches cold rehte hain; balance sirf threshold ke baad karo (jaise imbalance_pct ≈ 125%).
Pull migration mein sirf waiting (abhi nahi chal rahe) tasks kyun steal karte hain?
Jo task chal raha hai woh apne CPU par cache-hot hai; ek waiting task pull karna lost cache warmth minimize karta hai.
Balancing mein busy CPU ko fully khaali karne ki jagah mean ki taraf migrate kyun karte hain?
Overshoot karne se sirf reverse imbalance aur extra migrations hoti hain; mean load ki taraf move karna total migration cost minimize karta hai.
Ek single global queue (SQMS) bahut saare CPUs par scale kyun nahi karta?
Sab CPUs ek queue lock ke liye contend karte hain, scheduler serialize ho jaata hai — parallel speedup ke bilkul ulta.

Concept Map

creates problem

creates problem

creates problem

motivates

motivates

keeps task on

types

moves work via

destroys

solved by

solved by

good balance but

needs

used by

migrate only if

migrate only if

Multiprocessor Scheduling

Cold Cache Cost

Lock Contention

Idle CPUs Waste

Affinity

Load Balancing

Same CPU hot cache

Soft vs Hard

Push and Pull Migration

MQMS per-CPU queues

SQMS global queue

Linux CFS FreeBSD ULE

Imbalance cost gt Reload cost