问题
As we know, linux call ldconfig to load all *.so libraries and then link the applications who use the shared library. However, I am confused how the global variable is working in this case. Since there is only one copy of shared library across all these application, do they share the global variables in the shared library? If yes, then how they synchronize?
Thanks,
回答1:
No it is not shared - the code/text section of the library is shared - the data portion is unique to each process that uses the library
回答2:
As I commented:
Levine's book on linkers and loaders is a useful reference.
Linux dynamic linker
ld.sois free software, part of GNU libc and you can study and improve its source codethe dynamic linker is
ld.sonotldconfig(which just updated cached information used byld.so).the
ld.solinker is using the mmap(2) system call to project some.sosegments into the virtual address space of the process; the "text" segment (for code and read-only constants) usesMAP_SHAREDwithPROT_READ. The "data" segment (for global or static variables in C or C++) usesMAP_PRIVATEwithPROT_WRITEyou would learn a lot by
strace-ing your program to get a feeling of the involved system calls.
来源:https://stackoverflow.com/questions/12452232/does-dynamic-library-shared-global-variable-in-linux