1

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.

4
  • maybe is better to ask here: programmers.stackexchange.com Oct 24, 2012 at 4:38
  • @PlazmotechBinary I wasn't actually talking specifically about this question, I was talking about the 20 or so previous questions you've asked with no accepted answers. Oct 24, 2012 at 4:39
  • @jozefg I've gone through the questions, turns out there are a few I actually remember using, and I voted using the little up-arrow but forgot to check, because of the speed of their responses, and it says, "please check back in 20 mins" or something, and then I start programming again and forget about it. Derp. Oct 24, 2012 at 4:41
  • Alright, And I see you've improved that, thank you! Oct 24, 2012 at 4:43

3 Answers 3

1

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.

1
  • Oooh! Nice! Thanks for the response! I will give it a read when I come back from school. Oct 24, 2012 at 14:42
0

You need an OS to do things like virtualize memory and arbitrate access to the hardware. Because the OS has full access to things like memory and the hardware, it doesn't need an OS to run. The services that most programs get from an OS, the OS itself either has to provide itself or has to get from the hardware.

2
  • Right, but say, if I where to "write" and os, how would I go about having to run the said script without having a place to run it in? How would I run a script without said OS. Oct 24, 2012 at 4:44
  • I don't follow. Are you trying to write a script or an OS? If you're writing an OS, your equivalent of the OS -- the thing that provides the services you need -- is the hardware. Hardware -> OS -> Applications Oct 24, 2012 at 4:46
0

What runs any software is some kind of a CPU, a piece of electronic circuitry, hardware.

Whether the software is an OS or some other kind of program, the CPU doesn't care. It just runs it so long as it is possible (e.g. until said software causes an irrecoverable error forcing the CPU to enter some odd state or simply reset).

When powered on, the CPU starts executing whatever code it finds at a certain location in memory. That location can be either hardwired in the CPU or configured by external to the CPU circuitry or even programmed by software. Which one it is depends on the CPU and how the entire device is designed.

Not all programs need an OS to run on a CPU. An OS is a good example of such a program. If every OS needed another OS how would you stop this infinite recursion? :) But it's not the only one.

Many electronic devices, especially very simple ones like a digital clock, don't have any OS in them.

About the only time an OS runs atop of another OS, is when you have virtualization. But I'm not going to go there in this answer.

An OS is only needed either for its basic functions like scheduling, thread/process synchronization, memory allocation, etc or to do all or most of device I/O itself and hide the hardware peculiarities from programs running in the OS, IOW, to allow portable programs. You write print 123 in Python and it prints 123 everywhere, in any OS with which Python is compatible (=for which it is available), irrespective of the display, its resolution and many other differences that can be there on different computers and in different OSes. If general-purpose OSes had not provided some common functionality (and some more or less common API for it) such as console and file I/O and memory management, Python would not have been available for them and those OSes would not have been general-purpose in the first place.

An OS kernel is the core of an OS. It does most of the low-level and dirty work, dealing with:

  • interrupt handling
  • thread/process scheduling and synchronization
  • memory management
  • things alike

Sometimes that's enough of OS functionality and in such a case there isn't any difference between an OS and a kernel, they are the same thing here.

If, OTOH, more stuff is needed, e.g. much flexibility, support for different devices, some code to manage all that and a bunch of special drivers for file system, for network and TCP/IP stack, etc, then a simple (perhaps, only relatively simple) kernel alone isn't sufficient, there needs to be more stuff around it and that's where you start differentiating between an OS and a kernel and arrive at OS = kernel + extras. The text editor you're using, even if it came with your OS, is not quite one of those extras, it's a regular program, which needed not to be bundled with the OS, but for convenience it could be.

UNIX is one example of an OS. There are many more, most notably Linux and Windows.

You should get yourself some book on computer architecture and organization to learn how computers work in general. What it is inside there, how it's put together, what's happening under the hood, etc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.