11

This is probably more of a subjective question, but which language (not API like .NET or JDK) would you use should you write your own operating system? Which language provides flexibility, simplicity, and possibly a low-level interface to the hardware? I was thinking Java or C...

8
  • 13
    How does Java provide you a low-level interface to the hardware?
    – jasonh
    Jun 11, 2009 at 23:26
  • There are only a few instances I can think of an OS coded in Java. There are embedded systems that have hardware/software that can bootstrap a JVM like on some Blu Ray players. Jun 11, 2009 at 23:30
  • 2
    There are flavors of ARM cores (ARM7TDMI is one, IIRC) that include part of the Java byte code engine in hardware. Using it requires software support that apparently requires a license from some unknown party to even see the docs since the ARM documentation only says it exists. You'd still have interrupt handlers in native ARM instructions, however.
    – RBerteig
    Jun 11, 2009 at 23:36
  • 4
    "How does Java provide you a low-level interface to the hardware?" Java Native Interface. You could write 99% of the OS in Java and 1% in a lower-level language.
    – Nosredna
    Jun 11, 2009 at 23:47
  • 2
    It doesn't really matter if Java was designed for it or not. It's capable of it.
    – Nosredna
    Jun 26, 2009 at 23:53

26 Answers 26

32

C, of course.

4
  • Aside: It is so interesting that I lose 2 reputation points whenever people downvote the answer. So far 15 upvotes and 5 downvotes or so, not clear how SO algorithms work. But this answer got me a Badge of Honor: Nice Answer... pretty cool. Jun 11, 2009 at 23:44
  • 8
    Also, note that "C, of course" is a much better language than "Definitely... C" Jun 12, 2009 at 0:21
  • Not the most high-level... I would use a stronger language on top of C... but it works!
    – Aethex
    Jun 12, 2009 at 0:22
  • C is the only appropriate choice currently for a production operating system kernel, though D might also work.
    – Ana Betts
    Jun 12, 2009 at 2:40
12

Haskell.

Once you have flipped the right hardware bits, C is a terrible language to use for the rest of the OS. Things like the scheduler, filesystems, drivers, etc. are complex high-level algorithms, and you don't want to be writing those in assembly language (or C; same thing). It's too hard to get right. (The VM subsystem and memory manager may need to be written in something low-level, as you will need to bootstrap your high-level langauge's runtime somehow.)

Anyway, this isn't just a crazy idea that I am coming up with for SO. Here is an OS written in Haskell: http://programatica.cs.pdx.edu/House/

Lisp is another good choice; the original Lisp machines were infinitely more tweakable (at runtime) than "modern" OSes like UNIX and Windows.

Sometimes history forgets good ideas (often in the name of "maximum performance"), and that makes me sad.

1
8

D would be an interesting choice. From its own description:

D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. Special attention is given to the needs of quality assurance, documentation, management, portability and reliability.

The D runtime assumes the existence of a garbage collector, which would not be appropriate for the very lowest levels of the kernel. However, it would be appropriate for many of the higher layers.

2
  • D has inline assembly too, doesn't it?
    – Nosredna
    Jun 12, 2009 at 0:39
  • Actually last I heard ldc, an open-source D implementation allows you to disable the garbage collector. Check out XOmB.
    – Joe D
    Nov 19, 2010 at 21:54
6

Build the basic components like task schedulers and drivers etc with Assembly, then build the higher level components like applications and tools with C

I believe this is how Windows XP was built too (unsure about Windows Vista and Windows 7).

5

Definitely... C

5

C, ASM, C#

Singularity

1
  • Not necessarily. A low-level language like C would be able to load the required libraries and services to execute a high-level language like C#. It's a bit like JVM, but built right in.
    – Aethex
    Jun 12, 2009 at 21:55
3
  • Low-level in something like Haskell or D. Productivity over performance, in my opinion. You can rewrite slow parts in C++ or even assembly later if the need arises.
  • High-level in Python or Ruby. Ideally I'd also have a really fast JIT-capable VM for that language, but that's not going to happen for either language for a while. Lua would be a good alternative if speed gets in the way.
3
  • Haskell is not exactly low level
    – Unknown
    Jun 12, 2009 at 1:04
  • 1
    Eh, it's low-level enough. Like I said, productivity over performance. Jun 12, 2009 at 1:32
  • If you don't feel pain, you're not doing it right! haha typical programmer.
    – Breton
    Jun 12, 2009 at 1:41
2

The kernel has to be written in a low-level language, C is by far the best choice for this, because it is so memory efficient. The higher levels could be built with a combination of Java or more ideally Objective-C, and scripting languages like python and ruby, or lua.

2
  • 5
    The kernel "has to" be C? Why? There are many high-level languages that compile to native code.
    – jrockway
    Jun 11, 2009 at 23:37
  • Has to be low-level enough to interface directly with memory, C being in my opinion the best choice for that. Everything which doesn't need to interface directly with the hardware and isn't speed/memory critical can be written in higher level languages.
    – cloudhead
    Jun 11, 2009 at 23:51
2

Honestly, I would either use C or some hierarchy of languages that I had either designed or fit together completely seamlessly. What I would be looking for is a seamless experience that starts at the bare metal level and then I could move to higher and higher level languages as I moved up the problem space. I would probably chose something like:

  • C - for bare metal stuff like drivers, kernel, etc
  • Java/C# - for application-level things like administration consoles, OS apps
  • Python/PowerShell - for scripting activities like common administrative tasks (creating a new user, etc)

Personally, I think C/C#/PowerShell is more tightly integrated and the type of experience I'd be looking for. Of course, if I ever got so ambitious as to write an OS, I would have a lot of spare time on my hands and would probably really enjoy tackling the language stack first. So maybe it would be L/L#/LScript ...

2

BitC seems to have this in mind. Despite it's name it seems to be the midpoint of assembly language and lisp. The goal was to make a language with a strong correspondence with machine language but have an intermediate representation that supports stronger correctness inferences than is possible with most other common languages. The languages was created as part of the Coyotos project, an operating system with lofty goals of security and reliability. Formal verification is made significantly possible with the ir used in BitC.

1

Ada:

Ada is a structured, statically typed, imperative, and object-oriented high-level computer programming language, extended from Pascal and other languages. It was originally designed by a team led by Jean Ichbiah of CII Honeywell Bull under contract to the United States Department of Defense (DoD) from 1977 to 1983 to supersede the hundreds of programming languages then used by the DoD. Ada is strongly typed and compilers are validated for reliability in mission-critical applications, such as avionics software.

Ada, because it was not only specifically developed for such projects, but it also provides support for several very useful high level features (such as support for strong typing, concurrency and abstraction) that are simply not available in standard C.

So that, even as a project grows, you don't have to work around language limitations (think encapsulation, abstraction, namespaces in C).

Don't get me wrong, C works obviously for a great many of projects, but once a project has gained a certain size (think Linux kernel, gcc, GNOME), you will inevitably appreciate certain features of more high level languages to make the development process less tedious and also less obfuscated.

In C however, these features usually end up being -pretty poorly- emulated by excessive and almost pervert use of the pre-processor (this can for example be seen in the gcc code base), so that you get to see lots of nested macros, that from an implementation point of view, actually emulate features found in other programming languages.

In addition, Ada is the only programming language, that I am aware of, that actually provides standardized support for source code analysis using the ASIS, having such a facility in place is however the prerequisite to actually be able to maintain and transform/re-engineer a code base in the long run (think refactoring).

Having an interface like ASIS available, means that you can actually implement "semantic patching", where you can automatically rename a file, function or variable/data structure and it will actually work.

2
  • LOL You do go to my University?
    – Nolte
    Jun 14, 2009 at 2:49
  • Something also people often forget is that C's alleged support for low-level programming pales in comparison with that of Ada. Also depending on the kind of OS one intends to write, it might be prudent to consider the use of a formally defined subset of Ada, SPARK.
    – Schedler
    Oct 27, 2010 at 21:45
1

Java ?? no jave runs on a virtual machine which needs an os to run on top of , maybe C and some ASM ;)

2
  • 1
    You can compile java to native code. Look at gcj . You can use JNI to connect to lower level operations.
    – Kekoa
    Jun 12, 2009 at 0:17
  • I did not know that :) thx for the info but still don't think that java is a good idea for an os :) Jun 12, 2009 at 1:27
1

I would go with D to see whether it can do it.

1

