Kakayou
Kakayou

Reputation: 736

What does the OS do when you run java bytecode? How it interact with the JVM?

I have been studying Operating systems in my free time, but I am confused about how it works with Java & the JVM.

Some questions when running the java bytecode using a command like java file.class:

  1. I understand that JVM optimize and interpret or perform JIT of the program
  2. How does the JVM get CPU allocation for doing these in a multi-threaded application?

My assumption: In each thread of this application, they all use the same per-process JVM to perform these tasks. (Is this correct?)

What is the role of the operating system with the JVM, what interaction do they have?

Upvotes: 3

Views: 1188

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338201

Image showing stack of User layer accessing Application layer built on top of Operating System layer built on top of Hardware layer.

(image from Wikipedia)

JVM is just an app

The Java Virtual Machine (JVM) as seen in the OpenJDK project is just another app, usually written in C and C++, sometimes written in Java.

From the point of view of the host operating system, running a JVM is just like running a word-processor, a spreadsheet, or a web browser. All of these apps are large, consuming much memory and spawning threads.

As someone commented, a JVM technically is any software and/or hardware that complies with the official specifications. Indeed, there have been attempts at building hardware chips that knew how to execute Java butecode (see Jazelle and others) but they did not succeed afaik. In practice today, the JVMs we download from Oracle or AdoptOpenJDK or other distributors are simply C/C++ apps that run like any other app on your Mac, BSD, Linux, Windows, AIX, Solaris, or similar machine.

I understand that JVM optimize and interpret or perform JIT of the program

HyperCard from Apple is vintage software similar to Java in that it too executed code internally through an interpreter with a JIT so that repeated runs of the same code blocks would suddenly run faster. HyperCard too was just another app from the point of view of the Mac operating system.

How does the JVM get CPU allocation for doing these in a multi-threaded application?

By scheduling threads on CPU cores like any other app. Word-processors use threads for writing to storage in the background and for re-rendering the document on the background. Web browsers might allocate threads for handling each web page in separate windows/tabs.

In each thread of this application, they all use the same per-process JVM to perform these tasks. (Is this correct?)

Yes, with OpenJDK, you will see one process on your OS for the JVM. All the threads of all the Java apps running within that JVM are housed within that single OS process. However, as someone commented, these are mere implementation details. People are free to implement a JVM as they see fit, in any manner they choose, as long as they comply with the Java specifications.

See the source code

OpenJDK is open-source. So if you are really curious, peruse that source code. Note that you will find areas of code specific to each OS, such as macOS versus Linux versus MS Windows, on each CPU type, such as x86 or ARM or SPARC or such, where the JVM interacts with the host OS.

Upvotes: 4

Related Questions