3

I'm programming for quite some years now, but there's one thing that I never understood exactly:

There are 2 types of programming languages. Compiled ones and interpreted ones, while compiled ones first need to be to compiled to a interpreted language, before they can be executed.

For example, C/C++ needs to be compiled to machine language first, before it can be executed.

And here comes my question: Who does actually run a compiled C/C++ Windows/Linux program? Is it interpreded by the CPU directly, or does the OS Kernel execute it and pass the commands to the CPU?

And if the CPU executes it directly, how can the Windows Kernel then deny some calls? Otherwise, does it mean that the Kernel understands machine language as well, or do non-kernel C/C++ programs not compile to machine language?

Van Coding
  • 24,244
  • 24
  • 88
  • 132
  • @FelicePollano Yes, I know. But I ask from a architectural point of view, where things can run on top of each other ;) – Van Coding Apr 28 '14 at 10:33
  • You might like the [wikipedia article on protection ring](http://en.wikipedia.org/wiki/Ring_%28computer_security%29). – pmg Apr 28 '14 at 10:34
  • The compiled program contains machine language directly run by the cpu. Before running a program, the kernel puts the cpu into a state where certain types of instructions (like the stuff that maps physical memory to virtual memory) from running. It then jumps to the program an lets it run. – rcgldr Apr 28 '14 at 10:35
  • Good question, but let me just say that a comprehensive introductory text book will contain the answer, well explained. The classic being the [Tanenbaum](http://en.wikipedia.org/wiki/Modern_Operating_Systems). Also, it’s unorthodox to speak of compiled languages as being compiled “to an interpreted language” (although it’s true that the machine instructions are ultimately interpreted by the hardware). – Konrad Rudolph Apr 28 '14 at 10:35
  • It's a grey area. MSVC has, or had, an option to generate interpretative code. I'm not aware there's anything in the ISO standards that prohibits or prescribes either strategy. However certainly practically all C++ implementations generate code that execute on the hardware. – user207421 Apr 28 '14 at 10:38
  • "while compiled ones first need to be to compiled to a interpreted language, before they can be executed." This statement is plain wrong because compiled programs are directly executed by the CPU whereas interpreted ones are interpreted by an interpretor. Latter one is usually a compiled program. – Jabberwocky Apr 28 '14 at 10:39
  • @MichaelWalz While you're right, one could still say that the CPU interprets the machine language, otherwise it wouldn't understand what to do. I've just simplified it a bit. I mean, one could also say, that the CPU is only an machine-language interpreter program written in the atomic-language. – Van Coding Apr 28 '14 at 10:42
  • Replies and an answer are good.. Anyway, if you know more advanced information about these topic, How about trying writing your own simple OS? It may give you a lot of knowledge. I suggest visit [here](http://wiki.osdev.org/Main_Page) if you want. – ikh Apr 28 '14 at 10:51
  • You seem to harbor a misconception, there is no CPU and kernel, the CPU can execute in "kernel mode" which is more trusted and unsafe. You can even write kernels in interpreted languages, as foolish as this may seem. – dtech Apr 28 '14 at 11:29
  • `C/C++ needs to be compiled to machine language first, before it can be executed` Why? There are C interpreters out there that do not require this. Even if there weren't, it's not like there's something fundamental about the language that requires it to be turned into machine code before it can be run. Of course, this is the normal way C and C++ are used. But I don't think this is a requirement of the languages. – Brandin Apr 28 '14 at 21:13

1 Answers1

5

The program runs on the CPU. The CPU has different levels of privilege, so called "privileged instructions" may only be executed by the kernel - that's how security is enforced by the operating system.

Zebra North
  • 11,412
  • 7
  • 37
  • 49
  • 2
    X86 has 4 levels, called rings. The kernel and portions of some device drivers run at the highest level, called ring 0. Applications generally run at the lowest level, called ring 3. – rcgldr Apr 28 '14 at 10:36
  • The PDP-11 had seven in the 1970s. – user207421 Apr 28 '14 at 10:38