2

I have a very naive question here, and I would like you to correct me on whatever wrong concepts I put out here. The question is as follows:

I have ubuntu installed on my machine, now I write a helloWorld.c program in C language. Now, on the operating system I have a compiler installed, when I execute my helloWorld.c program, the OS schedules the compiler and that basically compiles my code into machine code, which eventually, I execute.

Now my kernel code is written in C, then how does my machine interprets that code? Say my kernel code is helloWorld.c, now would not I require any compiler, to compile this code. Also, if I hardcode a compiler in maybe ROM or something, then what language is it written in? Assembly language?

Let me know if I have made myself clear with the problem.

Thanks.

EDIT: By kernel code I mean, the code for operating system. Operating System code. I guess it is written in C right?

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • 1
    A "hello world" program is not "kernel code". A [kernel](https://en.wikipedia.org/wiki/Kernel_%28computing%29) is part of the operating system. (Depending on context, "operating system" can even be a synonym for "kernel".) – Wyzard Oct 14 '12 at 15:24
  • do you mean how Kernel code is actually working? – Aniket Inge Oct 14 '12 at 15:24
  • You keep referring to this 'kernel code' which you don't really understand - neither do I, this is not an existing term. Please explain what is the thing you call 'kernel code'. Is it your program? Is it C code in general? Assembly? The OS or the kernel itself? Or what? –  Oct 14 '12 at 15:26
  • @Wyzard I meant, let us just say I can encapsulate the entire kernel code into some random file `helloWorld.c`. Then how does this C written code be converted to the assembly code understood by the machine. – Kraken Oct 14 '12 at 15:27
  • Kernel is generally core OS code, it's already compiled (in general) and you didn't write a byte of it in your scenario, you just called some of it. – Tony Hopkinson Oct 14 '12 at 15:27
  • @Kraken, with a compiler, just like any other program. Assuming you're talking about actual OS kernels: the Linux kernel, for example, is written in C and compiled with GCC. (There are a few small parts written in assembly, for things that can't be done in C.) – Wyzard Oct 14 '12 at 15:29
  • Ah, while in general all Kernel code is assembly, not all assembly code is Kernel code. It's your C Compiler that converts your C code in to assembly. How is more of twenty volume encyclopedia, than an SO Answer. Before you could get your head round that, you need to pick up assembly itself, and Parsing theory. – Tony Hopkinson Oct 14 '12 at 15:32
  • @Wyzard I am a little overwhelmed with so many responses, the OS kernel; written in C, it is compiled with GCC? but is not there any platform required; for GCC to run on, Like for my custom program, to run GCC; i already have OS installed, I understand that I call the compiler, but compiler is scheduled by the OS only right? Then say on the start of my machine, how is the C written code for OS kernel, is interpreted by the machine. MAN I am confused! – Kraken Oct 14 '12 at 15:32
  • @Kraken That's called cross-compiling. For example, the first Windows was compiled on BSD. –  Oct 14 '12 at 15:45

3 Answers3

14

I think the crux of your question is a kind of chicken-n-egg problem for compilers.

  1. C-code requires a compiler to build it.
  2. A C compiler itself is written in C
  3. So how did we build the compiler in the first place?

Nowadays we have compilers available. So when one develops a newer version of a compiler (say gcc4.7) written in C, then one first uses an older version of the compiler to build it. i.e. gcc4.6(which already exists) will be used to compile the code and generate the latest version i.e. gcc4.7.

This can be traced all the way back to the first compiler ever which as you rightly mention would have required extensive amount of coding in assembly.

You might want to take a look at this discussion on the bootstrapping & history of the 1st compiler.


UPDATE :

Also a fairly common way to get the "first" OS+compiler running is cross-compilation. Here is a nice description of what all one needs to and how one goes about it. In engineering circles this process is called "porting" an OS to a specific device/architecture.

Essentially the first bare-bones one needs to have :

  1. kernel
  2. libc
  3. compiler
  4. shell
  5. basic OS utils
Community
  • 1
  • 1
TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
  • And when you compile a new kernel, you're running the compiler on some other, already-compiled kernel. Early kernels, like early compilers, had to be written in assembly. – Wyzard Oct 14 '12 at 15:37
  • @TheCodeArtist Let me ask the doubts again, the OS kernel code is written in C right? Now, as I have already mentioned in my question, say I need to compile this thing, then I am going to need a compiler, but would not I need an OS, for compiler to be scheduled onto the processor. It is more of like a chicken and egg problem. I hope you get what I am trying to say here? – Kraken Oct 14 '12 at 15:40
  • @Wyzard Basically, what really happens when I turn my system on for the very first time(i.e no OS installed on ym system), I have a C written OS kernel Code, and I have an already compiled compiler code, the compiler code gets onto the processor, loads itself with the operating system, and compiles it, once it gets compiled, the binary file is stored somewhere, and from now on, on every booting, this compiled code is loaded only, and executed, and not compiled everytime? Is this how it happens? – Kraken Oct 14 '12 at 15:43
  • You have an OS and a kernel running on your system. Lets consider this an older version as you have code which is more advanced than what the current kernel/OS does. Its just that it is still in code form and not a binary. So what you do is on your system running "old" OS, you use an "old" compiler (both of which exist currently as binaries) and build your C code. this gives your a "new" OS or a "new" compiler depending on what the code you just compiled. Now you reboot your system and select this new kernel/OS and use the new compiler that you just built. – TheCodeArtist Oct 14 '12 at 15:45
  • @TheCodeArtist What happens, when I dont have any OS installed on my system. What happens on the very first booting? When I insert the CD containing , say Ubuntu 12.10; and my laptop, does not have any OS installed on it beforehand. I wont have the "old compiled" and "old OS" to build my new code,in this case, right? – Kraken Oct 14 '12 at 15:48
  • 1
    Now going back to when one did NOT have an OS at all for a specific architecture/device, one would have to have used extensive assembly to get a fairly simple OS+compiler up and running which could then compile more complex OS+compiler. An alternative is to do this on a device of a different architecture which already has an OS+compiler. This is called "cross-compiling". For example a common scenario nowadays is building the linux-kernel for any ARM processor on an x86 PC. This is done using a cross-compiler "toolchain" that runs on x86, takes in C code and outputs binaries that run on ARM. – TheCodeArtist Oct 14 '12 at 15:48
  • @TheCodeArtist Whenever I want to upgrade from, say windows vista -> windows 7-> windows 8, I would just put in the CD, this CD contains the OS kernel code, and is compiled using the already existing compiler+OS on my machine. So, are you saying that this wont be the case , If I buy a brand new laptop, with no OS installde whatsoever? i.e. I will actually have to compile the OS on a different machine, and then use that binary to run on my system? – Kraken Oct 14 '12 at 15:53
  • 2
    @Kraken, the kernel is not compiled when you boot your computer. The kernel is compiled on someone else's computer, and the compiled, ready-to-run file is what's on your Ubuntu install CD. – Wyzard Oct 14 '12 at 15:53
  • @Wyzard Ohh! So I have this ready to run file on the CD, this gets stored on my system during the time of installation, and from then on, whenever I restart my system, this ready to run file is used, i.e. it is loaded into the memory and processed and exeuted? Aah, finally, I can have a clear vision (I guess) :) – Kraken Oct 14 '12 at 15:55
  • Yes. Someone needs to build the code once. That has been done by Microsoft in case of windows and Canonical in case of Ubuntu. And they provide you the ready-to-run binaries in the CD/DVDs that you will install onto your machine. :-) – TheCodeArtist Oct 14 '12 at 15:57
  • @TheCodeArtist I guess I can mark this answer accepted for now. Cheers. :) – Kraken Oct 14 '12 at 15:59
  • @TheCodeArtist I know I should not be asking it here, but when I install the ubnutu from CD, i get all these folders in the file system, I wont find the source code there right? I will have to download the source code explicitly for me to be able to play with it? Just so I know, what exactly is in the src folders i see in the /usr etc? – Kraken Oct 14 '12 at 16:52
  • By the way, just like the first C compiler was written in another language, mutation resulted chicken egg to come from another species of bird. The chicken-n-egg "problem" is not really a problem. – Shahbaz Oct 14 '12 at 16:53
  • @Kraken Yes, you are right. `/usr/src/` contains the relevant kernel header files which were used during building the kernel. These are useful when one wants to simply build a kernel-module for that particular kernel on the system. To obtain the kernel source for various version of Ubuntu, check out https://wiki.ubuntu.com/Kernel/SourceCode. On your PC, type `dmesg` on the console to see the kernel log. This will contain, among other things, the exact version of the linux-kernel you are running listed at the very top. – TheCodeArtist Oct 14 '12 at 16:58
6

The kernel is compiled too, into machine language specific to your processor architecture, same as the resulting helloWorld program.

Someone wrote the kernel in C, as you say, but compiled it on a computer that already has an operating system and is capable of compiling your new kernel.

Once upon a time, a kernel was written by hand in machine-language and then more sophisticated programs were written using that, including editors and compilers that were used to write replacement kernels. The rest was history...

Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
  • 1
    And it's called bootstrapping. –  Oct 14 '12 at 15:30
  • Yes, and just to elaborate, "Bootstrap" meaning that the kernel and supported components have all the faculties to recompile themselves (or even an improved version of themselves) from source code. "Pulling one's self up by one's bootstraps." – Jason Kleban Oct 14 '12 at 15:34
  • @uosaf well, bootstrap means a lot of things :) –  Oct 14 '12 at 15:44
  • And please extend your answer to include cross-compiling an OS, OP doesn't seem to have gotten that either. –  Oct 14 '12 at 15:46
0

An operating system "Kernel" usually runs in MODE0 of the Machine. It has direct access to all the hardware and has more instructions enabled for it. If your HELLO WORLD was to access hardware directly, writing it inside the kernel.. THEN.. First your bootloader would get the binary data from wherever your kernel resides, that will then set up the environment for the operating kernel to execute. That in turn will execute the program for you.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78