AlgoPlusAlgoPlus
Learn/Operating Systems
Lesson

Virtual Machines

Faking a whole computer in software so one physical machine can run several isolated operating systems.

8 min read Watch it move Build it

A virtual machine is an entire computer emulated in software — virtual CPU, memory, disk, and devices — convincing enough that a real operating system installs and runs inside it, never realising it isn't on bare hardware. The payoff is that one physical machine can run several complete, isolated operating systems at once.

The hypervisor

The software doing the faking is a hypervisor, also called a Virtual Machine Monitor (VMM). Each operating system it hosts is a guest OS. The classic trick is trap-and-emulate: a guest runs normally at full speed, but the instant it executes a *privileged* instruction (one that would touch real hardware), the CPU traps into the hypervisor, which emulates the effect on that guest's *virtual* hardware and returns. Guests stay walled off because none of them ever touches the real machine directly.

Type 1 vs Type 2

  1. 1Type 1 (bare-metal) — the hypervisor runs *directly on the hardware*, with no host OS beneath it. Thin and fast; this is what data centres run. Examples: VMware ESXi, Xen, Microsoft Hyper-V.
  2. 2Type 2 (hosted) — the hypervisor runs as an *ordinary application on top of a normal host OS*. Easy to install on a laptop, but a little slower because guest hardware access is relayed through the host. Examples: VirtualBox, VMware Workstation.
Type 1 (bare-metal)        Type 2 (hosted)
[Guest] [Guest]            [Guest] [Guest]
[   Hypervisor   ]        [   Hypervisor   ]
[    Hardware     ]        [    Host OS      ]
                          [    Hardware     ]
Isolation is the headline feature
Because each VM gets its own virtual CPU, memory, and devices, one guest can't read another's memory or crash it — a kernel panic inside one VM leaves its neighbours untouched. That isolation is why VMs power multi-tenant cloud servers.
VMs vs containers
A VM virtualizes the *hardware* and runs a full guest OS, so it's heavy but strongly isolated. A container (Docker) shares the host's *kernel* and only isolates the process view — far lighter and faster to start, but with a weaker isolation boundary. They solve overlapping problems at different costs.
OperationTimeSpace
Type 1 · data-center consolidationnear-native speedfull guest OS each
Type 2 · laptops, dev, testingslight overheadguest OS + host OS
Container · lighter; weaker isolationnative speedshares host kernel
Check yourself
What distinguishes a Type 1 hypervisor from a Type 2?