8

I'm interested in how operating systems work. I've been reading some articles about Linux and seem to understand how it all generally comes together, but I feel like there's a chicken and egg dilemma when it comes to constructing an operating system.

Since Linux is written in C (and Assembly), which needs a compiler to be compiled, how does one actually compile an operating system for the first time? Do you first write a boot loader out of your existing OS? Or do you do something else? Do you have to dual-boot for the first time? Or you need two machines? What did Linus first end up with when he started his project? What did his very first result look like?

What's the very first thing you need to design and write when you're programming an Operating System?

Leo
  • 191
  • 1
  • 5
  • 3
    You might be interested in reading this: http://en.wikipedia.org/wiki/Bootstrapping_(compilers) – Cam Mar 18 '13 at 02:47
  • 3
    Also potentially interesting: http://en.wikipedia.org/wiki/Machine_code. This is how real programmers coded before assembly =) – beatgammit Mar 18 '13 at 02:49
  • 3
    The [OSDev Wiki](http://wiki.osdev.org/Main_Page) has quite a bit of information on how operating systems are built. – Jon Purdy Mar 18 '13 at 02:49
  • Thank you! Haven't heard of Bootstrapping (as such) before and OSDev is a gem! –  Mar 18 '13 at 02:52
  • 3
    You might be interested in the Linux From Scratch (http://www.linuxfromscratch.org/lfs/) project. It is basically a step-by-step process to go about building and setting up your own Linux OS. You start with an existing OS, using the tools on the OS, you build a new set of tools. With the new set of tools, you then build a new set of tools again (this is called cross-compiling, building tools that are independent of the host OS). – Alex Mar 18 '13 at 02:53
  • This is way too broad question. Way too broad. –  Mar 18 '13 at 02:54
  • This question (as it stands) is both way too broad and not constructive ("Give me an article on the subject"). There's really not much you could do to have it stay on Stack Overflow, but with a bit of improvement in the question, it'd be good to migrate to programmers. – George Stocker Mar 18 '13 at 02:57
  • Well, I've tried to ask a number of basic questions (for example, what might the most primitive OS look like and do), but I understand that it's still too broad and am sorry. Maybe it can be considered as a set of relatively minor questions, but then that might be against the rules as well. It's just that I feel a book won't necessarily address these questions alone, and I am ill-equipped to understand the grand scheme of things. Nevertheless, SO is so good that I've managed to get some really great answers already and tons of valuable material to try and learn. For that I am grateful! – Leo Mar 18 '13 at 03:02
  • @Gleb you are asking many questions in this question. Could you modify it to ask one question at a time? A question that asks many questions is difficult to answer correctly - someone answers parts A, and C but is unsure of or doesn't address part B, while someone else answers A and B, but ignores C - neither answer answers all the questions. –  Mar 18 '13 at 03:04
  • 1
    I have to say, maybe there just isn't an answer to a basic operating system saying "Hello, World!". I've browsed through some of the duplicates and have considered all of the recommendations (mostly books, obviously). Maybe the answer is that it takes years and years of experience and knowledge to even get to what I'm asking about. Sorry if I was a bit naive. Just wanted an "in a nutshell" explanation in English of how it might be done, just to get my brain around the subject. But, as I said, I've learned more than enough from the answers provided to get me started, so thank you! – Leo Mar 18 '13 at 03:31

1 Answers1

3

This happens all the time in embedded systems. Generally you compile on an existing OS targetting whatever processor it is you are doing the OS for. This code then needs to be put in a form that the processor can load, either in a EPROM, or you make a "Bootloader" which has the job of being the first thing the processor will execute then it will work out how to load the OS off a medium such as a harddrive.

On the PC architecture there is a "BIOS" which provides basic access to various hardware like screen and harddrives, etc. It doest the "Bootloading" for you. So most PC based OS's like linux and windows use this to get themselves going using a "Boot Sector" on something that provides a file system.

The simplest OS is often just a task scheduler (multi threading), and a simple hardware abstraction layer. For the task schedule you will generally have a timer source that triggers a interrupt

void MyTaskSchedulerInterruptHandler()
{
   SaveStateOfCurrentTask(); // save all the CPU registers
   FindNextTaskToExecute();  // using tables of tasks currently being run, 
                             //find one to run next
   ExecuteTask();  // load all the CPU registers for this task
   // return from interrupt will then make the CPU start on the loaded task
}


main()
{    
    SetupInterruptVectors();
    InitializeHardware();
    StartSystemTasks();
   //never gets here
}
Keith Nicholas
  • 690
  • 1
  • 6
  • 12