It started with a question from a collegue at work: “Why are all operating systems nowadays written in C?”.
I wanted to dig deeper…
Some years ago it would have been extremely tedious to write an operating system on your own. Don’t get me wrong, it’s still a tremendous task, and I don’t think anybody should do it for the sake of proving the world, he is able to build the best operating system ever.
Tutorials are around everywhere, especially nice are video tutorials of people who are actually implementing an operating system with you “live”.
I used the following two:
- Writing a Simple Operating System - from Scratch, Nick Blundell
- Write your own Operating System, Viktor Engelmann
There are also nice web pages out there collecting stuff for OS developers, to pick two of them here:
One goal is to write a minimal self-hosting operating system. This is a long way to go. Many tools like make, binutils, nasm, compilers require some part of a POSIX environment.
So far the simplest explanation I have for everybody using C is simple:
- the operating system has been written in C ages ago
- every possible library is built on top of C and POSIX, and nobody wants to rewrite everything all the time
- the most prominent platform is Intel, and Intel has some special “features” like I/O ports and alignment, packing of structures which requires some low-level features from a language. C has those features (alignment and packing). Other platforms may be less quirky and old and be less demanding on the language to provide such features.
- open API specification: the Cdecl calling convention is available everywhere. The reason why so many scripting languages are popular, is that they can reuse tons of libraries via the Cdecl API.
- there is a lot documentation about how to do operating system tasks in C, there are not so many around for other languages.
This said, C++, D, Ada, Pascal are valid alternatives for building an operating system, to name two real-world examples, not just toys:
For embedded or special purpose operating systems it’s more feasible to use another language than C. Exokernels especially open up a new area in operating system development, where relying on the past is not really that important.
Don’t be fooled, if I say C, this doesn’t mean you cannot benefit early from an object oriented design, as soon for instance PCI with loadable drivers or a widget libary come into play. The following articles are useful to build simple-inheritance with C:
And, yes, the Linux kernel applies exactly this principle, as we can see in https://lwn.net/Articles/444910/.
I’m following the Wyoss guide and I’m trying to do almost everything in C which was done in C++ in the tutorial. I’m also deviating a little bit here and there:
- I have my own small little C library (src/libc).
- I wrote a boot loader (yes, I know the comments on OsDev regarding writing one). Nevertheless it’s informational and has some historic value.
- I try to use as many compilers and cross-compilers as possible not just one toolchain.
As always you can find the sources of the project on my personal git repository at: