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?
-
7to be honest most OS are written in C RT or not – jk. Dec 18 '09 at 10:25
-
They are not. See IBM Metronome: http://domino.research.ibm.com/comm/research_projects.nsf/pages/metronome.index.html – Mehrdad Afshari Dec 21 '09 at 22:39
-
1@jk: And there's reasons: C is very well suited to write OS kernels with. – David Thornley Dec 21 '09 at 22:42
16 Answers
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.

- 252
- 1
- 5
-
5Java does have an RTS system, and there is some good research and work on real-time garbage collection. – notnoop Dec 18 '09 at 09:44
-
@TheManWithNoName: Whats do you mean by Garbage collection? And whats JIT? – wrapperm Dec 18 '09 at 09:44
-
3JIT is the "Just-In-TIme compiler" which massively changes the runtime chracteristics of code http://en.wikipedia.org/wiki/Just-in-time_compilation Garbage Collection is the automatic memory-sweeping that Java implements. – Federico Giorgi Dec 18 '09 at 09:53
-
2Garbage collection is automatic freeing of unused objects - this normally has the potential to pause the running application (the pause can be reduced but is very difficult to fully eliminate, without risking out of memory scenarios). JIT is the Just In Time compiler which converts heavily used java bytecode into native code as needed - this obviously dramatically changes the performance but itself requires time to perform, and you may need to force complete compilation to meet the real time goals to begin with. – TheManWithNoName Dec 18 '09 at 09:58
-
Realtime garbage collectors have existed for decades, now. And JIT is in no way required for Java. In fact, in pretty much every single Java implementation in existence, there is no JIT compiler. Often, there is one in the JVM implementation, but a) a JVM is not required for Java and b) a JIT isn't required for the JVM, either. – Jörg W Mittag Dec 18 '09 at 19:38
Because RTOS developers, most likely, don't know C++ well enough.
C++ in Embedded Systems: Myth and Reality
Some perceive that C++ has overheads and costs that render it somehow unsuited to embedded systems programming, that it lacks the control and brevity of C, or that, while it may be suited to some niche applications, it will never displace C as the language of choice for embedded systems.
These perceptions are wrong. Where compilers and other tools are adequate, C++ is always preferable to C as an implementation language for embedded systems. While doing everything that C does, it offers greater opportunities for expression, encapsulation, re-use, and it even allows size and speed improvements that are impractical in C.
> Why, then, do these perceptions persist? The main reason is that when people form their opinions, they know a lot more about C than about C++. They have read some books, written some code, and are competent at using the features of C++, but they lack the knowledge of what is happening under the hood, the familiarity that allows one to visualize the disassembly while typing source or even while formulating a design.
Guidelines for using C++ as an alternative to C in embedded designs
Embedded software applications are most commonly written in C. For many years, C++ has been seen as the natural successor and has found greater acceptance, but the increase in its usage has been much slower than anticipated.
There are a number of reasons for this. Firstly, embedded developers are quite conservative and prefer to use solutions that are proven rather than novel " "if it ain't broke, don't fix it".
There is also the lesson of experience. Many developers have attempted to use C++ for embedded applications and failed. Such failures may sometimes be attributed to shortcomings in development tools, but most often it is the inappropriate use of the language " treating an embedded system like a desktop computer " that is to blame.
Limitations of C Although C is widely used, it has limitations, as it was not designed for embedded applications or for projects of a scale that is now commonplace. Key limitations include:
1) C is extremely powerful and flexible and can therefore be dangerous.(It has low level capabilities - which are useful for embedded " but also represent many pitfalls for the unwary.)
2) Programmers need to be very methodical and disciplined
3) Programmers need to understand how the program behaves at low and high levels (large projects are thus hard to maintain)
4) Programmers need an expert knowledge of the application
However, C++ has powerful object oriented capabilities which can help significantly with addressing the limitations of C:
1) it encapsulates and hides areas of high expertise from non-experts into "objects;" (A test case will demonstrate the encapsulation of expertise later in Part 2 in this series).
2) Objects can be used intuitively by non-experts to implement conceptual designs at a high-level

- 8,077
- 2
- 68
- 66
-
1In my opinion, nobody knows C++ well enough, including those that say that they do. We had an expert C++ programmer create an array class (before boost) and quickly about 32 major errors were found in it by another expert. C is not very secure by itself, but it is better understood. There is a reason why Java was created as a more secure, but simpler C++ (with, obviously, its own drawbacks). – Maarten Bodewes Feb 03 '22 at 10:24
-
Your links don't work any more - check these out for the articles you mentioned: ["C++ in Embedded Systems: Myth and Reality"](https://forums.parallax.com/discussion/download/86053&d=1318932104) and its follow-up by the same author: ["Modern C++ in embedded systems"](https://www.embedded.com/modern-c-in-embedded-systems-part-1-myth-and-reality/) - and ["Guidelines for using C++ as an alternative to C in embedded designs"](https://www.embedded.com/guidelines-for-using-c-as-an-alternative-to-c-in-embedded-designs-part-1/) – HelpingHand Feb 22 '22 at 21:47
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.

