什么是“运行时”?

发布于 2024-09-27 04:32:40 字数 1027 浏览 0 评论 0原文

我听说过“C 运行时”、“Visual C++ 2008 运行时”、“.NET 公共语言运行时”等。

  • 运行时”到底是什么?
  • 它是由什么制成的?
  • 它如何与我的代码交互?或者更准确地说,它是如何控制我的代码的?

在Linux上编写汇编语言时,我可以使用INT指令来进行系统调用。那么,运行时只是一堆预制函数,将低级函数包装成更抽象和高级的函数吗?但这看起来不是更像是库的定义,而不是运行时的定义吗?

“运行时”和“运行时库”是两个不同的东西吗?

ADD 1

这些天,我在想也许运行时与所谓的虚拟机有一些共同点,比如JVM。以下是引发这种想法的引文:

这个编译过程非常复杂,需要分解 几个抽象层,这些通常涉及三个 翻译器:编译器、虚拟机实现和 汇编器。 --- 计算系统的要素(简介、 通往硬件王国的道路)

ADD 2

本书 专家 C 编程:深层 C 秘密。第 6 章运行时数据结构是对这个问题的有用参考。

ADD 3 - 7:31 AM 2/28/2021

以下是我在了解了一些有关处理器设计的知识后的一些观点。整个计算机只是多个层次的抽象。它从基本晶体管一直到正在运行的程序。对于任何N 级 抽象,其运行时 是其下面的直接N-1 级 抽象。是上帝给了我们 0 级抽象。

I have heard about things like "C Runtime", "Visual C++ 2008 Runtime", ".NET Common Language Runtime", etc.

  • What is "runtime" exactly?
  • What is it made of?
  • How does it interact with my code? Or maybe more precisely, how is my code controlled by it?

When coding assembly language on Linux, I could use the INT instruction to make the system call. So, is the runtime nothing but a bunch of pre-fabricated functions that wrap the low level function into more abstract and high level functions? But doesn't this seem more like the definition for the library, not for the runtime?

Are "runtime" and "runtime library" two different things?

ADD 1

These days, I am thinking maybe Runtime has something in common with the so called Virtual Machine, such as JVM. Here's the quotation that leads to such thought:

This compilation process is sufficiently complex to be broken into
several layers of abstraction, and these usually involve three
translators: a compiler, a virtual machine implementation, and an
assembler. --- The Elements of Computing Systems (Introduction,
The Road Down To Hardware Land)

ADD 2

The book Expert C Programming: Deep C Secrets. Chapter 6 Runtime Data Structures is an useful reference to this question.

ADD 3 - 7:31 AM 2/28/2021

Here's some of my perspective after getting some knowledge about processor design. The whole computer thing is just multiple levels of abstraction. It goes from elementary transistors all the way up to the running program. For any level N of abstraction, its runtime is the immediate level N-1 of abstraction that goes below it. And it is God that give us the level 0 of abstraction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

木有鱼丸 2024-10-04 04:32:41

我对这里的其他答案并不感兴趣;它们对我来说太模糊和抽象了。我在故事中思考更多。这是我尝试更好的答案。

BASIC 示例

假设现在是 1985 年,您在 Apple II 上编写了一个简短的 BASIC 程序:

] 10 PRINT "HELLO WORLD!"
] 20 GOTO 10

到目前为止,您的程序只是源代码。它没有运行,我们会说它不涉及“运行时”。

但现在我运行它:

] RUN

它实际上运行得怎么样?它怎么知道如何将PRINT中的字符串参数发送到物理屏幕呢?我当然没有在代码中提供任何系统信息,并且 PRINT 本身对我的系统一无所知。

相反,RUN 本身实际上是一个程序——它的代码告诉它如何解析我的代码、如何执行它以及如何向计算机操作系统发送任何相关请求。 RUN 程序提供了“运行时”环境,充当操作系统和源代码之间的一层。操作系统本身就是这个“运行时”的一部分,但当我们谈论像 RUN 程序这样的“运行时”时,我们通常并不意味着包括它。

编译和运行时的类型

编译的二进制语言

在某些语言中,必须先编译源代码才能运行。有些语言将您的代码编译成机器语言——它可以直接由您的操作系统运行。这种编译后的代码通常称为“二进制”(即使所有其他类型的文件也是二进制的:)。

在这种情况下,仍然涉及最小的“运行时”——但该运行时是由操作系统本身提供的。编译步骤意味着在代码运行之前会检测到许多会导致程序崩溃的语句。

