Why are operating systems not written in java?

All operating systems up to date are written in C / C ++, in Java there are none. There are tons of Java applications, but not the OS. What for?

+6
source share
5 answers

Because we already have operating systems. Java is not designed to work on bare metal, but it is not as strong as it might seem at first glance. Because C compilers provide built-in functions that are compiled for specific instructions, the Java compiler (or JIT, this distinction makes no sense in this context) can do the same. It would also be difficult to handle the interaction of the GC and the memory manager. But it can be done. The result is a kernel with 95% Java and ready-to-run jars. What's next?

Now it's time to write an operating system. Device drivers, file system, network stack, all other components that allow you to do something with the computer. The Java standard library usually relies on system calls to make a heavy lift, both because of this and because starting a computer is a pain in the ass. Writing a file, for example, includes the following layers (at least I'm not a guy from the OS, so I probably skipped the material):

  • The file system, which should find a place for the file, updates its directory structure, processes logging, and finally decides which disk blocks should be written and in which order.
  • The block level that must plan for simultaneous writing and reading in order to maximize throughput and maximize fairness.
  • A device driver that should keep the device happy and push it in the right place for everything to happen. And, of course, each device is broken in its own way, requiring its own driver.

And all this should work fine and remain executed with a dozen threads, accessing the disk, because the disk is essentially a huge bunch of shared mutable state.

In the end, you have Linux, except that it does not work, because it does not have almost as much effort as is invested in functionality and performance, and only Java works in it. Perhaps you get performance from one address space and without differentiating the kernel / user, but the gain is not worth the effort.

There is one place where language-specific OS makes sense: VM. Let the underlying OS handle the hard parts of the computer, and the tenant operating system process the virtual machine in runtime. BareMetal and MirageOS follow this model. Why would you do this instead of using Docker? That's a good question.

+7
source

Indeed, there is JavaOS http://en.wikipedia.org/wiki/JavaOS

And here it is discussed why there are not many OS written in java in java. Is it possible to make an operating system using java?

In short, Java must run on the JVM. JVM needs to run on the OS. writing an OS using Java is not a good choice.

The OS needs to deal with hardware that cannot be executed using java (other than JNI). And this is because the JVM has provided only limited commands that can be used in Java. These commands include adding, calling a method, etc. But to deal with the hardware need for direct management of the registry, memory, processor, hardware drivers. They are not supported directly in the JVM, so JNI is required. This is back to the beginning - you still need to write an OS using C / assembly.

Hope this helps.

+3
source

For operating systems, you need to work at the lowest level. And this is a pain in Java. You need, for example, unsigned data types, and Java only signs data types. You need struct objects that have exactly the memory alignment that the driver expects (and no object header, such as Java, is added to each object).

Even the key components of Java itself are no longer written in Java.

And this is in no way a temporary thing. More and more being rewritten in native code to improve performance. VMware HotSpot is adding โ€œintrinsicsโ€ for high-performance native code, and work is currently underway to reduce the overall cost of its own calls.

For example JavaFX : the reason it is much faster than AWT / Swing is because it contains / uses a huge amount of native code. It relies on native code for rendering, and for example, if you add the browser component โ€œwebviewโ€, it actually uses the webkit C library to provide the browser.

There are many things that Java is really good at. It is a well-structured language with a fantastic toolchain. Python is much more compact to write, but its toolchain is a mess, for example. refactoring tools are disappointing. And where Java shines is optimization of polymorphism at runtime. Where C ++ compilers will have to make expensive virtual calls, because at compile time it is not known which implementation will be used - there Hotspot can persistently embed code to improve performance. But for operating systems you do not need it. You can allow yourself to manually optimize call sites and embed them.

+3
source

This answer does not mean to be exhaustive in any way, but I would like to share my thoughts on a (very broad) topic.

Although it is theoretically possible to write some OS in pure java, there are practical questions that make this task very difficult. The main problem is that there is no (currently relevant and reliable) java compiler capable of compiling java byte code. Thus, there is no existing tool for writing an entire OS from scratch as much as possible in Java, at least as far as I know.

Java was designed to run in some implementation of the Java virtual machine. There are versions for Windows, Mac, Linux, Android, etc. The language design is heavily based on the assumption that the JVM exists and will do the magic for you at runtime (think about garbage collection, the JIT compiler, reflection, etc.). Most likely, this is part of the reason why such a compiler does not exist: where would all these functions go? Compiled to byte code? It is possible, but at the moment I believe that it will be difficult to do. Even Android, whose SDK is entirely Java-based, runs Dalvik (a version of the JVM that supports a subset of the language) in the Linux kernel.

+1
source

One of the main advantages of using Java is that it abstracts out many low-level details that you usually don't need. These are the details that are required when creating the OS. Therefore, although you could get around this to write an OS in Java, it would have many limitations and you would spend a lot of time struggling with the language and its original design principles.

+1
source

Source: https://habr.com/ru/post/984991/


All Articles