-4

There have been experimental operating systems written in a variety of languages, including C# (Singularity, SharpOS), Java (JavaOS, JX), and Lisp (various Lisp machines).

Why then are C (and to a lesser degree C++) the dominant languages used for developing an operating system kernel? Is there something about C that makes it especially well-suited for use in kernel programming, or is it more for historical reasons?

Shahzaib Mazari
  • 434
  • 4
  • 14
  • Not really, no. – machine_1 Apr 14 '19 at 14:04
  • Can't run code in a virtual machine without first having a virtual machine. Android is very Java-centric, but it still has plenty of native code supporting that. – chris Apr 14 '19 at 14:06
  • Possible duplicate? https://stackoverflow.com/questions/10904721/is-it-possible-to-create-an-operating-system-using-python – machine_1 Apr 14 '19 at 14:08
  • The title of the question is good, though genetic. BTW python is used heavily for Ubuntu unity – Dr Phil Apr 14 '19 at 14:11
  • I have never heard of anyone writing an operating system in C#. Or C++, for that matter. – molbdnilo Apr 14 '19 at 14:11
  • 1
    For what it's worth, the lowest levels of most operating systems are actually written not in C but in assembly language. The first layer above that is often written in C because it's one of the easiest languages for interfacing with assembly language constructs and raw memory addresses. – Daniel Pryden Apr 14 '19 at 14:15
  • There was a project at one point to build a hardware CPU that could execute Java bytecode; if that ever had happened then you could write an OS kernel in Java. I don't think it was ever fully implemented, though. – Daniel Pryden Apr 14 '19 at 14:17
  • 1
    C# is not used in the system programming :) It needs the OS to run on (and the JIT compiler or interpretter) – 0___________ Apr 14 '19 at 14:18
  • @P__J__: Microsoft Research built an experimental OS kernel in C#; it was the [Singularity Operating System](https://en.wikipedia.org/wiki/Singularity_(operating_system)). – Daniel Pryden Apr 14 '19 at 14:19
  • 1
    @DanielPryden Read more before commenting. `The hardware abstraction layer is written in C++ and runs in protected mode. There is also some C code to handle debugging`. The rest can be written even in HASKEL if you like but the "essence" of OS is written in C++ & C – 0___________ Apr 14 '19 at 14:24
  • 1
    You need a self hosting language that can address memory directly and integrate easily with assembler. – Galik Apr 14 '19 at 14:25
  • @P__J__: You're right that Singularity contained C++ parts, and I'll admit I know practically nothing about the Singularity OS beyond what Wikipedia says about it. But the point is that you could write *kernel device drivers in C#*, which is nearly as "operating system"-y as you can get. Your comment said "C# is not used in the system programming" and I think that is an equally false statement. – Daniel Pryden Apr 14 '19 at 14:30
  • @DanielPryden this is a bit misleading. They are not "kernel" as we understand it in Linux for example. BTW the idea is dead and was only used by MS to test some ideas. – 0___________ Apr 14 '19 at 14:33
  • @P__J__: That's true (mainly because Linux is a particular kind of kernel architecture). But in my edit to the question I listed several other examples of operating systems with kernels or kernel-level parts written in languages other than C. I don't think this question is too broad or unanswerable. It's a legitimate question. – Daniel Pryden Apr 14 '19 at 14:34
  • @DanielPryden youe edit is wrong I am going to flag it for the mods attention. It changes the question and it the first paragraph is incorrect and misleding. None of those "OS"-es are written in the languages you mentioned. The core of those OSes was written in C, C++ or assembler. – 0___________ Apr 14 '19 at 14:36
  • @P__J__: I don't want to get in an argument with you. I don't think my edit changed the *intent* of the question, which was to ask why C was so strongly entrenched in this space. Perhaps the question is a matter of semantics. But you seem to be falling victim to the "No True Scotsman" fallacy. If we follow your argument to its logical conclusion, then we can say that *no operating system is written in C either*: all operating system kernels contain bits of assembly language for their respective hardware. That may be *true* but it's not what people usually mean. – Daniel Pryden Apr 14 '19 at 14:42
  • I can't understand why my question put on hold? I just want to know the answer of my question and I think my question is very clear. – Shahzaib Mazari Apr 14 '19 at 15:05
  • @ShahzaibMazari Does my answer not answer your question? – Jesper Juhl Apr 14 '19 at 15:09
  • @DanielPryden which is exactly what I wrote - C, C++ + assembly. Many do not even the single line written inassembly. – 0___________ Apr 14 '19 at 15:22

1 Answers1

4

Some (not all) reasons for using C languages for OS kernels:

Performance is important to an operating system, so interpreted languages are out and compiled languages are in. Also, you would have to write the interpreter as part of the OS in some other language anyway, so...

Direct access to hardware resources and easy ways to do low level bit "fiddling" is also important for an OS and C and C++ both have that covered.

You usually also need to write some bits directly in assembly for the specific processor architecture you are targeting. Writing bits and pieces in inline asm is simple in C and C++.

Also, most other languages support C's calling convention, so writing your OS in C makes it easy to expose system calls to user-space in a way that can be easily interacted with.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70