Reputation: 1045
According to Wikipedia an execution model is
part of the language specification, and is implemented as part of the language implementation.
It further defines that
the order of execution may be chosen statically [...] but a small portion must be chosen dynamically, as execution proceeds.
[...] The static choices are most often implemented inside a compiler, in which case the order of work is represented by the order in which instructions are placed into the executable binary. The dynamic choices would then be implemented inside the language's runtime system.
The runtime system may be a library, which is called by instructions inserted by the compiler, or the runtime system may be embedded into the executable directly, such as by inserting branch instructions, which make dynamic choices about which work to perform next.
Wikipedia specifies the runtime system as
any behavior that is not directly the work of a program is runtime system behavior. This definition includes as part of the runtime system things such as putting parameters onto the stack before a function call, the behavior of disk I/O, and parallel execution related behaviors.
which additionally
is also the gateway by which a running program interacts with the runtime environment, which contains not only state values that are accessible during program execution, but also active entities that can be interacted with during program execution like disk drives and people, via keyboards.
It further states that,
Higher-level behaviors implemented by a runtime system may include tasks such as drawing text on the screen or making an Internet connection.
It is often the case that an OS provide these kinds of behaviors as well [...] The runtime system is implemented as an abstraction layer that translates the invocation of the runtime system into an invocation of the operating system. This hides the complexity or variations in the services offered by different operating systems. [which basically are system calls for me, regarding the Linux Kernel]
This also implies that the OS kernel can itself be viewed as a runtime system, and that the set of OS calls that invoke OS behaviors may be viewed as interactions with a runtime system.
I understand that there has to be some kind of runtime environment, like the Linux Kernel, loading the compiled executable into memory, starting the process, allowing sub-threads and stuff like this. The kernel itself is written in C
and can not be viewed as the "runtime system" of the C
language. However, it provides functions like malloc()
and free()
which are essential parts of the runtime system.
Q So what exactly is the runtime system of C
? Is there any, not blurry, definition of it ? Is it a mixture of a self-standing kernel + Compiler ?
Upvotes: 3
Views: 2035
Reputation: 213328
The line between "runtime" and "standard library" is blurry and not really agreed upon. "Kernel" is usually a harder line, between privileged code and non-privileged code. The way different parts are labeled will also differ from platform to platform, so it's not really possible to give a general answer.
However, you can answer the question somewhat for particular systems. For example, here is how it would work on a typical Linux system with a GNU toolchain:
The program code, compiled as a bunch of *.o
files and linked into an executable.
The "C runtime library". This is available as a few extra *.o
files (crt1.o
, crti.o
, crtn.o
) which the compiler implicitly links into your program. This library rather small, and only really does two things. It provides _start
, which loads argc
and argv
, calls main
, and calls exit
when main
returns (sort of). The library also calls global constructors and destructors--which don't usually exist in C, but you can create them using language extensions.
The "C standard library". This is available as a monster *.so
or *.a
library which the compiler implicitly links into your program. It implements pure functions like strcpy
and atoi
, as well as more complicated systems like malloc
and free
, and also provides a set of system calls which can be thin wrappers around Linux syscalls like open
, more complicated wrappers around more generic syscalls like fork
and clone
, or can be VDSO-accelerated like gettimeofday
and clock_gettime
.
This is not a definitive answer to "what is the C runtime" because there is not really a strict definition of "runtime" to begin with. However, there is a library which is called the "C runtime library" on Linux, and similar libraries exist on other systems. You can often read the source code directly if you search for it, or you can disassemble it (it's not very long).
This will all change on some platforms, like embedded platforms, where you might have just a big "standard library" which has everything you need, and no kernel.
Upvotes: 5
Reputation: 61986
No, the kernel does not provide functions like malloc()
and free()
, though these functions do ultimately rely on memory provided by the kernel.
The runtime system of C is the C runtime libraries, which are provided by the vendor of your compiler. When you #include <stdio.h>
or anything like that, you are specifying that you want to use functionality from these libraries. If you look carefully at the link step of your make process, you will see that the object files produced by your compiler are liked together with the C runtime libraries, (also known as the "C runtime" for short,) to produce your executable.
Then, later, the OS will load and execute your program, but the OS has no idea, nor does it care, which language your program was written in, and what kind of runtime support it is making use of.
Upvotes: 0