views:

673

answers:

24

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...

+32  A: 

C, of course.

Ryan Oberoi
......Word......
Suvesh Pratapa
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.
Ryan Oberoi
Also, note that "C, of course" is a much better language than "Definitely... C"
Igor Krivokon
Not the most high-level... I would use a stronger language on top of C... but it works!
Aethex
I recently got a necromancer badge. I have no idea why.
Breton
C is the only appropriate choice currently for a production operating system kernel, though D might also work.
Paul Betts
A: 

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.

Nosredna
You mean the java virtual machine had some speed improvements recently. So what is running your JVM if your implementing an operating system?
soulmerge
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
There are C compilers to compile them selfs, many JVM already know to compile to native, so it's not far-fetched...
Liran Orevi
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
+2  A: 

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.

cloudhead
The kernel "has to" be C? Why? There are many high-level languages that compile to native code.
jrockway
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
+5  A: 

Definitely... C

bLee
+5  A: 

C, ASM, C#

Singularity

JTA
There can be only one.
Nosredna
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
A: 

Some would say Java.

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

Have an open mind.

Alex Brown
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
Oh that's pretty embarassing. Thanks RB.
Alex Brown
I can't help myself. I'm going to have to fix that for you. fortran -> Forth
Nosredna
+8  A: 

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.

Greg Hewgill
D has inline assembly too, doesn't it?
Nosredna
Yes, it does (for x86): http://www.digitalmars.com/d/2.0/iasm.html
Greg Hewgill
+9  A: 

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.

jrockway
+2  A: 

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 ...

Lee
A: 

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.

sjmulder
A: 

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

spitfire
A: 

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).

Igor Krivokon
+6  A: 

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).

Nippysaurus
A: 

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. :-)

SeeR
+1  A: 

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.

none
LOL You do go to my University?
Nolte Burke
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
+1  A: 

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

Yassir
You can compile java to native code. Look at gcj . You can use JNI to connect to lower level operations.
Kekoa
I did not know that :) thx for the info but still don't think that java is a good idea for an os :)
Yassir
+1  A: 

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

auvi
+3  A: 
  • 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.
musicfreak
Haskell is not exactly low level
Unknown
Eh, it's low-level enough. Like I said, productivity over performance.
musicfreak
If you don't feel pain, you're not doing it right! haha typical programmer.
Breton
+2  A: 

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.

TokenMacGuy
+1  A: 

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).
Unknown
A: 

"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.

Breton
+1  A: 

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.

A: 

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.

Roboprog
Modula-2 would be nice. Procedural, and has the proper bit-fiddling stuff.
Nosredna
A: 

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.

Giovanni Galbo