Linux中两个不同进程如何调用共享库文件?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在基于 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.
共享库的代码由操作系统复制(或更准确地说,映射)到内存中。
然后操作系统允许每个进程访问内存中的一份副本。
每个进程都可能将副本“视为”位于与另一个进程不同的内存地址处。这是由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.
这两个进程使用共享库代码段的相同物理地址,但两个进程的虚拟地址不同。虚拟内存有助于实现此功能。
根据这本书,计算机系统:程序员的视角,第 9.5 章 < em>VM 作为内存管理工具
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