Exercises — I - O management — polling, interrupt-driven, DMA
4.2.37 · D4· Coding › Operating Systems › I - O management — polling, interrupt-driven, DMA
Level 1 — Recognition
Recall Solution
KYA: Woh status register read karta hai. KYU: Status = "device condition" (busy / ready / error). Data register mein bytes hote hain; command register mein "START READ" jaise orders hote hain. Sirf status register "ready?" ka jawab deta hai. Kya kuch badalta hai? Nahi — status read karna ek pure query hai; yeh device ko koi command nahi deta. (Contrast: command register mein likhna actually action cause karta hai.)
Recall Solution
- (a) → DMA (block-at-a-time, completion pe ek interrupt).
- (b) → Polling (status register pe busy-wait).
- (c) → Interrupt-driven (device ek interrupt line raise karta hai). YE KYU SWAP NAHI HAIN: distinguishing question yeh hai ki kaun status dekhta hai aur kaun bytes move karta hai. Polling: CPU dekhta bhi hai + move bhi karta hai. Interrupt: device signal karta hai, CPU move karta hai. DMA: controller dekhta bhi hai + move bhi karta hai.
Recall Solution
Polling < Interrupt-driven < DMA. KYU: Polling CPU ko wait bhi karwata hai aur copy bhi → sabse zyada CPU work. Interrupts waiting hata dete hain lekin per-byte copy rakhte hain. DMA dono — waiting aur copying — hata deta hai → sabse kam CPU work. Har step "CPU se aur zyada kaam remove karta hai."
Level 2 — Application
Recall Solution
KYA: Wasted polls . YE FORMULA KYU: CPU poll loop back-to-back run karta hai; time mein woh iterations fit karta hai, aur aakhri wale se pehle har ek ne "not ready" paya → wasted. Plug in (same units mein convert karo): , . Ek ready event ke liye yeh 80,000 iterations pure waste hain.
Recall Solution
KYA: overhead . KYU: interrupt-driven I/O har data unit pe ek baar pay karta hai, aur units hain. Note karo yeh ke saath linearly badhta hai — yahi woh weakness hai jo DMA fix karta hai.
Recall Solution
KYA: DMA exactly 1 interrupt raise karta hai (completion pe, jab count = 0 ho jaye). KYU: DMA controller har byte autonomously handle karta hai; CPU ko sirf "block done" bataya jaata hai. Overhead units (plus ek choti si fixed setup). L2·Q2 ke 20,480 units se compare karo: DMA ne interrupt overhead ke factor se cut kar diya.
Level 3 — Analysis

