Has anybody here written their own operating system? I know next to nothing about the low level operation of operating systems, apart from a course I did years ago as an undergraduate, which I’ve now mostly forgotten. I know C fairly well, and some basic (i386 and ARM) assembler, and have access to all the GNU development tools.
Looking around the Internet, there’s some articles on writing a simple OS, though they all appear to be years old, and it doesn’t look like that much work to get something that will boot up and print to the screen. Does anybody have any experience with this? Is it a good learning experience? How much effort is needed to get something that will run a simple compiled binary file as an executable, with basic system calls?
What is the best reference book on OSs? Is Tanenbaum’s book still relevant today, or is it hopelessly out of date?
Boy, I wish I could give you more specific info. I had a class in college that was ‘Operating Systems’ and there was a semester long project wherein a group of four students would put together an OS.
I don’t remember what texts we used, if any.
The tricky part of what you mentioned is the ‘compile and run a binary file’, I believe. You’d need to write a compiler for your OS. Or write the program in such a way as to ignore the OS.
I wrote (well, re-wrote) most of the major pieces of a multi-user minicomputer operating system kernel, and wrote a realtime operating system for a microcontroller. Both of those were around 1980, so I’m pretty thoroughly out-of-date.
If you’re doing a single-user sytem on dedicated hardware (e.g., a microcontroller chip), then it’s really not too complex if you understand the fundamentals like dealing with interrupts. If you’re going to use an Intel-based PC for your hardware, then you’ll be interfacing with BIOS and the system will be much more complex.
I found that the task-switching and interrupt handling were tricky, but once they worked cleanly, you never have to look at them again.
Device drivers probably took more time than anything else. I can’t count how long it took me to build drivers for hard disks and such. With a microcontroller, you can just plug in an RS-232 or RS-422 serial interface and code the driver up in an afternoon. Trying to write USB drivers from scratch is not nearly so easy.
In my Operating Systems course in college, we eventually worked on our own OSes using C and PPC assembly, working off of a XINU base, IIRC. If I remember when I get home, I’ll take a look at my bookshelves and see if I can find the textbooks we used.
Have a look at Simple Real-time Operating System: A Kernel Inside View for a Beginner (Paperback) which I think does well at building an RTOS from the ground up. Sometimes the writing is a little rough, but the concepts are sound.
No, that’s no problem. gcc can compile in standard-alone mode, which is what you need. I think that you need the -nostdlib flag. Plenty of open-source operating systems use an unmodified gcc to compile their kernel.
Edit: Oh, you meant for the user-land programs. It’s still not a problem. You do have to supply your own crt0.S for your OS, but it’s not necessary to re-write the kernel.
Well, I wrote the “OS” for some microcontroller projects, basic stuff, not more than 2K of binary coding.
So put me in the sub-junior, featherweight category.
I don’t have any specific book recommendations as I just worked off of Intel’s IA-32 reference material and some tutorials, but I found that it was an interesting learning experience anyway. My experiences below are with the IA-32 architecture, though I do not know what you are interested in.
It’s quite different to work without the conveniences given by a fully feathered operating system. I found it very useful to use an emulator to avoid needing a second computer to reboot constantly to test new code (I successfully used bochs). It was a bit tricky at first to set up a boot sector and correctly compiling the kernel with gcc, but when that is figured out things get more interesting. On x86 you’ll then have to set up things like memory segmentation, interrupt vectors, various descriptor tables, etc. After that I went for implementing page allocation and virtual memory plus memory allocation (to finally get malloc()). Having all that working was quite satisfying.
The next big step was task switching which I found to be one of the trickier parts as there are so many things that can go wrong if you are not very careful, and debugging at this level can be quite frustrating and time consuming. However, it’s really satisfying when you get your first couple of processes running smoothly side by side, and can then work on a decent scheduler without worrying about the nitty gritty details. I didn’t really get to writing any device drivers besides handling keyboard input and text output though, as I found some other hobby project to work on instead.
I ordered the full Intel IA-32 documentation from Intel’s developer pages and got the whole shebang for free (just had to fill in my address and a couple weeks later a heavy package arrived here in Sweden), that is about 5 large books including an optimization reference, though I don’t know if that offer is still available. They are quite useful as they describe everything in detail, but a book covering OS writing for the desired architecture hopefully covers the relevant parts. It’s probably possible to scrape all the pieces from google too.
ETA: I forgot to write that the IA-32 manuals are available online as PDFs, though it was nice to get them in book format.
Linus Torwalds wrote his own OS. But I assume he used a cross-compiler to compile it. Obviously the very first OSs were written by someone. The original Apple used a version of Basic as its OS, and someone had to build OS functions into it (Woz, I assume). The original IBM PC also used Basic as its OS if you didn’t have a floppy drive and again someone had to build in the OS functions. Oh yes, Tim Paterson wrote MS-DOS and Gates bought that from Seattle Computing (and eventually hired Paterson too). And I think Charles Moore may have written his own OS to go Forth with. Although by the time I used Forth, it piggybacked onto MS-DOS.
The latest edition of Modern Operating Systems came out within the last two years, so it’s probably pretty up-to-date. I know it’s used here for the undergrad OS class.
I haven’t. I have worked on drivers. I’ve written a program onto a floppy that would run when you booted the PC, which could take input and display stuff on the screen, but the reference material I had for how to switch into protected mode was overly arcane for me to feel like slogging through. I’ve cross-compiled and modified a boot loader for an embedded version of Linux.
Overall, I’ve done quite a few of the bits that go into writing an OS, and these days could probably figure out how to manage it, but really unless you want to reverse engineer device drivers, its not worth it.
I’ve written a few operating systems. Most of them have been very tiny, and were used on single board computers in embedded systems.
I’m also heavily involved in a real time operating system which runs on industrial controllers. This is a proprietary OS and only runs on our controllers, but it’s a top of the line real time OS and if it were more generic it could easily compete with anything else on the market today.
Tanenbaum’s book is still relevant today, though it has a strong unix style slant to it. Unix has some quirks to it, but it’s a good example to learn from. I kinda learned by doing, so I didn’t read Tanenbaum’s book until after I already had some experience at this sort of thing, but it does seem to be a decent book on the subject. I haven’t seen a better reference yet.
As for how difficult it is, the question is how complicated do you want to make it? A DOS style will give you a basic command prompt and can run executables, but DOS didn’t even multitask. You can start with something like that. What exactly do you plan on having available for system calls? Do you plan on being able to access disk type devices, and if so, what sort of file system do you plan on using? Do you plan on supporting networking? A single board computer with no file system and no networking and just some basic system calls and a crude operator interface is pretty simple. Something that multitasks and gives you disk and network access is obviously a lot more complicated. And of course you’ll have to write the drivers for any physical devices that your system supports.
No, he didn’t.
In the beginning, there was AT&T, and they created this thing called Unix. At first, they gave it away free to universities, and it was used to teach computer science classes. Then AT&T figured out that they could actually make money off of this thing, so they stopped giving it away for free. Now the university guys were stuck. They didn’t have anything to teach off of.
So, they (Tanenbaum with some help) created Minix, which was pretty much a clone of Unix but was completely re-written so that it contained no AT&T code at all. Being a full fledged operating system, Minix was pretty useful, so people wanted to keep adding features to it so that it could keep up with AT&T Unix. The university types wanted to keep Minix small and simple, though, so that there was less clutter to get in their way as they dissected the OS for teaching purposes. So for a while, you had these two groups constantly fighting with each other, one group trying to keep Minix small and simple, the other trying to make it big and complex.
Then along came Linus Torvalds. For his masters thesis, he took Minix and turned it into a full AT&T style of unix. He didn’t write his own OS from scratch. A lot of the difficult things like task switching and interrupt handling and most of the kernel functions were already there. This isn’t to say that Linus didn’t do a whole lot of work on it, because he did put quite a bit of effort into it. It took a lot of work to convert Minix into Linux. Now, the university types could stay with Minix, and the people who wanted a real full featured OS could use Linux. Everyone was happy.
The last time I tried to get book copies I was told they weren’t available. I had to download the PDF versions. I hate reading PDF files for this sort of thing. I would much rather have a book.
Ack, that’s a pity. They are quite useful, though nowadays their prime task is to help populate my bookshelf. I was really surprised that I could order them for free, even though I wanted them only for hobby use.
[joking]Competing with the commerical real-time OSes on the market today? That’s a mighty low bar. [/joking]
You do hit on a good point. As an RTOS gets more generic and general purpose, it picks up more baggage and become less bullet-proof. System calls become more complex and take more time. They contain more dead code. Context switches take longer.
But, commercial RTOSes are a current fad and I can also see the rationale of not having to re-invent the wheel (or proprietary RTOS) for every new board.
This is completely false. There wasn’t one line of Minix code in Linux, and never was. The Minix license did not allow redistribution of source code. The original distribution of Linux was 100% written from scratch by Linus.
Not a whole OS, but one of my assignments in CS school was to implement a page-swapper on a simulated device. I found it a pretty fun assignment, but I ended up dropping out before we got to anything more complex along those lines.
I stand corrected. In his first ever public announcement about Linux, Linus specifically mentions that it is 100 percent free of Minix code.
I wasn’t completely off base, though. Linus did his original development under Minix, and Linux originally only supported the Minix file system. This wasn’t because he started with Minix source though (as I thought), but because he developed on a Minix system and his disk was formatted with the Minix file system, so that’s what he worked with.
I was wrong, but at least I can see how I got confused. Oh well.