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?
10
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
}