Recall Solution
KYA: crossover wahan hai jahan wait na karne se CPU time ki bachat interrupt overhead se zyada ho jaaye: . KYU: agar device almost instantly ready ho ( tiny), toh wait ek interrupt ki fixed cost se sasta hai → poll karo. Jaise hi , se exceed kare, freed CPU time overhead se zyada ho jaata hai → interrupt. Crossover: . Toh ke liye → interrupt-driven wins; (almost-ready device) ke liye → polling wins. Figure mein blue (polling) aur orange (interrupt) lines ka crossing dekho.
Recall Solution
KYA: interrupts per second . ISRs mein time per second . YE POINT KYU HAI: CPU ko sirf interrupts service karne ke liye 200% of its time chahiye hoga — physically impossible. ISR overhead dominate kar jaata hai. Conclusion: high data rates par, per-byte interrupts collapse ho jaate hain. Yahi exactly DMA kyun exist karta hai — poora block move karo, ek baar interrupt karo.
Recall Solution
Yeh claim galat hai. DMA aur CPU memory bus share karte hain. Jab bhi DMA controller ek word move karne ke liye bus grab karta hai, CPU us cycle mein memory use nahi kar sakta aur stall ho sakta hai — ise cycle stealing kehte hain. Jo SACH HAI: DMA CPU ko byte-copying loop se hata deta hai, toh uska load se ho jaata hai. Yeh ek badi bachat hai — lekin contention-free nahi, kyunki dono masters ek bus ke liye compete karte hain (bus arbitration se resolve hota hai). Sachchi baat: "CPU copying se free hai, lekin occasional bus stalls se nahi."
Level 4 — Synthesis
Recall Solution
Polling: moves wasted polls units. (CPU poore time stuck rehta hai.) Interrupt-driven: ISRs units of overhead. (Per-byte interrupts bade par hurt karte hain.) DMA: setup ISR units, CPU meanwhile free. Rank (best = least CPU work): DMA Polling Interrupt-driven. YE SURPRISE KYU? Bade blocks ke liye, per-byte interrupts yahan actually worst hain kyunki (50) per-byte poll savings — DMA ka block-at-a-time model decisive win hai. Yeh parent ka "forecast then verify" confirm hua. ✅
Recall Solution
(a) Keyboard → Interrupt-driven. Rare events (bahut bada ) ⇒ polling millions of cycles waste karta hai; sirf 1 byte ⇒ DMA setup worth it nahi. Interrupt = "rare ding pe mujhe jagao." (b) NIC → DMA. Huge blocks + high rate ⇒ per-byte interrupts collapse ho jaate hain (L3·Q2); ek completion interrupt ke saath block transfer hi ek sane choice hai. (c) Always-ready sensor → Polling. kuch ns hai ⇒ interrupt overhead poll cost se zyada ho jaayega. Seedha read kar lo. Unifying rule: technique ko device ki rate (kitni baar) aur block size (kitna zyada) se match karo. Koi ek method sab jagah nahi jeetta.
Level 5 — Mastery
Recall Solution
(a) Poll vs interrupt. Polling CPU ko poori wait ke liye tie up karta hai; interrupts ek fixed cost karte hain aur CPU ko free karte hain. Poll tabhi karo jab expected wait interrupt overhead se chhoti ho: ( par dono tie karte hain; usse neeche, ~ polls spin karna ek interrupt se sasta hai.)
(b) DMA switch karo jab transfer ek block ho ( bada) aur per-byte interrupt cost CPU ke time budget se exceed kare — matlab jab bhi ek fast device par kuch bytes se zyada ho. DMA cost ko ki jagah bana deta hai.
(c) Pseudocode:
handle_io(request):
if request.is_block and request.N > SMALL_THRESHOLD:
setup_DMA(src, dst, N) # one completion interrupt
elif expected_Tdev < t_int: # 5 microseconds
poll_status_until_ready() # busy-wait, cheaper here
else:
issue_command(); return # interrupt-driven; ISR finishes it
Defence: har branch wahan choose ki gayi hai jahan uska cost model provably lowest hai — near-instant devices ke liye polling, slow single-unit events ke liye interrupts, large blocks ke liye DMA. Yeh parent ka "P < I < D offloading" ek actual, defensible policy mein convert hua. Interrupts minimize karna kyun zaroori hai, iske liye CPU utilization and throughput aur Context switching dekho (har interrupt ek context save/restore cost karta hai).
Recall Solution
KYA: solve karo . KYU: crossover dhundo jahan DMA ki constant cost interrupts ki linear cost se neeche aa jaaye. Toh bytes ke liye, DMA CPU work mein already jeet jaata hai. Interpretation: chote blocks ke liye bhi break-even chota hai — DMA ka behaviour almost immediately dominate karta hai jaise hi transfer do-teen bytes se zyada ho.
Recall Self-test checklist (apne aap ko grade karne ke liye reveal karo)
Kya tumne har numeric answer sahi paya? ::: L2: 80000; 20480; 40. L3: crossover 2 μs; 2.0 s (200%). L4: 14096 / 204800 / ~100. L5: poll if under 5 μs; DMA wins for N ≥ 3; break-even N = 2.2. Kya tumne har choice ko rate AUR block size dono se justify kiya? ::: Agar nahi, toh L4·Q2 dobara padho. Kya tum driver policy memory se likh sakte ho? ::: Agar nahi, toh L5·Q1 pseudocode dobara padho.