java 为什么操作系统不是用java编写的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29580170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:31:48  来源:igfitidea点击:

Why operating systems are not written in java?

java

提问by stillanoob

All the operating systems till date have been written in C/C++ while there is none in Java. There are tonnes of Java applications but not an OS. Why?

迄今为止,所有操作系统都是用 C/C++ 编写的,而 Java 则没有。有大量的 Java 应用程序,但没有一个操作系统。为什么?

回答by Steve McKay

Because we have operating systems already, mainly. Java isn't designed to run on bare metal, but that's not as big of a hurdle as it might seem at first. As C compilers provide intrinsic functions that compile to specific instructions, a Java compiler (or JIT, the distinction isn't meaningful in this context) could do the same thing. Handling the interaction of GC and the memory manager would be somewhat tricky also. But it could be done. The result is a kernel that's 95% Java and ready to run jars. What's next?

因为我们已经有了操作系统,主要是。Java 并不是为在裸机上运行而设计的,但这并没有乍一看的那么大。由于 C 编译器提供编译为特定指令的内在函数,因此 Java 编译器(或 JIT,在此上下文中的区别没有意义)可以做同样的事情。处理 GC 和内存管理器的交互也会有些棘手。但这是可以做到的。结果是一个 95% 是 Java 的内核,可以运行 jars。下一步是什么?

Now it's time to write an operating system. Device drivers, a filesystem, a network stack, all the other components that make it possible to do things with a computer. The Java standard library normally leans heavily on system calls to do the heavy lifting, both because it has to and because running a computer is a pain in the ass. Writing a file, for example, involves the following layers (at least, I'm not an OS guy so I've surely missed stuff):

现在是编写操作系统的时候了。设备驱动程序、文件系统、网络堆栈以及所有其他组件,这些组件使计算机可以执行操作。Java 标准库通常严重依赖系统调用来完成繁重的工作,因为它必须这样做,也因为运行计算机是一件很痛苦的事情。例如,编写文件涉及以下层(至少,我不是操作系统专家,所以我肯定错过了一些东西):

  1. The filesystem, which has to find space for the file, update its directory structure, handle journaling, and finally decide what disk blocks need to be written and in what order.
  2. The block layer, which has to schedule concurrent writes and reads to maximize throughput while maximizing fairness.
  3. The device driver, which has to keep the device happy and poke it in the right places to make things happen. And of course every device is broken in its own special way, requiring its own driver.
  1. 文件系统必须为文件找到空间,更新其目录结构,处理日志,并最终决定需要写入哪些磁盘块以及以何种顺序写入。
  2. 块层,它必须调度并发写入和读取,以在最大化公平性的同时最大化吞吐量。
  3. 设备驱动程序,它必须保持设备运行良好,并将其插入正确的位置以使事情发生。当然,每个设备都以自己的特殊方式损坏,需要自己的驱动程序。

And all this has to work fine and remain performant with a dozen threads accessing the disk, because a disk is essentially an enormous pile of shared mutable state.

所有这一切都必须正常工作并在十几个线程访问磁盘时保持高性能,因为磁盘本质上是一大堆共享的可变状态。

At the end, you've got Linux, except it doesn't work as well because it doesn't have near as much effort invested into functionality and performance, and it only runs Java. Possibly you gain performance from having a single address space and no kernel/userspace distinction, but the gain isn't worth the effort involved.

最后,您得到了 Linux,但它不能很好地工作,因为它没有在功能和性能上投入太多精力,而且它只运行 Java。可能您可以通过拥有单一地址空间而没有内核/用户空间区别来获得性能,但是所付出的努力并不值得。

There is one place where a language-specific OS makes sense: VMs. Let the underlying OS handle the hard parts of running a computer, and the tenant OS handles turning a VM into an execution environment. BareMetaland MirageOSfollow this model. Why would you bother doing this instead of using Docker? That's a good question.

有一个地方可以使用特定于语言的操作系统:VM。让底层操作系统处理运行计算机的困难部分,租户操作系统负责将 VM 转换为执行环境。BareMetalMirageOS遵循此模型。你为什么要费心这样做而不是使用 Docker?这是个好问题。

回答by DeepNightTwo

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

确实有一个 JavaOS http://en.wikipedia.org/wiki/JavaOS

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

这里讨论为什么没有很多用java编写的操作系统是否可以使用java制作操作系统?

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

简而言之,Java 需要运行在 JVM 上。JVM 需要在操作系统上运行。使用 Java 编写操作系统不是一个好的选择。

OS needs to deal with hardware which is not doable using java (except using JNI). And that is because JVM only provided limited commands which can be used in Java. These command including add, call a method and so on. But deal with hardware need command to operate reg, memory, CPU, hardware drivers directly. These are not supported directly in JVM so JNI is needed. That is back to the start - it is still needed to write an OS using C/assembly.

操作系统需要处理使用 java 无法实现的硬件(使用 JNI 除外)。那是因为 JVM 只提供了有限的可以在 Java 中使用的命令。这些命令包括添加、调用方法等。但是处理硬件需要命令直接操作reg、内存、CPU、硬件驱动。JVM 不直接支持这些,因此需要 JNI。这又回到了起点——仍然需要使用 C/汇编来编写操作系统。

Hope this helps.

希望这可以帮助。

回答by Has QUIT--Anony-Mousse

For operating systems you need to work really low-level. And that is a pain in Java. You do need e.g. unsigned data types, and Java only has signed data types. You need structobjects that have exactly the memory alignment the driver expects (and no object header like Java adds to every object).

对于操作系统,您需要在非常低的级别上工作。这在 Java 中很痛苦。您确实需要例如无符号数据类型,而Java 只有有符号数据类型。您需要struct具有完全符合驱动程序期望的内存对齐的对象(并且没有像 Java 这样的对象头添加到每个对象)。

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

甚至 Java 本身的关键组件也不再用 Java 编写。

And this is -by no means- a temporary thing. More and more does get rewritten in native code to get better performance. The HotSpot VM adds "intrinsics" for performance critical native code, and there is work underway to reduce the overall cost of native calls.

这绝不是暂时的事情。越来越多的代码被重写为原生代码以获得更好的性能。HotSpot VM 为性能关键的本机代码添加了“内部函数”,并且正在进行工作以降低本机调用的总体成本。

For example JavaFX: The reason why it is much faster than AWT/Swing ever were is because it contains/uses a huge amount of native code. It relies on native code for rendering, and e.g. if you add the "webview" browser component it is actually using the webkit C library to provide the browser.

例如JavaFX:它之所以比 AWT/Swing 快得多,是因为它包含/使用了大量的本机代码。它依赖于本地代码进行渲染,例如,如果您添加“webview”浏览器组件,它实际上是使用 webkit C 库来提供浏览器。

There is a number of things Java does really well. It is a nicely structured language with a fantastictoolchain. Python is much more compact to write, but its toolchain is a mess, e.g. refactoring tools are disappointing. And where Java shines is at optimizing polymorphism at run-time. Where C++ compilers would need to do expensive virtual calls - because at compile time it is not known which implementation will be used - there Hotspot can aggressively inline code to get better performance. But for operating systems, you do not need this much. You can afford to manually optimize call sites and inlining.

Java 在很多方面做得非常好。它是一种结构良好的语言,具有出色的工具链。Python 编写起来要紧凑得多,但它的工具链是一团糟,例如重构工具令人失望。Java 的亮点在于在运行时优化多态性。在 C++ 编译器需要执行昂贵的虚拟调用的地方——因为在编译时不知道将使用哪个实现——热点可以积极地内联代码以获得更好的性能。但是对于操作系统,您不需要这么多。您可以负担得起手动优化调用站点和内联。

回答by Oleksi

One of the main benefits of using Java is that abstracts away a lot of low level details that you usually don't really need to care about. It's those details which are required when you build an OS. So while you could work around this to write an OS in Java, it would have a lot of limitations, and you'd spend a lot of time fighting with the language and its initial design principles.

使用 Java 的主要好处之一是可以抽象出许多您通常并不真正需要关心的底层细节。这是构建操作系统时所需的那些细节。因此,虽然您可以解决这个问题,用 Java 编写操作系统,但它会有很多限制,而且您会花费大量时间与语言及其初始设计原则作斗争。

回答by Giovanni Botta

This answer does not mean to be exhaustive in any way, but I'd like to share my thoughts on the (very vast) topic.

这个答案并不意味着以任何方式详尽无遗,但我想分享我对(非常广泛的)主题的看法。

Although it is theoretically possible to write some OS in pure java, there are practical matters that make this task really difficult. The main problem is that there is no (currently up to date and reliable) java compiler able to compile java to byte code. So there is no existing tool to make writing a whole OS from the ground up feasible in java, at least as far as my knowledge goes.

虽然理论上可以用纯 Java 编写一些操作系统,但有一些实际问题使这项任务变得非常困难。主要问题是没有(目前是最新且可靠的)java 编译器能够将 java 编译为字节码。因此,至少就我的知识而言,没有现有的工具可以让 Java 从头开始​​编写整个操作系统是可行的。

Java was designed to run in some implementation of the java virtual machine. There exist implementations for Windows, Mac, Linux, Android, etc. The design of the language is strongly based on the assumption that the JVM exists and will do some magic for you at runtime (think garbage collection, JIT compiler, reflection, etc.). This is most likely part of the reason why such a compiler does not exist: where would all these functionality go? Compiled down to byte code? It's possible but at this point I believe it would be difficult to do. Even Android, whose SDK is purely java based, runs Dalvik (a version of the JVM that supports a subset of the language) on a Linux Kernel.

Java 旨在运行在 Java 虚拟机的某些实现中。存在适用于 Windows、Mac、Linux、Android 等的实现。该语言的设计强烈基于 JVM 存在的假设,并且会在运行时为您做一些魔术(想想垃圾收集、JIT 编译器、反射等。 )。这很可能是这种编译器不存在的部分原因:所有这些功能会去哪里?编译成字节码?这是可能的,但在这一点上,我认为这很难做到。即使 Android 的 SDK 完全基于 Java,也会在 Linux 内核上运行 Dalvik(支持该语言子集的 JVM 版本)。