4.2.40 · D4 · HinglishOperating Systems

ExercisesVirtualization — type 1 and type 2 hypervisors

4,373 words20 min read↑ Read in English

4.2.40 · D4 · Coding › Operating Systems › Virtualization — type 1 and type 2 hypervisors

Shuru karne se pehle, ek picture poore page ko ek saath tie karti hai — layer stack. Neeche jo bhi grade karte hain woh sab actually ek hi sawaal hai: guest aur silicon ke beech kitne boxes baithe hain. Abhi ise study karo; almost har solution isi par point karta hai.

Figure — Virtualization — type 1 and type 2 hypervisors

Level 1 — Recognition

Recall Solution L1.1

ESXi ne machine ko khud boot kiya — yeh metal par base software hai. Uske neeche koi general-purpose host OS nahi hai. Answer: Type 1 (bare-metal). ESXi metal par baitha hai. Figure mein yeh left (Type 1) stack hai: Hypervisor box seedha Hardware (metal) box par baitha hai, beech mein koi Host OS box nahi hai. Guest ke neeche down-arrows count karo: guest → ESXi → hardware = 1 crossing.

Recall Solution L1.2

Path: Ubuntu guest → VirtualBox hypervisor → Windows 11 host OS → CPU. Yeh figure mein right (Type 2) stack hai — Host OS box present hai. Guest ke neeche do down-arrows hain, toh 2 crossings (hypervisor + host OS). Answer: Type 2 (hosted), 2 layers.

Recall Solution L1.3
  • Type 1 (bare-metal, left stack — koi Host OS box nahi): Xen, Hyper-V, VMware ESXi.
  • Type 2 (hosted, right stack — ek Host OS box hai): VirtualBox, VMware Workstation, Parallels. Pehchaan: Type 1 names woh cheezein hain jo tum machine ka base banane ke liye install karte ho; Type 2 names woh hain jinhe tum desktop OS ke andar double-click karte ho.

Level 2 — Application

Recall Solution L2.1

KVM ek kernel module hai jo running Linux kernel ko hi hypervisor mein badal deta hai. Linux kernel dono hai — bare-metal OS bhi aur VMM bhi — hypervisor aur metal ke beech koi alag host-OS application layer nahi hai. Figure terms mein, Hypervisor box aur (jo hota) Host OS box ek box mein fuse ho jaate hain jo metal par baitha hai, jisse left (Type 1) stack ki shape milti hai. Toh guest seedha ek privileged hypervisor se baat karta hai. Answer: effectively Type 1 (the classic "hybrid"). Exam-safe phrasing: "Type 1 hybrid — the kernel is the hypervisor."

Recall Solution L2.2

Guest B ke I/O path mein ek extra host-OS hop hai — figure ke right stack mein woh extra Host OS down-arrow: B → hypervisor → Windows → hardware, jabki A ka path hai A → Hyper-V → hardware. Ek kam down-arrow = ek kam crossing = kam per-request latency, aur I/O woh jagah hai jahan woh overhead concentrate hota hai. Answer: Guest A (Hyper-V, Type 1) faster hai.

Recall Solution L2.3

Woh poora laptop dedicate nahi kar sakte, aur unhe raw speed se zyada convenience chahiye. Answer: Type 2 (hosted) — e.g. VirtualBox ya VMware Workstation Player. Yeh Windows ke upar kisi bhi app ki tarah install hota hai (figure mein Host OS box bana rehta hai) aur uske saath coexist karta hai. Yeh exactly dev/test use case hai.


Level 3 — Analysis

Recall Solution L3.1

