Вопрос

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,

Это было полезно?

Решение

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

Другие советы

As I commented:

  • Levine's book on linkers and loaders is a useful reference.

  • Linux dynamic linker ld.so is free software, part of GNU libc and you can study and improve its source code

  • the dynamic linker is ld.so not ldconfig (which just updated cached information used by ld.so).

  • the ld.so linker is using the mmap(2) system call to project some .so segments into the virtual address space of the process; the "text" segment (for code and read-only constants) uses MAP_SHARED with PROT_READ. The "data" segment (for global or static variables in C or C++) uses MAP_PRIVATE with PROT_WRITE

  • you would learn a lot by strace-ing your program to get a feeling of the involved system calls.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top