How Do Virtual Machines Really Work?


With the rise of virtual machines and codes that can be run anywhere, having a working knowledge of tools like JVM is crucial irrespective of whether one is a coder or not. Dmytro Vezhnin, CEO and co-founder at CodeGym, delves into the details of virtual machines and their workings in this two-part explainer. Here in part one, he discusses what a virtual machine is and how Java works behind the scenes.

Students often come across such an argument in favor of the Java programming language as support for the “write once, run anywhere” principle. This means that code written for any system can be executed on any other system. It is possible because of the Java Virtual Machine (JVM). Let’s look at the basics of how the JVM works. Whether you’re a beginner programmer, or have been writing code for a long time but are not too sure of how the Java platform actually works, staying up to date with the latest virtual machine technology is a good step forward.

First Experience with a VM 

In early youth, the author of these lines was deeply into retro games. I got some old consoles and collected games for them. Once I received a CD with Final Fantasy 9 for PlayStation as a Birthday gift from my friend. The problem was that I didn’t have such a console. The disc looked similar to those inserted into a regular computer drive, but my operating system couldn’t read it: the format didn’t match. The disk just spun in the drive, and my puzzled OS didn’t know what to do with it. I got on the Internet, and for the first time in my life, I learned about virtual machines, programs that simulate a particular device, computer, console, or phone, on the principle of a black box. That is, it works differently than the original, but having received something as input, it will output the same output as the Hardware original. I downloaded the PlayStation emulator, an actual virtual machine! Well, it was an exciting experience. I had to set up memory slots, connect a real disk drive to a virtual Playstation, and prescribe some paths. I don’t remember everything, but I fiddled with it for a long time. However, after a while, I was able to start it, and my drive, which previously had no idea what kind of strange disk I had inserted into it, suddenly started working! I cried out joyfully when I saw the game intro and finally set out on a long adventure. 

What Is a Virtual Machine?

This is the essence of virtual machines – creating a virtual isolated computer (or several) inside a real one. Insulation is essential. Later at work, I got to know Virtualbox and VMWare. I worked on Windows but wanted to learn Linux. Of course, I could just install a second operating system and download the one I need right now. However, this approach has drawbacks. Too often, rebooting from one system to another is not very convenient.

But if you use a virtual system and put Linux there, it will be much more convenient. Especially the fact that “everything that happens in Vegas stays in Vegas” will not go further than virtual reality. Installed Linux works as if on a separate computer. For Windows, such a virtual system will look like one extensive program that you can minimize, close, or switch from it to another program at any time.

The convenience is that you can create and run many such virtual machines simultaneously, if, of course, there are enough resources. Or you can run them one by one. For example, inside Windows, you can create one virtual machine for a test installation of Ubuntu and install CentOS and software for testing server applications in another.

There are several types of virtual machines. The two main types of virtual machines are system virtual machines and process virtual machines. What we have described above is the system virtual machine. It is designed to simulate the entire computer system, including the operation of a complete OS. System VMs are typically used to run programs on an OS that is not present on the host machine or to run multiple instances of virtual machines to allocate hardware resources efficiently. The operating system on the system virtual machine is isolated from the host machine.

Unlike system virtual machines, process virtual machines are created specifically to run a single application. When this process is not in use, the virtual machine is also not in use – it is “destroyed” when the process or application terminates. An example of a process virtual machine is the Java Virtual Machine (JVM), which allows any system to run Java applications as if they were native to that system.

How Does a Hypervisor Work?

The question arises: how exactly does a virtual machine convert a piece of actual hardware into a virtual one for use in a virtual environment. This is what the hypervisor does. Often, a hypervisor is a special program that, after being launched, “takes” part of the memory, processor power, and so on and assembles them into a separate virtual computer.

The task of the hypervisor is to ensure that the guest system works only inside the allocated resources and does not get into the host system. It also monitors the health of the guest system and reports if something goes wrong. It’s about running an operating system inside an operating system. Sometimes such a hypervisor allows you to access disk sectors or processor registers directly, but this is not always the case.

A hardware hypervisor works differently: instead of running on a host system, it becomes the host itself. In fact, a hardware hypervisor is such a mini-operating system, the only task of which is to run any other operating systems on top of itself and give them more freedom in accessing the hardware. For the hardware hypervisor to work, the processor and motherboard must support virtualization at the hardware level. This means that they should contain special commands that will help the hypervisor properly distribute processor power and delimit access to the memory of different systems.

Virtual Systems: Uses and Applications

Virtual systems’ main task is making the most of the hardware. So you can find hypervisors:

  • In servers on the Internet to get virtual servers, where you can, for example, make your own hosting
  • In-game video servers – this is how cloud gaming works
  • For software testing. For example, to test Android apps, developers usually use emulators of different cell phones
  • In home computers for simultaneous operation on several operating systems or testing new system features
  • To test viruses and trojans in encapsulated environments.

Application and Process Virtualization

In 1990, Sun Microsystems started the Stealth project, which was run by engineers who didn’t want to use the C and C++ APIs. They were convinced there was a better way to write and run applications. The project was renamed several times, called, for example, Oak. Well, in 1995, during the release, this project became known as Java.

This was stimulated by the advent of the Internet, a network in which computers of different architectures and operating systems must work. Of course, a platform that allows you to run the same applications on all these machines without having to rewrite and recompile them is a great idea.

Java was unique in its kind. The program written on it could be run on any computer with the free Java Run-Time Environment (JRE) installed. That is what set it apart and we’ll discuss this in greater detail in the next segment of this explainer.

Let us know your thoughts on virtual machines on FacebookOpens a new window , TwitterOpens a new window , and LinkedInOpens a new window and stay tuned for part two of this explainer!