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?
在Linux中,我有一个名为foo.so的共享库文件。当我执行2个不同的进程p1时,p2都使用foo.so.这个foo.so会被这两个进程重叠吗?
2 个解决方案
#1
29
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?
在基于Unix的系统(包括Linux)上,代码段(.text)可以在多个进程之间共享,因为它是不可变的。你提到这个重叠吗?
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.
基本上,每个包含静态数据的共享库(例如全局变量)都有一个全局偏移表(GOT)。在共享库上,所有对静态数据的引用(想想全局变量)都是通过GOT(它们是间接的)发生的。因此,即使代码段在多个进程之间共享,每个进程也具有其共享库的其他段的独占映射,包括相应的GOT,其条目相应地重新定位。
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 the following article: Dynamic Linking and Loading.
我还推荐以下文章:动态链接和加载。
#2
4
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.
每个进程可能会“看到”副本处于与另一个不同的内存地址。这由CPU的内存管理单元解决。
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.
它可能比这更复杂,但这基本上是在Linux和其他与Unix相关的操作系统(如Mac OS X)中工作的方式。
#1
29
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?
在基于Unix的系统(包括Linux)上,代码段(.text)可以在多个进程之间共享,因为它是不可变的。你提到这个重叠吗?
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.
基本上,每个包含静态数据的共享库(例如全局变量)都有一个全局偏移表(GOT)。在共享库上,所有对静态数据的引用(想想全局变量)都是通过GOT(它们是间接的)发生的。因此,即使代码段在多个进程之间共享,每个进程也具有其共享库的其他段的独占映射,包括相应的GOT,其条目相应地重新定位。
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 the following article: Dynamic Linking and Loading.
我还推荐以下文章:动态链接和加载。
#2
4
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.
每个进程可能会“看到”副本处于与另一个不同的内存地址。这由CPU的内存管理单元解决。
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.
它可能比这更复杂,但这基本上是在Linux和其他与Unix相关的操作系统(如Mac OS X)中工作的方式。