为什么同一程序的不同实例的代码段是通用的

发布于 2024-08-06 06:57:11 字数 107 浏览 9 评论 0原文

我想知道为什么代码段对于同一程序的不同实例是常见的。

例如:考虑程序 P1.exe 正在运行,如果 P1.exe 的另一个副本正在运行,则两个运行实例的代码段将是通用的。为什么会这样呢?

I wanted to know why code segment is common for different instances of same program.

For example: consider program P1.exe running, if another copy of P1.exe is running, code segment will be common for both running instances. Why is it so?

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

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

发布评论

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

评论(4

不如归去 2024-08-13 06:57:11

如果有问题的代码段是从 DLL 加载的,则可能是操作系统很聪明并重新使用了已加载的库。这是使用动态加载库代码的核心点之一,它允许代码在多个进程之间共享。

不确定 Windows 是否足够聪明,能够对常规 EXE 文件的代码部分执行此操作,但如果可能的话,这是有意义的。

也可能是虚拟内存欺骗了你;两个进程看起来像是在同一地址上有相同的东西,但该地址是虚拟的,因此它们实际上只是显示物理内存的映射。

If the code segment in question is loaded from a DLL, it might be the operating system being clever and re-using the already loaded library. This is one of the core points of using dynamically loaded library code, it allows the code to be shared across multiple processes.

Not sure if Windows is clever enough to do this with the code sections of regular EXE files, but it would make sense if possible.

It could also be virtual memory fooling you; two processes can look like they have the same thing on the same address, but that address is virtual, so they really are just showing mappings of physical memory.

微暖i 2024-08-13 06:57:11

代码通常是只读的,因此制作多个副本会很浪费。

此外,Windows(至少,我不能代表这个级别的其他操作系统)使用分页基础结构直接从可执行文件中调入和调出代码,就好像它是一个分页文件一样。由于您正在处理相同的可执行文件,因此它是从相同位置分页到相同位置。

现代操作系统实际上不再支持自修改代码。可以生成新代码(通过在分配内存时设置正确的标志),但这与原始代码段是分开的。

Code is typically read-only, so it would be wasteful to make multiple copies of it.

Also, Windows (at least, I can't speak for other OS's at this level) uses the paging infrastructure to page code in and out direct from the executable file, as if it were a paging file. Since you are dealing with the same executable, it is paging from the same location to the same location.

Self-modifying code is effectively no longer supported by modern operating systems. Generating new code is possible (by setting the correct flags when allocating memory) but this is separate from the original code segment.

北凤男飞 2024-08-13 06:57:11

代码段是(应该是)静态的(不会改变),因此没有理由不在多个实例中使用它。

The code segment is (supposed to be) static (does not change) so there is no reason not to use it for several instances.

淡淡的优雅 2024-08-13 06:57:11

从基础层面开始,分段只是实现内存隔离和分区的一种方法。 分页是实现此目的的另一种方法。在大多数情况下,任何可以通过分段实现的内容,都可以通过分页实现。因此,x86 上的大多数现代操作系统根本放弃使用分段,而是完全依赖分页设施。

因此,所有进程通常都在(Base = 0,Limit = 4GB,Privilege level = 3)的普通段下运行,这意味着代码/数据段寄存器在确定物理地址方面没有真正的作用,并且是只是用来设置进程的权限级别。所有进程通常都会以相同的权限运行,因此它们在段寄存器中应该具有相同的值。

编辑

也许我误解了这个问题。我认为问题作者是在问为什么两个进程在代码段寄存器中具有相同的值。

Just to start at a basic level, Segmentation is just a way to implement memory isolation and partitioning. Paging is another way to achieve this. For the most part, anything you can achieve via segmentation, you can be achieve via paging. As such, most modern operating systems on the x86 forego using segmentation at all, instead relying completely on paging facilities.

Because of this, all processes will usually be running under the trivial segment of (Base = 0, Limit = 4GB, Privilege level = 3), which means the code/data segment registers play no real part in determining the physical address, and are just used to set the privilege level of the process. All processes will usually be run at the same privilege, so they should all have the same value in the segment register.

Edit

Maybe I misinterpreted the question. I thought the question author was asking why both processes have the same value in the code segment register.

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