Comparison on using Java for an Operating System instead of C
Java is a managed language which offers memory safety.
In Java, pointers do not exist, so we cannot do pointer arithmetic on a function pointer. An application can invoke a method defined in a class through the class object. In Java, we cannot do unsafe typecast of an object to overwrite the method pointer with something else. An out of bound array access throws a runtime exception in Java, so return address corruption is not possible.
Now, let us look at some important aspects of an Operating System implemented through Java.
- Memory isolation:
In Java, memory can only be accessed via objects. A Java application cannot access memory outside an object. The virtual memory is not abstracted to the application. Due to these reasons, we do not need additional hardware support (segmentation/page tables) for process allocation. There is no need for page tables or privilege rings. The kernel is itself a Java process, so both kernel and untrusted applications execute in ring-0.
- System Calls:
To execute system calls, we need to switch to the kernel process. Switching to a different process does not require a page table switch. So, the system call handler is just a context switch routine that copies system call arguments in kernel address space and directly jumps to the system call handler.
- Inter Process Communication (IPC):
IPC is similar to the system call handler. The context switch method needs to invoke the receive routine in the target process after copying messages. So, this is better in comparison to Linux and Windows, where we have to do two ring transitions and one context switch to schedule the target routine.
- Device drivers:
Each device driver can be assigned to a separate Java process. Device drivers can talk to kernel via system calls.
But, the existing operating systems are not written in Java. It is because, Java is not an efficient language particularly due to garbage collection that can cause arbitrary latencies.
Therefore all the Operating Systems use C language extensively instead of Java.