does dynamic library shared global variable in linux

混江龙づ霸主 提交于 2019-12-12 16:51:25

问题


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.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.



来源:https://stackoverflow.com/questions/12452232/does-dynamic-library-shared-global-variable-in-linux

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