Gilbert has posted 6 posts at DZone. View Full User Profile

Writing an Operating System: Has Java Replaced C?

05.17.2010
| 24956 views |
  • submit to reddit

From its beginning, Java has steadily replaced C. Java is a viable alternative to C. How far will it go? Is C in danger when writing an operating system?

This article considers the subject of writing an operating system. When writing an operating system, you have at least three choices: (1) Pure C. (2) Pure Java technology. (3) Combination of C and Java technologies.

It's the culture. Consistently, we choose not to use Java. But why? Is there something inherently wrong with the technology? No, I blame the culture, not the technology. We choose to a language because of both social and technical benefits. Because it scores so high on social benefits, C is not in danger.

Technical benefits. While C requires more from a programmer, C can do things that Java can't easily do. Build a virtual memory manager, preemptive multi-tasking, a boot loader, a self-contained executable program. We expect an operating system to keep on running after an application crashes. We expect an operating system to reclaim resources after a process ends. We do not yet expect this from Java itself.

Social benefits. As technicians, most of us understand the technical benefits. But how many technicians think about the social benefits? When every programmer on an operating system project knows C, every programmer is trained in C culture, a value system associated with C. When C programmers work together, they share a common background and experience of systems programming. They inherit a rich culture from a POSIX-style of programming. They also inherit a culture of high expectations for community and vendor support. On the other hand, Java programmers inherit a different culture, based upon application programming, and thus lower expectations.

  • For example, what C programmer would distribute a dynamically shared library without a version number embedded in the library name? On the other hand, what C programmer would use it? It would be unthinkable! And yet, Java programmers do it all the time.
  • What C programmer would change the interface of such a library without regard to backward compatibility? As a consumer, what C programmer would put up with that? C libraries are not automatically backward compatible. No, the C language must be used in a specific way and this is what C programmers are taught. Best practices are taught within a culture. And again, Java programmers are missing something.
  • In the OSGi module system, a programmer is taught to assume that a module will not be compatible with the next version. But what does that say about the Java culture? Java culture teaches a programmer how to write an unusable program.

 

Defeated by culture, overcome by culture. Java culture defeated early efforts to combine Java technologies with the operating system. Look at the heroic effort of JPackage.Org. Java culture makes it difficult to distribute with RPM. So JPackage.Org overcame the Java culture by creating a culture of its own. With RPM, we assume that an RPM will be compatible with a certain version and higher, until proven otherwise. The difference is huge.

A combination of C and Java could be a better choice. We are not required to write an operating system in just one language, are we? Why not use the best of C and the best of Java? We are starting to see products based on a combination of C and Java. So C is going to be around a while. Has any programming language ever died? (See also ASM, COBOL, FORTRAN, Lisp, Perl, PROLOG, SGML, Smalltalk.)

A pure Java operating system remains elusive. What about tomorrow? A combination of C plus Java technologies makes sense. Any programming language that runs on the Java Virtual Machine can be used to write the operating system, system libraries, system commands, device drivers, and applications. A module system is a necessary first step. Over time, as Java itself improves, as best practice improves, as module systems improve, fewer lines of code need to be written in C. A Java-based operating system depends upon a widespread knowledge of how to write Java properly for use in an operating system. A cultural shift is required. It is difficult, if not impossible, to overcome defects in a culture.

C remains a good choice. For many years, the C programming language has been a popular choice for writing an operating system. Some might say that it is the only choice. C provides many qualities we desire when writing an operating system. (1) Perfect binary backward compatibility is obtainable. With C, you can, for example, run a program written for Fedora Core 9 and run it unchanged on Fedora Core 12. (2) Separation of the interface from the implementation. With C, you can, for example, put the entire kernel API into header files. With Java, not so much. (3) Low-level access to hardware. With C, you can write software for any hardware.

Again, with Java, backward compatibility remains elusive. It can be done, but very few developers know best practices. A module system, such as OSGi, becomes critical because we want to use existing products but we cannot use directly. If everyone had written products correctly from the beginning, a module system would be simpler. But I blame the culture, not the language. Most Java programmers are probably both unaware and unconcerned about these issues because they are not using Java to write an operating system. Too many upstream APIs are unsuitable for an operating system because incompatibility is expected and planned.

Java borrows from C. It should be no surprise that the Java programming language borrowed heavily from C. It was intended to be an easy transition from C to Java. And from the earliest versions, the Java programming language has been an alternative language for writing an operating system. But Java has not reached its potential.

