Linux中两个不同进程如何调用共享库文件?

发布于 2024-10-06 19:10:08 字数 92 浏览 3 评论 0原文

在Linux中, 我有一个名为 foo.so 的共享库文件 当我执行两个不同的进程 p1、p2 时,它们都使用 foo.so。 这个 foo.so 是否与这两个进程重叠?

In Linux,
I have a shared library file called foo.so
When I execute 2 different process p1, p2 that both use foo.so.
Does this foo.so get overlapped by those 2 process?

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

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

发布评论

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

评论(3

梦幻的心爱 2024-10-13 19:10:08

在基于 Unix 的系统(包括 Linux)上,代码段 (.text) 可以在多个进程,因为它是不可变的。您提到的这是重叠吗?

基本上,每个包含静态数据(例如全局变量)的共享库都有一个 全局偏移表(GOT)。在共享库上,对静态数据(考虑全局变量)的所有引用都是通过 GOT 发生的(它们是间接的)。因此,即使代码段在多个进程之间共享,每个进程也有其对共享库其他段的独占映射,包括各自的 GOT,其条目会相应地重新定位。

简而言之,进程之间只共享代码,而不共享数据。但是,我认为常量可能是一个例外,具体取决于编译标志。

我还推荐以下书中的第 10 章“动态链接和加载”:链接器和加载器

On Unix-based systems (includes Linux), the code segment (.text) may be shared among multiple processes because it's immutable. Is this overlapping you mention?

Basically, each shared library that contains static data (such as global variables) has a Global Offset Table (GOT). On shared libraries, all references to static data (think of global vars) occur via GOT (they're indirect). So even if the code segment is shared among multiple processes, each process has its exclusive mapping of other segments of the shared library, including the respective GOT, whose entries are relocated accordingly.

In short, only code is shared among processes, not data. However, I think constants may be an exception depending on compilation flags.

I also recommend chapter 10, Dynamic Linking and Loading, from the following book: Linkers and Loaders.

っ〆星空下的拥抱 2024-10-13 19:10:08

共享库的代码由操作系统复制(或更准确地说,映射)到内存中。

然后操作系统允许每个进程访问内存中的一份副本。

每个进程都可能将副本“视为”位于与另一个进程不同的内存地址处。这是由CPU的内存管理单元解决的。

它可能比这更复杂,但这基本上就是 Linux 和其他 Unix 相关操作系统(如 Mac OS X)中的工作方式。

The code for the shared library is copied (or more accurately, mapped) into memory by the operating system.

Then the OS gives each of the processes access to that one copy in memory.

It's possible that each of the processes will "see" the copy as being at a different memory address than the other. This is resolved by the CPU's memory management unit.

It can get more complicated than this, but that's basically how things work in Linux and other Unix-related operating systems like Mac OS X.

绝情姑娘 2024-10-13 19:10:08

这两个进程使用共享库代码段的相同物理地址,但两个进程的虚拟地址不同。虚拟内存有助于实现此功能。

根据这本书,计算机系统:程序员的视角,第 9.5 章 < em>VM 作为内存管理工具

但是,在某些情况下,进程需要共享代码和数据。例如,每个进程必须调用相同的操作系统内核代码,每个 C 程序都调用标准 C 库中的例程,例如 printf。操作系统可以通过将不同进程中的适当虚拟页映射到相同的物理页来安排多个进程共享此代码的单个副本,而不是在每个进程中包含内核和标准 C 库的单独副本。

The two processes are using the same physical address of the code segment of the shared library, but their virtual address is different for the two processes. Virtual memory is helping as implementing this feature here.

According to the book, Computer Systems: A Programmer's Perspective, in chapter 9.5 VM as a tool for Memory Management

However, in some instances it is desirable for processes to share code and data. For example, every process must call the same operating system kernel code, and every C program makes calls to routines in the standard C library such as printf. Rather than including separate copies of the kernel and standard C library in each process, the operating system can arrange for multiple processes to share a single copy of this code by mapping the appropriate virtual pages in different processes to the same physical pages.

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