Page 1 of 1

Designing for a language other than C.

Posted: Sun May 08, 2005 3:35 pm
by mystran
In the series of Mystran's random babblings, we will today touch the issue of designing an operating system from a slightly different point of view than most of us would normally do. Hopefully this will provide some insights to someone (including me ;D).

I think most of us take it from granted that the operating system itself is written in some Algol derivative. In fact, in most cases that derivative will be C or C++, although we have at least one Pascal user here as well. There's every now and then discussion about writing the kernel in some "higher level" language, but I think many of us will still assume that the final operating system will feel someone like existing systems.

What I mean is that when we design things like memory management or IPC, we tend to think in terms of what can be done in a half sane way with a language like C or C++.

Suppose for example, that we where writing an OS for a dynamically typed, functional language, say Lisp. There would be several natural things to support. First of all, since a Lisp implementation can trivially support a copying garbage collector, there would be little point in serialization. Just make copy of the structure into a buffer area, and transfer the buffer.

But what happens if the structure contains functions? There are two alternate things that could be done. Either copy the function as normal data (not hard to do since dynamic compilation is likely to be a feature anyway), or make remote pointer, and wrap it inside a stub that calls the other process. Since all processes are assumed to run Lisp, there's little point in specifying types of interfaces. Any function can be called remotely.

Now, let's assume that we have a language such as Scheme, were continuations are first-class, and tail-call elimination is a fact. Now the IPC system should allow one to transfer a continuation from one process to another. But if we can do this, why not allow that continuation be forwarded elsewhere? On regular operating systems this kind of stuff is quite hard to implement, but it's not hard to support them in an IPC design directly.

On the other hand, let's consider a situation where all programs where running E code, which relies heavily on events. Explicit multi-threading would just be extra now. Each of the systems processors could just take events from a global event queue, and execute them in whatever process's address space they happened to belong to. Scheduling would now work on event-level, and events would have priorities, not processes.

One can probably think of tons of other language features (or programming conventions) that would benefit from kernel level implementations, but wouldn't be practical in a regular imperative language like C.

Paul Graham talks about the blub paradox and I think it tends to affect OS development as well. I hope my writing helps you think different.

Re:Designing for a language other than C.

Posted: Sun May 08, 2005 4:39 pm
by Colonel Kernel
IMO, the best way to "think different" is to consider the end users of your OS and what their needs are. The same goes for all software, really. The link you gave was really interesting, but the key point is that those guys didn't use Lisp just because they could -- they used it to be more competitive and offer more features to their customers in less time. This is capitalism.

The big problem I see with implementing an OS in a language like Lisp is that it becomes more cumbersome to efficiently support applications written in other languages like C++ and even Java. If you're writing an OS that will only be used by Lisp programmers, then great. Otherwise I don't think it's really practical.

I agree that if we can't use programming languages with better and more powerful abstractions, then we're limiting ourselves. What I would like to see is efficient OS support for an entire range of languages, from C++ to Lisp and its brethren. Choice is good. :)

Re:Designing for a language other than C.

Posted: Sun May 08, 2005 7:24 pm
by crc
My OS is written in Forth (with the core compiler being written in assembly), so I get to look at things from a slightly different perspective.

I tend to be minimalistic. My hard disk driver (using LBA) is 29 lines of code; 890 bytes of source. Like most of the drivers, it's compiled on startup. (Non-essential drivers can be loaded and compiled when they're first needed). Parallel port support is all of nine lines; Serial port support takes 18. (These counts include blank lines and comments). Very few drivers take more than 1k of memory once compiled. Many are just a few hundred bytes.

Of course, I can't draw from a lot of existing code. Direct porting of C to Forth is ugly, so I have to study examples to figure out how they work and rewrite them. Not that this is a bad thing....

On the applications front, I don't support apps that aren't written in Forth. So it's not a big issue for me. Every app gets loaded into a single addressing space and can reuse any function from the "kernel" or other apps. It's really a nice environment, at least for me. I have an editor and the essential Forth environment on hand at all times. Loading an additional app is just "load <path><nameofapp> \".

Actually, it's reached a point where I dislike coding in C and other "conventional" languages. The seamless environment I have with RetroForth is just too nice to give up :)

Re:Designing for a language other than C.

Posted: Mon May 09, 2005 10:42 am
by mystran
Colonel Kernel wrote: The big problem I see with implementing an OS in a language like Lisp is that it becomes more cumbersome to efficiently support applications written in other languages like C++ and even Java. If you're writing an OS that will only be used by Lisp programmers, then great. Otherwise I don't think it's really practical.
But there's the exact point. By writing an OS in C, and for C, you make it more cumbersome to efficiently support applications written in other languages like Lisp or Haskell, or E, or whatever.

In fact, the language I mentioned were just arbitary examples of languages I happen to know well enough to talk about. Believe me, for many languages C is not the most simple language to interface with. What I was trying to say is that often we take "efficient support for C" as the first priority, even if "efficient support for LanguageX" would be just as good.

And before anyone mentions backwards compability, I'd say there is little point in talking about OS design if all you want to do is support existing programs. :)

Re:Designing for a language other than C.

Posted: Mon May 09, 2005 2:09 pm
by Crazed123
Portability would be more of a sensible priority. I want to support any language running on my OS. And that events thing is an interesting idea. Think that could be integrated with a normal schedule by allowing processes/threads to go into perpetual sleeping state and be disrupted only be events from a queue?

I should try programming in LISP a bit. I've got a book that gives me some theoretical understanding, but some practice would really do me good.