Here is a brief and incomplete review of the history of a Java-based operating system.

  • 1996, "Java", Oracle (was Sun Microsystems)
  • 1996, "JavaOS", Oracle
  • 1997, "JOS", jos.org
  • ?, "Eclipse", eclipse.org
  • 2004, "JNode, jnode.org
  • 2007, "Guest VM", Oracle
  • 2008, "Android", Google
  • 2009, "WebSphere Application Server Hypervisor Edition", IBM

JavaOS What ever happened to JavaOS? It booted across the network. It downloaded a JVM written in C. It downloaded standard Java class libraries from the network. Applications were written in Java and downloaded. This operating system was never intended as a replacement for Linux and Windows. But it demonstrated the potential for the C/Java architecture.

"Everything that can be written in Java should be written in Java." On the first original Java Lobby forum, we discussed the potential of the Java language for writing an operating system. The Java Operating System (JOS) project was born. At the time, some parts of the operating system could not yet be easily written in Java. The JOS Project demonstrated that a Java-based operating system was possible. It also demonstrated that Sun Microsystems kept making decisions without any regard for writing an operating system. (1) Bloat of Java Class Library. Business APIs, such as JDBC, were added to the core. (2) Development of a separate J2ME specification to reduce bloat. (3) No support for modules, no module system.

With JNode, the operating system kernel is written in the Java programming language and compiled ahead of time on a foreign operating system. Some C programmers have said, "It's a trick!" They have been led to believe that it is not possible to develop an operating system in a programming language other than C. Go ahead. Try to find one line of C code in the source.

With Eclipse, you can already do just about everything within the IDE that you might do with an operating system. Some do not yet see the potential for Eclipse to become an operating system. But eventually, a separate operating system becomes redundant. Why not just boot directly to Eclipse?

Does Android surprise you? Java is used wherever possible. The C programming language still has a place. It is set aside for anything that the Java programming language cannot yet do.

WebSphere Application Server Hypervisor Edition is a commercial product from IBM. They started with Linux, removed everything that was not needed to run WebSphere, a J2EE application server. Like Android, the C language is used to build enough of a platform to run the JVM. Applications are written in Java.

Conclusion: Culture is the problem. Culture determines what is possible. You cannot build and maintain an operating system in Java for as long as Java culture teaches that it cannot be done, and should not be. A culture of system programming builds an operating system. The programming language is not the problem.

 

Reference: http://www.dzone.com/links/c_in_danger_and_thus_higherlevel_languages_.html
Published at DZone with permission of its author, Gilbert Herschberger.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jonathan Fisher replied on Mon, 2010/05/17 - 9:03am

I have long thought a JVM operating system would be incredible, but I want to ask a question to spark some discussion. What language are you going to write your 'JVM OS ' in? :D

Gilbert Herschberger replied on Mon, 2010/05/17 - 10:34am in response to: Jonathan Fisher

Dear Jonathan,

Like you, I have long thought it would be incredible, too. I have been working since 1997 toward one goal: a general-purpose, Java-based operating system. I have been developing Java applications and libraries with a Java-based operating system in mind.

The problem is poorly written Java. In general, Java programmers do not write with an operatinng system in mind. If only upstream APIs were written properly, a Java-based operating system would be easily assembled from existing, proven products, something in the tradition of Linux.

JJOS is a combination of C/C++ and Java. When I worked to make JJOS more platform independent, the problem was poorly written Java. The C/C++ source code was easy to master. I contributed a Bytecode Native Interface (BCNI) to JJOS, enabling platform-independent jars to run on Linux or its native kernel.

JNode is pure Java. And when I work to integrate existing products with JNode, the problem is poorly written Java. JNode has a long list of excellent features. But without independently developed and compiled, best-of-breed APIs and a real module system, it remains experimental.

Only a community can build an operating system. I'm sorry when I was not clear that a Java-based operating system cannot be written by one person alone. I write this article because a Java-based operating system requires a community of Java programmers surrounded by the right culture. I have already contributed architecture and documentation to more than one Java-based operating system, plus 2+ million lines of Java source code. And yet, herculean effort cannot overcome a cultural problem.

Thanks,

Jim Bethancourt replied on Mon, 2010/05/17 - 12:49pm

You might want to check out the GuestVM Project: http://labs.oracle.com/projects/guestvm/