Har down-arrow add karta hai; figure mein guest ke neeche down-arrows count karo. Kyun percent-extra aur raw gap nahi? Raw gap apne aap mein meaningless hai — ek op ke next huge hai lekin ek op ke next negligible. The fraction normalise karta hai extra cost ko us kaam ke against jo tum anyway pay karne wale the, toh yeh physically meaningful sawaal ka jawab deta hai: "Type 1 ke time ki per unit, Type 2 kitna zyada charge karta hai?" — ek scale-free number jo tum workloads ke across compare kar sakte ho. Type 2 is modelled op par ≈ 16.7% slower hai. Extra 0.2 μs literally figure mein us ek extra Host OS down-arrow ki price hai — poori story yehi hai.

Recall Solution L3.2

Yahan exactly guest ke neeche down-arrows count karta hai — wahi crossings jo humne page ke upar define ki thi. Ek arrow per crossing, toh crossings cost karte hain : Type 2 () ka extra fraction Type 1 () par:

=\frac{\delta}{t_{hw}+\delta}$$ Jab $\delta \to \infty$, $f \to 1$, yaani **100\%**. Physically: jab ek arrow cross karna actual hardware work se far zyada cost kare *Hardware* box mein, toh woh ek extra *Host OS* arrow *double* kar deta hai time ko — Type 2 twice as slow ho jaata hai. **Isliye I/O-heavy, crossing-dominated workloads Type 2 ko sabse zyada punish karte hain** (I/O poore stack par bahut saare trips force karta hai neeche aur wapas upar).
Recall Solution L3.3

Hypervisor (trap-and-emulate): Pure emulator: Ratio . Ek hypervisor yahan ≈ 334× faster hai pure emulator se, kyunki woh 99.9% safe instructions ko seedha metal par run karta hai aur sirf rare privileged ones ko trap karta hai. Yehi "trap-and-emulate" ka pura point hai.


Level 4 — Synthesis

Recall Solution L4.1

(a) Kya break hota hai: guest kernel ko ek less-privileged ring mein demote kiya jaata hai (dekho CPU Privilege Rings). POPF sensitive hai (woh quietly interrupt flag read/write karta hai) lekin privileged nahi, toh woh trap nahi karta — koi up-arrow fire nahi hota; woh bas quietly wrong kaam karta hai. Yeh Popek & Goldberg rule "har sensitive instruction privileged honi chahiye" violate karta hai, toh hypervisor ko kabhi up-arrow intercept karne ko nahi milta, aur guest ka machine state ka view corrupt ho jaata hai. (b) Teen fixes:

  • Binary translationhypervisor bad instruction sequences ko on the fly rewrite karta hai (early VMware). Kaun change karta hai: hypervisor ka execution engine.
  • Paravirtualizationguest OS ko modify kiya jaata hai taaki hypervisor ko explicitly "hypercalls" ke zariye call kare problematic instruction run karne ki jagah (Xen). Kaun change karta hai: guest.
  • Hardware-assisted (Intel VT-x / AMD-V)CPU ek real guest mode add karta hai taaki nasty instructions bhi cleanly trap karein. Kaun change karta hai: silicon. Yeh modern default hai.
Recall Solution L4.2

(i) Cloud provider → Type 1 (e.g. VMware ESXi, Xen, ya KVM). Sabse strong reason: performance + smaller security surface — hot path par koi host-OS down-arrow nahi (left stack), aur ek thin hypervisor ek chhota trusted computing base hai defend karne ke liye hazaron tenants ke across. Dekho Cloud Computing. (ii) Solo developer → Type 2 (e.g. VirtualBox ya VMware Workstation). Sabse strong reason: convenience — developer ka laptop already ek primary desktop OS run kar raha hai jise woh wipe nahi kar sakte, aur Type 2 hypervisor uske upar ek ordinary app ki tarah install hota hai (figure mein right stack mein Host OS box bana rehta hai). VT-x/AMD-V ke under CPU-bound testing near-native speed par run hoti hai, toh single added down-arrow se thoda extra I/O overhead — dev/test ke liye irrelevant hai. Teen guest OSes ko seconds mein snapshot aur reset kar paana, apne familiar desktop se baahar gaye bina, yahan raw throughput se kahin zyada matter karta hai.

