I have heard rumors of a "Java OS". I've never tried it myself, but it made me curious. Is it possible to have a kernel written in Java? And if so, how would someone go about making one? Pointers to readings, tutorials, etc. would be greatly appreciated.
It isn't possible to write a whole operating system in Java---well, at least if that code is then translated into Java bytecode. The reason is simple: the operating system needs to communicate directly with the hardware in the system (CPU, RAM, mobo controllers, PCI cards etc.). But Java bytecode is a platform-neutral set of instructions, so isn't in a form directly compatible with the CPU, nor does it provide the ability to communicate with the low-level hardware directly. So ultimately you'd need some layer written in the CPU's instruction set to execute basic commands. Once you'd done that you could write a Java bytecode-to-assembly JIT interpreter or compiler for your architecture, and then simple Java instructions could be executed. That would lay the foundation for the rest of the OS.
The fundamental point here is that you need a JVM to run Java bytecode in. That JVM has to be written either for another operating system and let that OS take care of the low-level instructions (as most are at the moment), or written directly for a processor set. The latter would pretty much constitute a "Java OS", but you can't write the JVM in Java, or we'd be going round in circles! The jnode project is an example "Java OS", and it has modules written in x86 assembly to provide the necessary native code.
There are other concerns too of course: like driver compatibility, portability, development and support, provision of various APIs like graphics and networking---the existing JVMs use well-established OS libraries, and it's a lot of work to duplicate those. Ultimately you lose a lot of that by writing a new OS just for Java.
Of course, as a human you could write the entire OS in Java source code or bytecode, if you had a way to turn the result into assembly for your particularly processor (as per GCJ)... but then it's not really Java, is it?!
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
Performance of java as compared to c or fortan is not satisfactory. If OS is developed in Java then it will have serious performance issues. We can see the following chart to have an idea of the performance of java with other languages.
Originally posted by Himanshu Gupta: [QB]Performance of java as compared to c or fortan is not satisfactory. If OS is developed in Java then it will have serious performance issues. We can see the following chart to have an idea of the performance of java with other languages.
Interesting comparison, but a couple of points: (1) This benchmark doesn't show any huge disadvantage of recent JVMs (e.g. Java 5 Server VM) as it's almost as good as C/C++ with the huge added advantage of bytecode portability (if you need it); (2) does this really add anything to the discussion of a fundamentally Java OS? Surely an operating system built with Java in mind from the outset (rather than having to use JNI and to interface with other external OS code) would be optimised sufficiently that it is that slight bit faster than a JVM running on another OS? Just a thought.
I read somewhere that java as compared to C and assembly language is very slow. What I was thinking is that in OS there are a lot of calculations every second. Moreover i thought that using JNI means more code and thus more computations as what can be done using C/Assembly in less time has to be done following a process of byte code interacting with some interface which really does the same work.
I may be wrong but I just wrote what I have in my mind.
Joined: Mar 28, 2008
Thanks guys! So ultimately, though it's (sort of) possible to write an OS in Java, it may or may not be super practical. Correct?
Joined: Mar 27, 2003
I read somewhere that java as compared to C and assembly language is very slow.
You were probably reading an old report, or a set of benchmarks designed for high performance tasks---like numerical analysis, or DSP. There is another benchmark which shows Java 1.4 is faster than C++ for many non-numerical tasks. Assembly gets the best speed, if you have the patience to write and re-write it for each architecture. But with CPU speeds the way they are, there isn't much difference these days.
What I was thinking is that in OS there are a lot of calculations every second.
Not really---an OS doesn't do much computation at all, it just manages other processes and threads, re-draws any graphics like the desktop, I/O and networking, optimising file indexing etc. None of this is particularly CPU intensive: my Linux desktop sits at less than 2% system CPU, and my non-graphical servers mostly under 0.5%.
Moreover i thought that using JNI...
But the whole point of writing a Java OS is that JNI isn't really necessary---the OS provides the required native code built into the JVM (i.e. the OS) so there is no additional layer required. If you needed to use JNI in a Java OS, then the OS wouldn't really be written well in Java, would it?
Thanks Charles Lyons. Your reply updated my years long old information. [ January 01, 2009: Message edited by: Himanshu Gupta ]
Joined: Mar 27, 2003
As an exercise for the class - would the existing defined bytecodes be enough to execute all the functions needed for an OS or would some additional bytecodes be required?
That's an interesting question... I'd have to admit I don't know the answer immediately, having never had the inclination to read the bytecode spec.
Without further research I'd guess yes, because Java must have all the basic computational operations which any software requires. I can't think of many basic CPU instructions which aren't covered by Java operators. On the other hand, it has no way to move things around structures unique to your processor (like registers). Yet if the processor was designed specifically for Java, I guess it could provide some Java way to do this. For example, through static method invocations which generate bytecode understood directly by the processor---e.g. "static void move(int a,int b)" on a CPU-specific class which would move data from register a into register b. I suppose memory allocation and GC would be a bit trickier, but not impossible.
BTW, do you have a "model answer" to this yourself or is this totally open ended?