how is a shared library file called by two different processes in Linux?

有些话、适合烂在心里 提交于 2019-11-29 21:04:34
jweyrich

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!