I would only pick the following 3 out of practicality.

  • C (good old fashioned)
  • C++ (C with stuff tacked on. Windows is partially written in this)
  • Java (the medium level language that just might have a capable garbage collector with controllable pauses with G1).
1

If I were going to start a new OS I'd do it with the subset of C++ recommended by the embedded industry. You can use things like classes and use it "as a better C" and be just as fast. Just avoid things that have massive overhead. You can even use some template features, if you stick to a certain subet that basically don't have any overhead. Look on embedded.com for features in C++ that have little to no overhead, but will allow you organize your code better than you ever could in C.

1

Oberon? I guess I miss Pascal too much some times. C paid the bills for quite a while, but I don't really love it.

1
  • Modula-2 would be nice. Procedural, and has the proper bit-fiddling stuff.
    – Nosredna
    Jun 26, 2009 at 23:55
1

Lisp of course!

With Apologies to Robert Frost

Title text: Some say the world will end in fire; some say in segfaults.

0

For an OS, you want speed at the lowest levels. So assembly, C, C++, Objective-C, or Java seem to be the current choices. Although it's just recently that Java got fast, and it's hard for me to imagine an OS with garbage collection.

If I were writing my own, it would be a mix of assembly and C.

4
  • You mean the java virtual machine had some speed improvements recently. So what is running your JVM if your implementing an operating system?
    – soulmerge
    Jun 11, 2009 at 23:28
  • Obviously the OS can be built in layers. The bulk of it could be in Java. There's a lot to an operating system these days. Not just drivers.
    – Nosredna
    Jun 11, 2009 at 23:31
  • There are C compilers to compile them selfs, many JVM already know to compile to native, so it's not far-fetched... Jun 11, 2009 at 23:36
  • 2
    The OS could be configured to run Java natively, just as Windows does with CLR or whatever it's called. A native-Java OS is certainly something to ponder about.
    – Aethex
    Jun 11, 2009 at 23:37
0

A C or C++ microkernel with a JIT for a highly dynamic language like Ruby or maybe a language with native support for the Prototype pattern. Even device drivers in that language.

Not because it's practical but because it's really cool. Cool in the way that NeXTStep was cool for using Obj-C for pretty much everything.

0

http://www.dwheeler.com/sloc/redhat71-v1/redhat71sloc.html - share of languages in Linux's source code.

0

C, by a number of reasons. Other candidates, like D, are great. However, C has this advantage: there's a lot of available open-sourced C code that you could reuse in your project (much more than is for other languages appropriate for system programming).

0

I would be torn between using some existing low level language and write my own based on C# but with much better generics support. In second case I would make each method generic, but all the constraints will be resolved by compiler - to allow "duck typing" like in Scala but still language should be static. Also static virtual methods would lower the codebase.

I've had that idea for a long time, but it never seems to be doable in real timeframe, so who knows maybe in the future. :-)

0

Some would say Java.

Note that openfirmware is written partly in Forth, and it's very low level.

Have an open mind.

2
  • Um, FORTH is not FORTRAN. FORTH is, however, a very low level language despite its appearance. It was invented to program controls for things like telescopes and long exposure cameras and ran in 8-bit microprocessors.
    – RBerteig
    Jun 11, 2009 at 23:40
  • I can't help myself. I'm going to have to fix that for you. fortran -> Forth
    – Nosredna
    Jun 11, 2009 at 23:55
0

"The kernel has to be written in a low-level language, C is by far the best choice for this, because it is so memory efficient. "

Um... What about FORTH?

FORTH can be low level and high level, so you could have a whole operating system written in FORTH from the ground up, and still have a nice easy REPL scripting environment on top, also in FORTH.

However, any decent operating system should support lots of langauges on top, from C all the way to Python Ruby and Javascript. Making FORTH the basis for it all has a lot of benefits though.

edit: I'd only ever attempt this for an embedded environment with a single known hardware set. Trying to write an OS that could compete with Linux or Windows is a fools job.

0

If this isn't a hypothetical question, and you're looking to create your own OS, I'd probably go with C because most of the examples out there are written in C.

Also, (And I haven't build an OS yet so take this with a grain of salt), I'm thinking that the c runtime libraries would be a lot easier to port to your new OS than say .NET.

0

Pascal + Oberon: they have the power of C and C++ but they're not as daunting to use. Both these languages are grossly under appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.