Part 3 of 8 · 3 min read
Virtualisation
Think of a theatre with one stage and many plays. The stage staff change the set so quickly between acts that each audience sees only its own play. The analogy stops in one place: a theatre needs minutes to change a set, and an OS switches in microseconds.
In plain words: The main trick of an OS is virtualisation. It takes a physical resource such as the processor, memory or a disk and turns it into a more general and easier-to-use virtual form. For this reason the OS is sometimes called a virtual machine.
The key lines.
A small program shows the effect. It spins for half a second, prints a label, and repeats three times:
#include <stdio.h>
#include <time.h>
static void spin(double seconds) {
struct timespec start, now;
clock_gettime(CLOCK_MONOTONIC, &start);
do {
clock_gettime(CLOCK_MONOTONIC, &now);
} while ((now.tv_sec - start.tv_sec) + (now.tv_nsec - start.tv_nsec) / 1e9 < seconds);
}
int main(int argc, char *argv[]) {
if (argc != 2) { fprintf(stderr, "usage: cpu <label>\n"); return 1; }
for (int i = 0; i < 3; i++) {
spin(0.5);
printf("%s ", argv[1]);
fflush(stdout);
}
return 0;
}
Compile and run it with gcc -o cpu cpu.c and ./cpu A. Output on the test machine, which has two processors:
A A A
Now start four copies at the same moment, each with its own label:
$ ./cpu A & ./cpu B & ./cpu C & ./cpu D & wait
B A C D A B C D A B D C
Four programs run at once on a machine with only two processors, and all finish in about the same 1.5 seconds. Every copy behaves as if it had a processor to itself. The OS, with help from the hardware, creates that illusion by switching the processors among the programs many times a second. The order of the labels differs on each run, because the OS decides who runs when. This is virtualising the CPU.
Memory works the same way. Each program sees its own private range of addresses, while the OS maps those onto the shared physical memory. A disk is virtualised into files and folders. Later levels take each one in turn.
A worked example.
A worked check (made up to show the idea). Each of the four programs would use the processor for 1.5 seconds if it ran alone, so 4 × 1.5 = 6 processor-seconds in total. Two processors over 1.5 seconds give only 2 × 1.5 = 3 processor-seconds. The programs spin on the clock, not on a count, so each one loses time while it waits for its turn. The operating system's time report showed about 3 seconds of user time for all four together, which matches 2 × 1.5.
Virtualisation
Virtualisation is the technique of taking one physical resource, such as a processor or a block of memory, and presenting it to programs as many separate virtual copies, each of which behaves as if it were a complete resource of its own.
Example: one processor presented as many virtual processors, so that four programs each seem to have their own.
Check yourself: 1. Why did the four copies finish in about the same time as one? 2. What does the OS virtualise besides the processor? 3. Why does the order of A, B, C and D change between runs?