C 就是这样一种语言;当你运行 C 程序时,它完全能够向操作系统发送非法请求(例如,“让我控制计算机上的所有内存,然后将其全部删除”)。如果遇到非法请求,操作系统通常只会终止您的程序,而不告诉您原因,并将该程序被终止时内存的内容转储到 .dump 文件中,这非常困难理解。但有时你的代码中有一个命令是一个非常糟糕的主意,但操作系统并不认为它是非法的,例如“擦除该程序正在使用的随机内存位”;这可能会导致非常奇怪的问题,很难弄清真相。

字节码语言

其他语言(例如Java、Python)将您的代码编译成操作系统无法直接读取的语言,但特定的运行时程序可以读取您编译的代码。这种编译后的代码通常称为“字节码”。

该运行时程序越复杂,它可以在您的代码未包含的方面(甚至在您使用的库中)执行更多额外的操作 - 例如,Java 运行时环境(“JRE”)和 Python 运行时环境可以跟踪不再需要的内存分配,并告诉操作系统可以安全地将该内存重用于其他用途,并且它可以捕获代码尝试向操作系统发送非法请求的情况,然后退出有可读错误。

所有这些开销使它们比编译的二进制语言慢,但它使运行时强大且灵活;在某些情况下,它甚至可以在开始运行后引入其他代码,而无需重新开始。编译步骤意味着在代码运行之前会检测到许多会导致程序崩溃的语句;强大的运行时可以防止您的代码做愚蠢的事情(例如,您无法“擦除该程序正在使用的随机内存位”)。

脚本语言

还有一些语言根本不预编译您的代码。运行时会完成逐行读取代码、解释代码并执行代码的所有工作。这使得它们比“字节码”语言更慢,但也更灵活;在某些情况下,您甚至可以在源代码运行时修改它!尽管这也意味着您的代码中可能存在完全非法的语句,并且它可能会出现在您的生产代码中而不引起注意,直到有一天它运行并导致崩溃。

这些通常被称为“脚本”语言;它们包括 Javascript、Perl 和 PHP。其中一些提供了您可以选择编译代码以提高其速度的情况(例如,Javascript 的 WebAssembly 项目)。因此,Javascript 可以允许网站上的用户查看正在运行的确切代码,因为他们的浏览器提供了运行时。

这种灵活性还允许在运行时环境中进行创新,例如node.js,它既是一个代码库,又是一个运行时环境,可以将您的Javascript代码作为服务器运行,这与您尝试在一个浏览器。

I'm not crazy about the other answers here; they're too vague and abstract for me. I think more in stories. Here's my attempt at a better answer.

a BASIC example

Let's say it's 1985 and you write a short BASIC program on an Apple II:

] 10 PRINT "HELLO WORLD!"
] 20 GOTO 10

So far, your program is just source code. It's not running, and we would say there is no "runtime" involved with it.

But now I run it:

] RUN

How is it actually running? How does it know how to send the string parameter from PRINT to the physical screen? I certainly didn't provide any system information in my code, and PRINT itself doesn't know anything about my system.

Instead, RUN is actually a program itself -- its code tells it how to parse my code, how to execute it, and how to send any relevant requests to the computer's operating system. The RUN program provides the "runtime" environment that acts as a layer between the operating system and my source code. The operating system itself acts as part of this "runtime", but we usually don't mean to include it when we talk about a "runtime" like the RUN program.

Types of compilation and runtime

Compiled binary languages

In some languages, your source code must be compiled before it can be run. Some languages compile your code into machine language -- it can be run by your operating system directly. This compiled code is often called "binary" (even though every other kind of file is also in binary :).

In this case, there is still a minimal "runtime" involved -- but that runtime is provided by the operating system itself. The compile step means that many statements that would cause your program to crash are detected before the code is ever run.

C is one such language; when you run a C program, it's totally able to send illegal requests to the operating system (like, "give me control of all of the memory on the computer, and erase it all"). If an illegal request is hit, usually the OS will just kill your program and not tell you why, and dump the contents of that program's memory at the time it was killed to a .dump file that's pretty hard to make sense of. But sometimes your code has a command that is a very bad idea, but the OS doesn't consider it illegal, like "erase a random bit of memory this program is using"; that can cause super weird problems that are hard to get to the bottom of.

Bytecode languages

Other languages (e.g. Java, Python) compile your code into a language that the operating system can't read directly, but a specific runtime program can read your compiled code. This compiled code is often called "bytecode".

The more elaborate this runtime program is, the more extra stuff it can do on the side that your code did not include (even in the libraries you use) -- for instance, the Java runtime environment ("JRE") and Python runtime environment can keep track of memory assignments that are no longer needed, and tell the operating system it's safe to reuse that memory for something else, and it can catch situations where your code would try to send an illegal request to the operating system, and instead exit with a readable error.

