OCR H446, section 1.2.1 (h)
What is a virtual machine?
A virtual machine (VM) is software that takes on the function of a machine. It imitates (emulates) a computer, or part of one, so that programs can run on it as if it were real hardware.
OCR describes this as any instance where software is used to take on the function of a machine. That includes two main cases:
- Executing intermediate code (such as bytecode) with a language virtual machine.
- Running an operating system inside another with a system virtual machine.
1. Executing intermediate code
Some languages are compiled not to machine code for one particular processor, but to intermediate code (such as bytecode), which is simple and is the same for every computer. A virtual machine then runs that bytecode.
- Java programs are compiled to bytecode and run on the Java Virtual Machine (JVM). Python also compiles to bytecode, which its own virtual machine runs.
- The VM interprets the bytecode (or translates it to machine code as it runs, called just-in-time compilation).
Why do this?
- Portability. The same bytecode runs on any computer that has the VM: "write once, run anywhere". Only the VM must be written for each platform.
- Security. The VM can check the code and limit what it is allowed to do, so a program is "sandboxed".
- Compact code, smaller than the source.
The cost is that it is usually slower than running native machine code, because of the extra layer.
2. Running an operating system within another
A system virtual machine is a complete simulated computer, with its own virtual processor, memory, disk and devices, inside which a whole operating system can be installed and run. The real computer is the host, and the virtual one is the guest.
A program called a hypervisor (or virtual machine monitor) creates and manages the VMs, and shares the real hardware's processor, memory and storage between them. Examples include VirtualBox, VMware and Hyper-V.
Why do this?
- Run different operating systems on one computer, for example Linux on a Windows PC, to use software for another platform.
- Testing and development: try software, or risky actions, safely. The guest can be reset, and nothing happens to the host.
- Isolation and security: malware in a VM is kept away from the host. Servers are separated from each other.
- Server consolidation: many virtual servers share one physical machine, which saves money, space and power. This is how cloud computing works.
- Running old software that needs an old operating system or hardware (legacy), through emulation.
- Easy backup and moving: a VM is a set of files that can be copied to another machine.
Disadvantages
- Slower: the guest shares the hardware, and there's an extra layer.
- It needs a lot of resources from the host: memory, storage and processor time.
- If the host fails, all the VMs on it fail.
- Management and licensing can be complicated.
Comparison
| Language VM (such as the JVM) | System VM (such as VirtualBox) | |
|---|---|---|
| Runs | Bytecode from one program | A whole operating system and its programs |
| Purpose | Portability of programs | Running other operating systems and isolation |
| Size | Small | Large (a full OS, with its files) |
| Example | A Java application | Windows running inside a Linux computer |
Worked example
Task: a software company wants to test its new program on Windows, macOS and Linux, but has only Windows computers. Suggest how VMs could help.
- Install a hypervisor on a Windows PC, and create virtual machines running Linux (and, where licences allow, other systems).
- Run the program inside each guest, and check that it works.
- If a test crashes or damages the system, reset the VM, which doesn't affect the real computer.
- If the program is in Java, the JVM also lets the same bytecode run on each system, with no separate version needed.
Exam tip: mention both meanings: a language VM that runs intermediate code (giving portability) and a system VM that runs an operating system inside another (giving isolation, testing and server consolidation). Give an advantage and a disadvantage, such as the performance cost.
Check yourself
What is a virtual machine?
Software that takes on the function of a machine, so that programs can run on it as if it were real hardware.
Why are programs compiled to bytecode and run on a virtual machine?
The same bytecode can run on any computer that has the virtual machine, which makes the program portable. The VM can also check the code for security.
Give two reasons for running an operating system inside a virtual machine
Any two of: running software for a different operating system; testing software safely; isolating risky programs; running many virtual servers on one computer; running old software.
Give one disadvantage of virtual machines
They are slower than running on the real hardware and use a lot of the host's memory, storage and processing power.