- 58,763
- 21
- 123
- 144
-
3You'll note though that the Java RTS System has to run on top of a RTOS - no-one's built a realtime baremetal Java system yet. – caf Dec 18 '09 at 09:48
-
@notnoop: people have done it already, for example http://www.jnode.org/ and http://cjos.sourceforge.net/archive/ – viraptor Dec 18 '09 at 10:36
-
Ajile systems (www.ajile.com) make realtime Java CPU's. They run Java on bare metal hardware. Interrupt response latency is under 1 microsecond. Thread-to thread context switches take less than 1 microsecond. Power consumption is 100milliwatts maximum at 100% cpu. Performance is on par with a Pentium at 400Mhz. Companies that use them do not advertise it. For them,it is a competitive advantage. Finding people to do embedded Java real-time, now that's a bit harder. I like their hardware. It is fun to use and has solved real world problems for real companies and is in use around the world. – Tim Williscroft Dec 21 '09 at 22:05
-
The lastest jnode progress report is dated: 2008. Four years ago at time of this comment. Did anything develop from there? – will Apr 19 '12 at 14:24
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.

- 103,016
- 27
- 158
- 194
-
Gosh ... most of these used to be coded in FORTRAN and assembler. A C RTOS is like the 'evolved' option with a CD-player and bucket seats.
On INTeL hardware, quite a bit of real-time system work was written with PL/M, which you'd have to describe as a macro-assembler high-level language (3GL). – will Apr 19 '12 at 14:16
- Availability of highly optimized c-compilers for all hardware that RTOS-es typically run on.
- The relative ease with which you can include very low level optimizations in c-code.
- Availability of c-code for a lot of useful low-level system tools which hence can easily be incorporated.

- 7,060
- 3
- 26
- 50
-
1+1 for your second reason. In writing any sort of OS, you're going to have to get really up close and personal with the hardware now and then. Java was designed with the intention of glossing over the really low-level stuff, and it shows. – David Thornley Dec 21 '09 at 22:18
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.

- 143,651
- 25
- 248
- 329
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.

- 88,407
- 13
- 85
- 165
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.

- 51,293
- 14
- 84
- 114
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.

- 66,855
- 13
- 106
- 140
Is it necessary to code RTOS in C language always?
No. You can code RTOS also in assembler, Ada and few other.
Why can't that be coded in java or some other technology..?? Is that because of the absence of pointer concept in java?
No. Unpredictable time of code execution.

- 2,485
- 4
- 26
- 36
There's Real Time in Java, but it requires support from the OS. See: http://java.sun.com/javase/technologies/realtime/index.jsp

- 2,386
- 1
- 14
- 21
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.

- 73,784
- 33
- 194
- 347
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.)

- 56,304
- 9
- 91
- 158
An RTOS isn't always written in C. Usually it is so, but in ThreadX I believe they use assembly.

- 675
- 1
- 5
- 26

- 11
- 1
-
C will be compiled to assembly and you can even use inline assembly in most C compilers. – Paul Sep 29 '15 at 12:02
A garbage-collected language like Java is highly unsuited for real-time programming. The reasons for this should be obvious.

- 58,739
- 8
- 81
- 86
-
There is such a thing as Real-time garbage collection: http://www.ibm.com/developerworks/java/library/j-rtj4/index.html – notnoop Dec 18 '09 at 09:42
-
2
-
-
In the spirit of answering questions, perhaps it would be helpful to list the reasons. There are all levels of users and what is obvious to some may not be to others. – semaj Dec 18 '09 at 16:34
-
@semaj: But in this case, the answer *is* obvious: @Anon has clearly never heard of realtime garbage collectors. – Jörg W Mittag Dec 18 '09 at 19:35
-
A real-time garbage collector is going to require a fair amount of support, which means it's something a Java RTOS will need and a C RTOS won't. Really, though, garbage collection is largely irrelevant here. – David Thornley Dec 21 '09 at 22:41
Is it necessary to code RTOS in C language always?
No. There are RTOS written in Lisp or Smalltalk, for example.
Why can't that be coded in java or some other technology..??
What makes you think it can't?
Is that because of the absence of pointer concept in java?
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.

- 363,080
- 75
- 446
- 653
-
-
More to the point, where's the myth? What answers to this question claim that OSs can only be written in C? – David Thornley Dec 21 '09 at 22:21
-
@Jörg: This was not about writing an OS but writing an RTOS. The whole point with an RTOS is that it is deterministic. To have a truly deterministic java you need to remove quite a lot of the things that are good with java (even if you use Java RTS) and that would sort of defeat the purpose of using it. You can get pretty darn close if you code things right and use something like JRRT but you will not be as realtime as you need to be when writing a RTOS. At least not yet. – Fredrik Dec 21 '09 at 22:23
-
I suppose I should point out that the only widely used Lisp OSs I ever heard of were on specially designed hardware, and nobody ever told me they were real-time. Were there actual real-time OSs written in Lisp or Smalltalk? – David Thornley Dec 21 '09 at 22:40