I’m really curious right now. I’m a Python programmer, and this question just boggled me: You write an OS. How do you run it? It has to be run somehow, and that way is within another OS?
How can an application run without being in an OS? How do you tell the computer to run, say, C, and execute these commands to the screen, if it doesn’t have an OS to run in?
Does it have to do with a UNIX kernel? If so, what is a unix kernel, or a kernel in general?
I’m sure OSes are more complicated than that, but how does it work? It would be really brilliant to know this!
Thanks.
You can indeed write a program without an OS. Indeed, on your PC there is already a program that runs without an OS before your OS boots up. There are two in fact. The first is your BIOS.
The IBM PC architecture is one of a family of architectures that employs BIOSes to start up the computer. Not all architectures have BIOSes. The iPhone for example boots directly into a bootloader. In fact, most “modern” architectures don’t have BIOSes but boot directly into a bootloader. It’s actually conceptually simpler this way.
The fact that PCs need BIOSes is merely a historical legacy. The original IBM BIOS was in fact a basic bootloader for loading DOS. Modern BIOSes are still bootloaders.. that load bootloaders.
Most PC BIOSes are proprietary. They allow manufacturers to initialize custom/proprietary hardware before passing control to bootloaders. This makes it possible to write bootloaders without having to worry about weather the OS image is on a flash drive, a USB thumbdrive, an SD card, a magnetic disk, on DVD etc. The boot loader simply sees a disk that have been initialized by the BIOS.
The next stage of boot up is the bootloader. The reason why the IBM PC architecture requires a bootloader is that the BIOS is usually designed to set up the CPU to run DOS. Modern OSes require the CPU to be configured slightly differently. Also, modern OS kernels (a kernel is the actual core executable code of the OS doing things like manage memory, cpu etc) tend to be large beasts, often larger than 4MB in size which is much larger than what most BIOSes are designed to load. So the BIOS loads a small bootloader which in turn loads the actual OS. Again, this is only necessary due to decisions made by IBM in the 1980s when they designed the original PC architecture. Modern archictectures like the iPhone or PS3 don’t do this. They boot directly into the bootloader.
The bootloader is necessary to solve the chicken and egg problem: to load the OS you need to read from disk. To read from disk you need to use a device driver which is loaded by the OS. To break this circular dependency people write bootloaders which are basically very simple OSes that is designed to run only one program (the OS) and understand how to read from disks (or SD card, or the network etc.).
Which brings us to the part that answers your question. If you’ve written an OS, how do you load it? You load it by configuring your bootloader. Windows comes with a bootloader that’s not too flexible. It understands how to load Windows but that’s about it. You can install open source bootloaders like Grub or Burg which understand how to load other OSes as well.
The fundamental concepts involved in booting a machine is actually simple. It’s the details that’s scary. But if you’re really interested in how OSes work learning it is very rewarding in the end. Also, if you’re interested in this stuff I’d suggest looking around at other architectures apart from the IBM PC like Mac hardware which uses EFI instead of a traditional BIOS or Linksys routers which boot into Linux directly from a bootloader or embedded platforms like Arduinos that run a single program you compile directly on the CPU without an OS.