All of this overhead makes them slower than compiled binary languages, but it makes the runtime powerful and flexible; in some cases, it can even pull in other code after it starts running, without having to start over. The compile step means that many statements that would cause your program to crash are detected before the code is ever run; and the powerful runtime can keep your code from doing stupid things (e.g., you can't "erase a random bit of memory this program is using").

Scripting languages

Still other languages don't precompile your code at all; the runtime does all of the work of reading your code line by line, interpreting it and executing it. This makes them even slower than "bytecode" languages, but also even more flexible; in some cases, you can even fiddle with your source code as it runs! Though it also means that you can have a totally illegal statement in your code, and it could sit there in your production code without drawing attention, until one day it is run and causes a crash.

These are generally called "scripting" languages; they include Javascript, Perl, and PHP. Some of these provide cases where you can choose to compile the code to improve its speed (e.g., Javascript's WebAssembly project). So Javascript can allow users on a website to see the exact code that is running, since their browser is providing the runtime.

This flexibility also allows for innovations in runtime environments, like node.js, which is both a code library and a runtime environment that can run your Javascript code as a server, which involves behaving very differently than if you tried to run the same code on a browser.

羁客 2024-10-04 04:32:41

在我的理解中,运行时正是它的含义——程序运行的时间。您可以说某些事情发生在运行时 / 运行时或编译时。

我认为运行时运行时库应该是(如果不是的话)两个独立的东西。 “C 运行时”对我来说似乎不合适。我称之为“C 运行时库”。

对您其他问题的答复:
我认为术语“运行时”可以扩展到包括程序运行时的环境和上下文,因此:

  • 它由程序运行期间可以称为“环境”的所有内容组成。正在运行,例如其他进程、操作系统和使用的库的状态、其他进程的状态等,
  • 一般意义上它不会与您的代码交互,它只是定义您的代码在什么情况下工作,可以使用什么它在执行过程中。

这个答案在某种程度上只是我的观点,而不是事实或定义。

In my understanding runtime is exactly what it means - the time when the program is run. You can say something happens at runtime / run time or at compile time.

I think runtime and runtime library should be (if they aren't) two separate things. "C runtime" doesn't seem right to me. I call it "C runtime library".

Answers to your other questions:
I think the term runtime can be extended to include also the environment and the context of the program when it is run, so:

  • it consists of everything that can be called "environment" during the time when the program is run, for example other processes, state of the operating system and used libraries, state of other processes, etc
  • it doesn't interact with your code in a general sense, it just defines in what circumstances your code works, what is available to it during execution.

This answer is to some extend just my opinion, not a fact or definition.

陌伤浅笑 2024-10-04 04:32:41

马特·鲍尔回答正确。我想通过例子来说明这一点。

考虑运行一个用Turbo-Borland C/C++(1991年版本3.1)编译器编译的程序,并让它在32位版本的Windows(如Win 98/2000等)下运行。

它是一个16位编译器。您将看到所有程序都有 16 位指针。当你的操作系统是 32 位时为什么会这样?因为你的编译器设置了16位的执行环境,并且32位版本的操作系统支持它。

通常所说的 JRE(Java 运行时环境)为 Java 程序提供了执行时可能需要的所有资源。

实际上,运行环境是虚拟机思想的大脑产物。虚拟机实现硬件与程序可能需要执行的内容之间的原始接口。运行时环境采用这些接口并呈现给程序员使用。编译器开发人员需要这些设施为其程序提供执行环境。

Matt Ball answered it correctly. I would say about it with examples.

Consider running a program compiled in Turbo-Borland C/C++ (version 3.1 from the year 1991) compiler and let it run under a 32-bit version of windows like Win 98/2000 etc.

It's a 16-bit compiler. And you will see all your programs have 16-bit pointers. Why is it so when your OS is 32bit? Because your compiler has set up the execution environment of 16 bit and the 32-bit version of OS supported it.

What is commonly called as JRE (Java Runtime Environment) provides a Java program with all the resources it may need to execute.

Actually, runtime environment is brain product of idea of Virtual Machines. A virtual machine implements the raw interface between hardware and what a program may need to execute. The runtime environment adopts these interfaces and presents them for the use of the programmer. A compiler developer would need these facilities to provide an execution environment for its programs.

怎樣才叫好 2024-10-04 04:32:41

运行时间正是您的代码生效的地方,您可以看到代码所做的许多重要事情。

运行时负责分配内存、释放内存、使用操作系统的子系统(如文件服务、IO 服务..网络服务等)。

您的代码将被称为“理论工作”,直到您实际运行代码。
运行时是一个朋友,可以帮助实现这一目标。

Run time exactly where your code comes into life and you can see lot of important thing your code do.

Runtime has a responsibility of allocating memory , freeing memory , using operating system's sub system like (File Services, IO Services.. Network Services etc.)

Your code will be called "WORKING IN THEORY" until you practically run your code.
and Runtime is a friend which helps in achiving this.

苦行僧 2024-10-04 04:32:41

运行时可以表示程序生命周期的当前阶段(运行时/编译时/加载时/链接时)
或者它可能意味着运行时库,它形成与执行环境交互的基本低级操作。
或者它可能意味着运行时系统,与执行环境相同。

对于 C 程序,运行时是设置堆栈、堆等的代码,这是 C 环境所期望的要求。它本质上设置了语言所承诺的环境。 (它可能有一个运行时库组件,crt0.lib 或类似的 C 语言)

a runtime could denote the current phase of program life (runtime / compile time / load time / link time)
or it could mean a runtime library, which form the basic low level actions that interface with the execution environment.
or it could mean a runtime system, which is the same as an execution environment.

in the case of C programs, the runtime is the code that sets up the stack, the heap etc. which a requirement expected by the C environment. it essentially sets up the environment that is promised by the language. (it could have a runtime library component, crt0.lib or something like that in case of C)

第几種人 2024-10-04 04:32:41

运行时基本上意味着程序与机器的硬件和操作系统交互的时间。 C 没有自己的运行时,而是从操作系统(基本上是 RAM 的一部分)请求运行时来执行自身。

Runtime basically means when program interacts with the hardware and operating system of a machine. C does not have it's own runtime but instead, it requests runtime from an operating system (which is basically a part of ram) to execute itself.

清晨说晚安 2024-10-04 04:32:41

我发现以下文件夹结构为理解提供了非常有洞察力的上下文runtime 是什么:

Runtimes of Mozilla XulRunner

您可以看到有 '”,有“SDK”或“软件开发套件”,然后是运行时,例如。在运行时运行的东西。它的内容如下:

runtimes' 文件夹内容

win32 zip 包含 .exe -s 和 .dll -s。

所以例如。 C 运行时将是这样的文件 - C 运行时库、.so-s 或 .dll -s - 您在运行时运行,由于它们(或其内容或用途)包含在定义中而变得特殊C 语言(在“纸上”),然后由您选择的 C 实现来实现。然后您可以获得该实现的运行时,使用它并在其基础上进行构建。

也就是说,有一点两极分化,新的基于 C 的程序的用户将需要可运行的文件。作为基于 C 的程序的开发人员,您也是如此,但您也需要 C 编译器和 C 库头文件;用户不需要那些。

I found that the following folder structure makes a very insightful context for understanding what runtime is:

Runtimes of Mozilla XulRunner

You can see that there is the 'source', there is the 'SDK' or 'Software Development Kit' and then there is the Runtime, eg. the stuff that gets run - at runtime. It's contents are like:

runtimes' folder contents

The win32 zip contains .exe -s and .dll -s.

So eg. the C runtime would be the files like this -- C runtime libraries, .so-s or .dll -s -- you run at runtime, made special by their (or their contents' or purposes') inclusion in the definition of the C language (on 'paper'), then implemented by your C implementation of choice. And then you get the runtime of that implementation, to use it and to build upon it.

That is, with a little polarisation, the runnable files that the users of your new C-based program will need. As a developer of a C-based program, so do you, but you need the C compiler and the C library headers, too; the users don't need those.

嗫嚅 2024-10-04 04:32:41

如果我对上述答案的理解是正确的,那么运行时基本上是“后台进程”,例如垃圾收集、内存分配,基本上是由编写代码的库/框架间接调用的任何进程,特别是那些编译后、应用程序运行时发生的进程。

If my understanding from reading the above answers is correct, Runtime is basically 'background processes' such as garbage collection, memory-allocation, basically any processes that are invoked indirectly, by the libraries / frameworks that your code is written in, and specifically those processes that occur after compilation, while the application is running.

公布 2024-10-04 04:32:41

Runtime的完全限定名称似乎是为非Web应用软件提供运行时所需的编程语言相关功能的附加环境。

运行时实现与编程语言相关的功能,这些功能对于任何应用程序域都保持相同,包括数学运算、内存操作、消息传递、操作系统或数据库抽象服务等。

运行时必须以某种方式与正在运行的应用程序连接才能发挥作用,例如作为共享动态库、应用程序在其中运行的虚拟机进程或与应用程序通信的服务进程加载到应用程序内存空间中。

The fully qualified name of Runtime seems to be the additional environment to provide programming language-related functions required at run time for non-web application software.

Runtime implements programming language-related functions, which remain the same to any application domain, including math operations, memory operations, messaging, OS or DB abstraction service, etc.

The runtime must in some way be connected with the running applications to be useful, such as being loaded into application memory space as a shared dynamic library, a virtual machine process inside which the application runs, or a service process communicating with the application.

埖埖迣鎅 2024-10-04 04:32:41

运行时与设计时和编译时/链接时有些相反。从历史上看,它来自缓慢的大型机环境,其中机器时间非常昂贵。

Runtime is somewhat opposite to design-time and compile-time/link-time. Historically it comes from slow mainframe environment where machine-time was expensive.

有木有妳兜一样 2024-10-04 04:32:40

运行时描述了程序运行时执行的软件/指令,特别是那些您没有明确编写但对于正确执行代码所必需的指令。

像 C 这样的低级语言的运行时间非常小(如果有的话)。更复杂的语言(例如允许动态消息传递的 Objective-C)具有更广泛的运行时间。

您认为运行时代码是库代码是正确的,但库代码是一个更通用的术语,描述任何库生成的代码。运行时代码具体是实现语言本身功能所需的代码。

Runtime describes software/instructions that are executed while your program is running, especially those instructions that you did not write explicitly, but are necessary for the proper execution of your code.

Low-level languages like C have very small (if any) runtime. More complex languages like Objective-C, which allows for dynamic message passing, have a much more extensive runtime.

You are correct that runtime code is library code, but library code is a more general term, describing the code produced by any library. Runtime code is specifically the code required to implement the features of the language itself.

从﹋此江山别 2024-10-04 04:32:40

运行时是一个通用术语,指的是代码运行的任何库、框架或平台。

C 和 C++ 运行时是函数的集合。

.NET 运行时包含一个中间语言解释器、垃圾收集器等。

Runtime is a general term that refers to any library, framework, or platform that your code runs on.

The C and C++ runtimes are collections of functions.

The .NET runtime contains an intermediate language interpreter, a garbage collector, and more.

绿光 2024-10-04 04:32:40

运行时执行环境是执行代码并出现在运行时的语言实现的一部分;实现的编译时部分在C 标准中称为翻译环境

示例:

  • Java 运行时由虚拟机和标准库组成

  • 常见的 C 运行时由加载器(操作系统的一部分)和运行时库,它实现了 C 语言中编译器未内置到可执行文件中的部分;在托管环境中,这包括标准库的大部分部分

The runtime or execution environment is the part of a language implementation which executes code and is present at run-time; the compile-time part of the implementation is called the translation environment in the C standard.

Examples:

  • the Java runtime consists of the virtual machine and the standard library

  • a common C runtime consists of the loader (which is part of the operating system) and the runtime library, which implements the parts of the C language which are not built into the executable by the compiler; in hosted environments, this includes most parts of the standard library

过期情话 2024-10-04 04:32:40

根据维基百科:运行时库/运行时系统

在计算机编程中,运行时库是编译器使用的特殊程序库,用于在计算机程序的运行时(执行)期间实现编程语言内置的功能。这通常包括输入和输出或内存管理的函数。


运行时系统(也称为运行时系统或运行时)是设计用于支持以某种计算机语言编写的计算机程序的执行的软件。运行时系统包含基本低级命令的实现,也可以实现更高级别的命令,并且可以支持类型检查、调试,甚至代码生成和优化。
程序员可以通过应用程序编程接口访问运行时系统的某些服务,但其他服务(例如任务调度和资源管理)可能无法访问。


回复:您的编辑,“运行时”和“运行时库”是同一事物的两个不同名称。

As per Wikipedia: runtime library/run-time system.

In computer programming, a runtime library is a special program library used by a compiler, to implement functions built into a programming language, during the runtime (execution) of a computer program. This often includes functions for input and output, or for memory management.


A run-time system (also called runtime system or just runtime) is software designed to support the execution of computer programs written in some computer language. The run-time system contains implementations of basic low-level commands and may also implement higher-level commands and may support type checking, debugging, and even code generation and optimization.
Some services of the run-time system are accessible to the programmer through an application programming interface, but other services (such as task scheduling and resource management) may be inaccessible.


Re: your edit, "runtime" and "runtime library" are two different names for the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文