gpt4 book ai didi

c - C中的运行时系统

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:50 25 4
gpt4 key购买 nike

根据维基百科 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的运行时系统到底是什么?是否有任何不模糊的定义?它是自立内核 + 编译器的混合体吗?

最佳答案

“运行时”和“标准库”之间的界线很模糊,而且没有真正达成一致。 “内核”通常是特权代码和非特权代码之间的硬线。不同部分的标记方式也会因平台而异,因此不太可能给出一个通用的答案。

但是,您可以针对特定系统稍微回答这个问题。例如,以下是它在带有 GNU 工具链的典型 Linux 系统上的工作方式:

  • 程序代码,编译为一堆 *.o 文件并链接到可执行文件中。

  • “C 运行时库”。这可以作为一些额外的 *.o 文件(crt1.ocrti.ocrtn.o) 编译器隐式链接到您的程序中。这个库相当小,实际上只做两件事。它提供了_start,加载argcargv,调用main,调用exitmain 返回时(某种程度上)。该库还调用全局构造函数和析构函数——它们在 C 中通常不存在,但您可以使用语言扩展来创建它们。

  • “C 标准库”。这可以作为怪物 *.so*.a 库使用,编译器隐式链接到您的程序中。它实现了像 strcpyatoi 这样的纯函数,以及像 mallocfree 这样更复杂的系统,而且提供一组系统调用,它们可以是像 open 这样的 Linux 系统调用的简单包装,也可以是像 forkclone 这样更通用的系统调用的更复杂的包装,或者可以像 gettimeofdayclock_gettime 一样进行 VDSO 加速。

这不是“什么是 C 运行时”的明确答案,因为“运行时”并没有真正严格的定义。但是,Linux 上有一个称为“C 运行时库”的库,其他系统上也存在类似的库。经常搜索可以直接看源码,也可以反汇编(不是很长)。

这一切在某些平台上都会发生变化,例如嵌入式平台,您可能只有一个大型“标准库”,其中包含您需要的一切,但没有内核。

关于c - C中的运行时系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com