C中的运行时系统
Runtime System in C
根据维基百科,execution model 是
part of the language specification, and is implemented as part of the language implementation.
进一步定义
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.
维基百科将 runtime system 指定为
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.
另外
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.
它进一步指出,
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.
我知道必须有某种运行时环境,比如 Linux 内核,将编译后的可执行文件加载到内存中,启动进程,允许子线程和类似的东西。内核本身是用 C
编写的,不能被视为 C
语言的“运行时系统”。但是,它提供了 malloc()
和 free()
等功能,这些功能是运行时系统的重要组成部分。
Q 那么C
的运行系统到底是什么?是否有任何不模糊的定义?它是自立内核+编译器的混合体吗?
不,内核不提供像malloc()
和free()
这样的功能,尽管这些功能最终依赖于内核提供的内存。
C的运行时系统是C运行时库,由编译器厂商提供。当您 #include <stdio.h>
或类似的东西时,您指定要使用这些库中的功能。如果仔细查看 make 过程的 link 步骤,您会发现编译器生成的目标文件与 C 运行时库(也简称为 "C runtime", ) 生成您的可执行文件。
然后,稍后,OS会加载并执行你的程序,但是OS不知道,也不关心你的程序是用什么语言写的,是什么样的它正在使用的运行时支持。
"runtime" 和 "standard library" 之间的界线模糊不清,并没有真正达成一致。 "Kernel" 通常是特权代码和非特权代码之间的强硬界限。不同部分的标记方式也会因平台而异,所以不太可能给出一个通用的答案。
但是,您可以针对特定系统稍微回答这个问题。例如,下面是它在带有 GNU 工具链的典型 Linux 系统上的工作方式:
程序代码,编译为一堆 *.o
文件并链接到可执行文件中。
"C runtime library"。这可以作为一些额外的 *.o
文件(crt1.o
、crti.o
、crtn.o
)使用,编译器隐式链接到您的程序中。这个库相当小,实际上只做两件事。它提供 _start
,加载 argc
和 argv
,调用 main
,并在 main
returns 时调用 exit
(有点像).该库还调用全局构造函数和析构函数——它们在 C 中通常不存在,但您可以使用语言扩展来创建它们。
"C standard library"。这是一个怪物 *.so
或 *.a
库,编译器隐式链接到您的程序中。它实现了像 strcpy
和 atoi
这样的纯函数,以及像 malloc
和 free
这样更复杂的系统,并且还提供了一组系统调用,它们可以是薄包装Linux 系统调用,如 open
,更复杂的包装器围绕更通用的系统调用,如 fork
和 clone
,或者可以像 gettimeofday
和 clock_gettime
一样进行 VDSO 加速].
这不是 "what is the C runtime" 的明确答案,因为 "runtime" 并没有严格的定义。但是, "C runtime library" 在 Linux 上有一个库,其他系统上也存在类似的库。经常搜索一下可以直接看源码,也可以反汇编一下(不是很长)
在某些平台上,这一切都会改变,例如嵌入式平台,您可能只有一个大 "standard library",其中包含您需要的一切,但没有内核。
根据维基百科,execution model 是
part of the language specification, and is implemented as part of the language implementation.
进一步定义
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.
维基百科将 runtime system 指定为
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.
另外
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.
它进一步指出,
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.
我知道必须有某种运行时环境,比如 Linux 内核,将编译后的可执行文件加载到内存中,启动进程,允许子线程和类似的东西。内核本身是用 C
编写的,不能被视为 C
语言的“运行时系统”。但是,它提供了 malloc()
和 free()
等功能,这些功能是运行时系统的重要组成部分。
Q 那么C
的运行系统到底是什么?是否有任何不模糊的定义?它是自立内核+编译器的混合体吗?
不,内核不提供像malloc()
和free()
这样的功能,尽管这些功能最终依赖于内核提供的内存。
C的运行时系统是C运行时库,由编译器厂商提供。当您 #include <stdio.h>
或类似的东西时,您指定要使用这些库中的功能。如果仔细查看 make 过程的 link 步骤,您会发现编译器生成的目标文件与 C 运行时库(也简称为 "C runtime", ) 生成您的可执行文件。
然后,稍后,OS会加载并执行你的程序,但是OS不知道,也不关心你的程序是用什么语言写的,是什么样的它正在使用的运行时支持。
"runtime" 和 "standard library" 之间的界线模糊不清,并没有真正达成一致。 "Kernel" 通常是特权代码和非特权代码之间的强硬界限。不同部分的标记方式也会因平台而异,所以不太可能给出一个通用的答案。
但是,您可以针对特定系统稍微回答这个问题。例如,下面是它在带有 GNU 工具链的典型 Linux 系统上的工作方式:
程序代码,编译为一堆
*.o
文件并链接到可执行文件中。"C runtime library"。这可以作为一些额外的
*.o
文件(crt1.o
、crti.o
、crtn.o
)使用,编译器隐式链接到您的程序中。这个库相当小,实际上只做两件事。它提供_start
,加载argc
和argv
,调用main
,并在main
returns 时调用exit
(有点像).该库还调用全局构造函数和析构函数——它们在 C 中通常不存在,但您可以使用语言扩展来创建它们。"C standard library"。这是一个怪物
*.so
或*.a
库,编译器隐式链接到您的程序中。它实现了像strcpy
和atoi
这样的纯函数,以及像malloc
和free
这样更复杂的系统,并且还提供了一组系统调用,它们可以是薄包装Linux 系统调用,如open
,更复杂的包装器围绕更通用的系统调用,如fork
和clone
,或者可以像gettimeofday
和clock_gettime
一样进行 VDSO 加速].
这不是 "what is the C runtime" 的明确答案,因为 "runtime" 并没有严格的定义。但是, "C runtime library" 在 Linux 上有一个库,其他系统上也存在类似的库。经常搜索一下可以直接看源码,也可以反汇编一下(不是很长)
在某些平台上,这一切都会改变,例如嵌入式平台,您可能只有一个大 "standard library",其中包含您需要的一切,但没有内核。