Recall Solution L4.3

Containers. Ek VM poora guest OS per instance ship karta hai (ek Guest OS box aur uska apna kernel) — 200 kernels ka RAM aur boot cost. Containers single host kernel share karte hain aur sirf user space ko isolate karte hain, toh 200 unke far lighter hain. Kyunki sabhi services same kernel accept karte hain, isolation trade-off acceptable hai. Dekho Containers vs Virtual Machines. (Agar services ko different kernels ya stronger isolation chahiye hoti, VMs jeette.)


Level 5 — Mastery

Recall Solution L5.1

Type 1 ka Type 2 par per-op saving hai (exactly ek extra arrow). ops par, Type 1 seconds runtime bachata hai, lekin Type 2 ne seconds setup bachaye the. Break-even woh jagah hai jahan runtime saving setup saving ke equal ho: Interpretation: 3 billion privileged ops se neeche, Type 2 ki ek-baar ki 600 s setup saving dominate karti hai → bas Type 2 use karo. Usse upar, Type 1 ki per-op speed setup cost wapas pay karti hai → bare-metal jao. Yeh "Type 2 for dev, Type 1 for production" ka quantitative heart hai.

Recall Solution L5.2

Lower bound . Numerator hai assumption se. Denominator hai , jo aur ka sum hai, toh woh hai. Ek non-negative number ek positive number se divide karne par milta hai. Hence . ∎

Upper bound . Gap consider karo

= \frac{t_{hw}}{t_{hw}+\delta}.$$ Numerator $t_{hw}>0$ aur denominator $t_{hw}+\delta>0$ hai, toh $1-f(\delta) > 0$ hai, yaani $f(\delta) < 1$. ∎ Combine karke: $0 \le f(\delta) < 1$ sabhi $\delta\ge 0,\ t_{hw}>0$ ke liye. Kyunki $f<1$ ka matlab hai extra time $T_1$ ke $100\%$ se kam hai, **Type 2 is model mein kabhi Type 1 ke time ka twice se zyada nahi hota**. **Do limiting cases:** - $\delta \to 0$ (free crossings): $f \to 0$ — Type 1 aur Type 2 **tie** karte hain. Yeh CPU-bound kaam model karta hai VT-x/AMD-V ke under jahan ek guest instruction barely arrows cross karti hai aur near-native run hoti hai. - $\delta \to \infty$ (crossing hardware work ko dwarf kare): $f \to 1$ — Type 2 **twice** time approach karta hai (lekin kabhi reach nahi karta). Yeh pathological I/O model karta hai jahan har request extra *Host OS* down-arrow pay karta hai. Yeh dono har real case ko bracket karte hain.
Recall Solution L5.3

CPU mein privilege rings hote hain (CPU Privilege Rings); hypervisor sabse privileged mode mein baitha hai aur har guest kernel ko usse neeche demote karta hai. Jab ek demoted guest ek privileged instruction run karta hai, CPU trap up karta hai (System Calls and Traps) hypervisor mein, jo effect emulate karta hai — yeh "trap-and-emulate" woh shared engine hai jo dono Type 1 aur Type 2 ke peeche hai. Fark sirf layer count hai (figure mein down-arrows) hardware tak jaane ke raaste par: Type 1 ka guest hardware tak pohnchne ke liye ek down-arrow cross karta hai, jabki Type 2 ka guest do cross karta hai (hypervisor + host OS), toh Type 2 mein har privileged trap aur har I/O ek extra hop pay karta hai — jo exactly woh overhead hai jo humne upar quantify kiya.


Recall Ek-line self-test jaane se pehle

Kisi bhi setup ke baare mein poochho: "Metal ne kya boot kiya, aur ek guest CPU tak pohnchne ke liye kitne down-arrows cross karta hai?" Ek down-arrow aur koi host OS nahi → Type 1. Do down-arrows, neeche ek full host OS ke saath → Type 2.