.net AppDomain、程序集、进程和线程之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:29:39  来源:igfitidea点击:

Difference between AppDomain, Assembly, Process, and a Thread

.net

提问by tush1r

What is the difference between AppDomain, Assembly, Process, and a Thread?

AppDomainAssemblyProcessThread之间有什么区别?

回答by

An AppDomain is an isolation unit within a process. AppDomains can be created at runtime, loaded with code, and unloaded. Its an isolation boundary designed to make .NET apps more reliable.

AppDomain 是进程内的隔离单元。AppDomains 可以在运行时创建、加载代码和卸载。它是一个隔离边界,旨在使 .NET 应用程序更可靠。

An assembly holds one or more modules, which hold compiled chunks of code. You will typically see an assembly as an .EXE or a .DLL.

一个程序集包含一个或多个模块,这些模块包含已编译的代码块。您通常会将程序集视为 .EXE 或 .DLL。

A process is an executing application (waaaay oversimplified).

进程是一个正在执行的应用程序(waaaay 过于简化)。

A thread is an execution context. The operating system executes code within a thread. The operating system switches between threads, allowing each to execute in turn, thus giving the impression that multiple applications are running at the same time.

线程是一个执行上下文。操作系统在线程内执行代码。操作系统在线程之间切换,允许每个线程轮流执行,从而给人一种同时运行多个应用程序的印象。

To put it all together (very simplified)...

把它们放在一起(非常简化)......

A program is executed. A process is created by the operating system, and within its single thread it starts loading code to execute. In a .NET application, a single AppDomain is created by the CLR. The application's executing assembly (the .EXE) is loaded into this AppDomain and begins execution. The application can spawn new processes, create AppDomains, load other assemblies into these domains, and then create new Threads to execute code in any of these AppDomains.

一个程序被执行。进程由操作系统创建,并在其单个线程内开始加载要执行的代码。在 .NET 应用程序中,单个 AppDomain 由 CLR 创建。应用程序的执行程序集(.EXE)被加载到这个 AppDomain 中并开始执行。应用程序可以生成新进程、创建 AppDomains、将其他程序集加载到这些域中,然后创建新线程以在这些 AppDomains 中的任何一个中执行代码。

回答by IntelligentBinary

One of the biggest advantage of CLR's JIT compiler is - it prevents overlapping of processes' virtual address space. For example, if process 1 is spawned and the CLR (MScorEE.dll) is managing the execution of a managed assembly (.exe or .dll) within that process, then the JIT compiler will make sure that the virtual address space allocated to this process will not collide or overlap with the other adjacent processes. Having this advantage, it is now possible to re-use the single process for more than one managed code execution! Each managed code execution will have its own AppDomain and more than AppDomains could be part of a single process. This is what used by IIS and SQL Server (single process, many AppDomains).

CLR 的 JIT 编译器的最大优势之一是 - 它可以防止进程的虚拟地址空间重叠。例如,如果生成进程 1 并且 CLR (MScorEE.dll) 正在管理该进程内托管程序集(.exe 或 .dll)的执行,则 JIT 编译器将确保分配给此进程的虚拟地址空间进程不会与其他相邻进程发生冲突或重叠。有了这一优势,现在可以将单个进程重用于多个托管代码执行!每个托管代码执行都将拥有自己的 AppDomain,并且多个 AppDomain 可以成为单个进程的一部分。这是 IIS 和 SQL Server 使用的(单个进程,多个 AppDomains)。

Assembly is an abstract term that represents a single, re-usable component of managed code. Assembly consists of Metadata (PE32 or PE32+ header + IL header) and IL instructions. CLR's JIT compiler compiles and converts ILs of assembly into a machine specific instruction set, based on the processor and it's architecture (x86 or x64).

程序集是一个抽象术语,表示托管代码的单个可重用组件。汇编由元数据(PE32 或 PE32+ 标头 + IL 标头)和 IL 指令组成。CLR 的 JIT 编译器根据处理器及其架构(x86 或 x64)将汇编的 IL 编译并转换为机器特定的指令集。

Process is what OS uses to facilitate the execution of a program. A process is a "RAM representation" of a program that has address space which consists of stack, heap, static and code region. Each process has a unique process Id associated with it.

进程是操作系统用来促进程序执行的东西。进程是程序的“RAM 表示”,其地址空间由堆栈、堆、静态和代码区域组成。每个进程都有一个唯一的进程 ID 与之关联。

Thread is a light weight process. A process has at least one thread (i.e. main thread) and depending upon the parallelism OS can create multiple threads within a single process and context-switch among them to support faster program execution. Threads can share some memory regions within a process.

线程是一个轻量级的进程。一个进程至少有一个线程(即主线程),操作系统可以根据并行度在单个进程中创建多个线程并在它们之间进行上下文切换以支持更快的程序执行。线程可以共享进程内的一些内存区域。