问题描述
在Linux中,我有一个共享库文件foo.so当我执行2个不同的进程p1,p2时,它们都使用foo.so.这个foo.so是否会被这两个进程重叠?
In Linux,I have a shared library file called foo.soWhen I execute 2 different process p1, p2 that both use foo.so.Does this foo.so get overlapped by those 2 process?
推荐答案
在基于Unix的系统(包括Linux)上,代码段(.text)可以在多个进程之间共享,因为它是不可变的.您提到这个重叠吗?
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?
基本上,每个包含静态数据(例如全局变量)的共享库都具有全局偏移表(GOT).在共享库上,所有对静态数据的引用(认为是全局变量)都是通过GOT(它们是间接的)发生的.因此,即使代码段在多个进程之间共享,每个进程也对共享库的其他段(包括各自的GOT)进行独占映射,其条目会相应地重定位.
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.
我还推荐以下书籍的第10章,动态链接和加载:链接器和加载程序.
I also recommend chapter 10, Dynamic Linking and Loading, from the following book: Linkers and Loaders.
这篇关于Linux中两个不同的进程如何调用共享库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!