They've built it on top of the Xen Mini-OS and use the Maxine JVM. It's pretty well modularized and doesn't have a dearth of classes, making it easy to modify.

Cheers, Jim

Gilbert Herschberger replied on Mon, 2010/05/17 - 8:07pm in response to: Jim Bethancourt

Thanks for the link, Jim. The Guest VM minimizes the size of the non-Java operating system. Do you think this is a Good Thing? With this solution, much of the primordial code has already been written. Therefore, you can build your own operating system on top of Guest VM using nothing but the Java programming language. The Xen hypervisor is written in which language? The microkernel is written in which language? The Maxine JVM is written in which language? Is C in danger?

As for the Java culture, could you please explain why Open JDK is not yet ported to Guest VM? Why isn't everyone working on this? In other words, what prevents us from abandoning Linux and Windows?

Is there a high quality C compiler written in Java? If there were, a C/Java operating system could become self-hosting.

Thanks,

Mladen Girazovski replied on Tue, 2010/05/18 - 5:53am

 Writing an Operating System: Has Java Replaced C?

 Definately not.

There is not one solid, responsive & robust OS written in Java, most Operating Systems are written in C (i don't know any OS written in C++ btw.)

I doubt that there is a "cultural" Problem, but rather the fact that not any language can be used for any purpose effectivly.

Alessandro Santini replied on Tue, 2010/05/18 - 12:50pm in response to: Gilbert Herschberger

Gilbert,

I confess I am confused. There are many things that you cannot accomplish using Java - addressing the memory and the registers directly (e.g. PE bit in CR0), registering to and invoking interrupts, among others.

Now the question is - without these features, how are you going to write device drivers? How are you going to write the boot sector of the disk? Can you fit a JVM in 256 bytes?

Gilbert Herschberger replied on Tue, 2010/05/18 - 4:06pm in response to: Mladen Girazovski

Dear Mladen,

When working on one operating system project, much time was consumed by debate on the best way to express the kernel interface in C++. We never agreed on class design! After switching to C, no more debate. Everyone agreed on POSIX.

I have watched as a team chooses a programming language. I have heard a strong argument for C that goes something like this: An operating system is very complicated. It is written in C. This new application is going to be complicated. If something as complicated as an operating system is written in C, surely our application can be written in C, and should be.

What does it say about C++ and Java that we cannot write an operating system? C has proven itself. It is predictable. It is safe. Why even consider another language?

Thank you for your insightful response,

Gilbert Herschberger replied on Tue, 2010/05/18 - 4:36pm in response to: Alessandro Santini

Alessandro,

Let's start with a normal JVM. The standard Java class library provides support for operating system features, such as threads and file systems and networks. At some point in the Java code, there is going to be a native method. The Java Native Interface (JNI) provides a standard way to enable a Java method to invoke a C function. In turn, some C function might use features of an foreign operating system. While System.arraycopy() probably does not use an OS feature, System.currentTimeMillis() probably does.

When we remove the foreign operating system, we remove all of its features. Now, we must go back and rewrite a C function to do actual work. With JNode, device drivers are written in Java. Do you want to read and write to hardware ports? There is a Java API for that. Do you want to install an interrupt handler? There is a Java API for that. Do you want bit-mapped graphics? A Java package does that. Do you want a file system? A Java package does that.

For JJOS and JNode, native methods are statically linked to the JVM. With native methods, you can do anything you can do with C, such as read, write and allocate memory, register and invoke interrupts.

The boot loader is written to the boot sector of a disk (if there is a disk). Just like Linux, a boot loader can load-and-decompress a boot image. For JJOS, the JVM and standard Java class libraries are statically linked to the boot image and loaded into memory with a boot loader, such as GRUB. For JNode, you can boot the JVM from a CD or boot it from VMware Player.

Thanks,

Aamir Iqbal replied on Fri, 2010/05/21 - 12:42am

Gilbert Thumbs up for you brother.

a very nice and informative approach to do this. I wish one day you will build a pure java based OS. And let the world say "Java can replace C" . 

Keep it up. best of luck.

 

Regards,

Amir Iqbal

 

"I am Java +ve now!"

Gilbert Herschberger replied on Sat, 2010/07/10 - 2:44pm

Update 1: Android by Google is a Java-based operating system. It is written with C and Java. It clearly points to defects in class file design and why JARs may be very bad.

Update 2: Karaf is a subproject of Apache Felix. It may be relevant to a general-purpose Java-based operating system.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.