为什么 RTOS 只用 c 语言编码?
RTOS有必要一直用C语言编写吗?为什么不能用 java 或其他技术进行编码..?是因为java中没有指针概念吗?
Is it necessary to code RTOS in C language always? Why can't that be coded in java or some other technology..?? Is that because of the absence of pointer concept in java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
垃圾收集是阻碍 Java 实时化的重要原因。 JIT 是另一个问题,但它是可以克服的。
但总的来说,C 是有效的可移植汇编,可提供非常可预测的运行时性能,这对于可靠的实时执行至关重要。
Garbage collection is the big reason against Java being Real Time. JIT is another, but it could be overcome.
In general though, C being effectively portable assembly gives very predicable run-time performance, and that's essential for reliable real-time execution.
实时系统也可以用其他语言进行编程。例如,Java 有一个 Java RTS 系统。
与其他答案相反,实时垃圾收集有合理的工作量。但是,这些不会捆绑在您的典型发行版中。
令人担忧的是,其他语言通常具有难以实现确定性和可靠性的功能,例如传统的垃圾收集、JIT、运行时优化等。
Real time systems can be programmed in other languages too. Java has a Java RTS System for example.
Contrary to other answers, there is reasonable amount of work on real-time garbage collections. However, these don't get bundled in your typical distributions.
The concern is that other languages usually have features that make determinism and reliability hard to achieve, e.g traditional garbage collection, JIT, run-time optimizations, etc.
最初,RTOS 不仅仅用 C 语言编码。它们还可以用其他语言编码。然而,用于 RTOS 的语言需要提供确定性行为。这意味着特定操作的延迟必须始终低于特定的时间量。这排除了例如垃圾收集,在大多数实现中,垃圾收集将在不确定的时间内停止所有线程的执行。
At first RTOS aren't just coded in C. They can also be coded in other languages. However the language used for an RTOS needs to offer deterministic behaviour. This means that the latency for a specific action must always be under a specific amount of time. This rules out for example garbage collection, which in most implementations will stop the execution of all threads for an undetermined time.
因为 RTOS 开发人员很可能对 C++ 不够了解。
嵌入式系统中的 C++:神话与现实
在嵌入式设计中使用 C++ 替代 C 的指南
Because RTOS developers, most likely, don't know C++ well enough.
C++ in Embedded Systems: Myth and Reality
Guidelines for using C++ as an alternative to C in embedded designs
包括非常低级别的优化
在c代码中。
有用的低级系统工具
因此可以很容易地合并。
include very low level optimizations
in c-code.
useful low-level system tools which
hence can easily be incorporated.
根据定义,RTOS 必须支持确定性调度和执行。一般来说,低中断延迟和直接硬件访问也是一个理想的因素。 Java 中使用的垃圾收集、JIT 编译和字节码执行等技术使这些目标难以实现。
Java 可用于实时系统,但通常它运行在 RTOS 上,而不是在其实现中使用。
尽管如此,认为 RTOS 总是用 C 实现的说法同样是不正确的。任何系统级语言都适合,包括汇编语言。在大多数情况下,至少内核的某些部分将在汇编程序中。 C++ 将是一种合适的语言(很明显,因为它本质上是 C 的超集),许多商业 RTOS 无论如何都有 C++ 包装器;我习惯性地为 RTOS 开发 C++ 抽象层以支持可移植性。
通常使用 C 的另一个原因是,C(通常是 C/C++)编译器通常是第一个且通常是唯一可用于新体系结构的语言(汇编程序除外)(如今通常以 GNU 编译器实现的形式) )。因此,如果您希望能够将 RTOS 移植到最广泛的平台,那么使用最普遍的语言是有意义的。
By definition an RTOS must support deterministic scheduling and execution. Generally low interrupt latency, and direct hardware access are also a desirable factor. Technologies used in Java such as garbage collection, JIT compilation, and bytecode execution make these goals hard to achieve.
Java may be used in real-time systems, but typically it runs on an RTOS rather than being used in its implementation.
All that said, it would equally be untrue to suggest that RTOS are always implemented in C. Any systems level language would be suitable including assembler. In most cases at least some part of the kernel would be in assembler in any case. C++ would be a suitable language (rather obviously since it is essentially a C superset), many commercial RTOSs have C++ wrappers in any case; I habitually develop C++ abstraction layers for RTOS to support portability.
The other reason C is typically used is because a C (often a C/C++) compiler is generally the first and often the only language (other than assembler) available for a new architecture (frequently these days in the form of a GNU compiler implementation). So if you want to be able to port your RTOS to the widest number of platforms, it makes sense to use the most ubiquitous language.
不是“必要的”,但更实用
作为一种可以使用 Java 的语言,并且实际发生了各种奇怪的情况。
但一些边缘案例和演示实际上更像是“证明规则的例外”。
一般来说,Java 是一个大型复杂系统,旨在用于业务逻辑而不是操作系统内核。
如果我们还没有 C,Java 可能会朝不同的方向或多个方向发展。
但我们确实有 C,它对于操作系统内核来说几乎是完美的,但对于业务逻辑来说却是一个相当大的挑战。
对于内核来说,Java 和 C 一样好,这与应用程序方面 C 和 Java 一样好,都是现实的。经验,减去一些边缘例子,压倒性地证明了每种语言的优点。
Not "necessary", but a lot more practical
As a language Java could be used, and there are various wacky cases of it actually happening.
But a few fringe cases and demonstrations are really more "the exception(s) that prove the rule".
In general, Java is a big elaborate system intended for business logic and not OS kernels.
If we did not already have C, Java might have developed in a different direction, or in multiple directions.
But we do have C, which is nearly perfect for an OS kernel and quite a challenge for business logic.
Arguments that Java is just as good as C for a kernel are about as realistic as arguments that C is just as good as Java for applications. Experience, minus a few fringe examples, proves overwhelmingly what each language is good for.
我认为java为此目的最大的问题是自动垃圾收集。这是关于用 Java 创建实时系统的链接。
I think the biggest problem with java for this purpose is the automatic garbage collection. Here's a link on creating realtime systems in java.
因为基于 C 的 RTOS 众所周知并且已经使用了几十年。在许多特定情况下,它们的行为是可预测的,您可以找到许多使用这些系统进行开发的专家。
据我所知,基于 Java 的 RTOS 还没有达到能够让制作安全关键型实时应用程序的公司采用它的水平。
从技术上讲,基于 Java 的 RTOS 是没有争议的,但该主题的研究、工程和产品尚不成熟。
Because C-based RTOS are well known and have been used for many decades. Their behaviour is predictable for many specific situations and you can find many experts for developing with these systems.
I know no Java-based RTOS having reached a level such that a company making safety critical realtime applications would adopt it.
Technically, there is no argument against a Java-based RTOS, but research, engineering and products on the subject is not yet mature.
不。您还可以使用汇编器、Ada 和其他一些语言来编写 RTOS。
不可以。代码执行时间不可预测。
No. You can code RTOS also in assembler, Ada and few other.
No. Unpredictable time of code execution.
Java中有Real Time,但它需要操作系统的支持。
请参阅: http://java.sun.com/javase/technologies/realtime/索引.jsp
There's Real Time in Java, but it requires support from the OS.
See: http://java.sun.com/javase/technologies/realtime/index.jsp
C 是为编写操作系统而设计的,因此常见的措辞是“便携式汇编程序”,因此可以预期它用于此目的。
如果您想要实时 Java,Sun 有一个商业产品。
C was designed for writing operating systems, hence the common wording "portable assembler", so it is to be expected that it is used for that purpose.
If you want to have real time Java, Sun has a commercial offering.
如果有的话,那是因为指针。在 Java 中,除了基本数据类型之外的所有数据都在堆上分配,并且任何不同于
int
的变量都是指针。这不是编写操作系统的好方法,因为它对大多数操作强加了一层间接性,而在操作系统编写中,这一层可能会杀了你。操作系统内核是您需要优化和高性能的地方,因为您不知道将在其上运行什么。对于实时操作系统尤其如此,其中半毫秒的延迟可能至关重要。这需要与 CPU 和其他硬件真正融洽,并且能够编写高度微优化的代码,以高度可预测性执行特定的操作。
因此,C 是构建 RTOS 内核的非常好的工具,而 Java 则不是。这并不意味着您不能使用 Java 来做到这一点,但它会更难,而且可能不会那么成功。
我很好奇你为什么问这个问题。如果您使用的是 RTOS,那么它是用什么编写的并不重要。如果您想破解一个 RTOS,那么它是用什么编写的确实很重要,但是操作系统的概念和实现本身就足够困难了学习一门新语言是微不足道的。 (此外,如果您研究操作系统设计和实现,您几乎肯定会发现您使用的资源将使用 C 作为教学语言。)
If anything, it's because of pointers. In Java, everything except fundamental data types is allocated on the heap, and any variable that isn't something like
int
is a pointer. That's not a good way to write an operating system, because it imposes one layer of indirection over most operations, and in OS writing that layer can kill you.The OS kernel is a place where you want optimization and high performance, since you don't know what will run on it. This is particularly true of real-time OSs, in which a half-millisecond delay can be critical. This requires getting really chummy with the CPU and other hardware, and the ability to write highly microoptimized code that will execute specific things with great predictability.
For this reason, C is a very good tool to build a RTOS kernel with, and Java isn't. That doesn't mean you couldn't do it with Java, but it would be harder and probably not as successful.
I am curious as to why you ask the question. If you're using a RTOS, it doesn't really matter what it was written in. If you want to hack one, it does matter what it was written in, but the concepts and implementation of the OS are sufficiently hard in themselves that learning a new language is trivial to learn. (Moreover, if you study OS design and implementation, you're almost certain to find that the resources you use will use C as a teaching language.)
RTOS 并不总是用 C 编写。通常是这样,但在 ThreadX 中我相信它们使用汇编。
An RTOS isn't always written in C. Usually it is so, but in ThreadX I believe they use assembly.
不会。例如,有些 RTOS 是用 Lisp 或 Smalltalk 编写的。
是什么让你认为不能?
不,这是因为有一个神话,即操作系统只能用 C 语言编写。这个神话可以被证明是错误的,但仍然拒绝消亡。
这个神话如此普遍,以至于那些想要编写新操作系统的人都不敢尝试 C 以外的任何东西。
No. There are RTOS written in Lisp or Smalltalk, for example.
What makes you think it can't?
No, it's because there is a myth that Operating Systems can only be written in C. A myth that can be trivially proven false, yet still refuses to die.
This myth is so pervasive, that people who want to write a new OS, are simply too afraid to try anything other than C.
像 Java 这样的垃圾收集语言非常不适合实时编程。其原因应该是显而易见的。
A garbage-collected language like Java is highly unsuited for real-time programming. The reasons for this should be obvious.