Is global variable in a shared library / dll, shared across process

╄→尐↘猪︶ㄣ 提交于 2019-11-27 17:53:21

问题


I am developing a shared library(.so) and dll. I have a global variable which is updated in multiple threads. So I have mutex lock for synchronization.

I am not clear whether global data in shared library is shared across process. If it is then I need to use semaphores for synchronization. As I understand global variables are part of data segment so I wanted to understand how dll manages the global data across processes. Any information w.r.t. dll format and segment will be helpful.

Thanks.


回答1:


By default, no, global variables are not shared across processes.

However, you can use a data segment (data_seg) in order to share global variables across processes. You can find more information on MSDN in the article titled "How do I share data in my DLL with an application or with other DLLs?"




回答2:


Absolutely NO. Each process has its own virtual memory space and do not see memory of other processes. Two processes can even store different value at the same address, say 1000000 - because theirs virtual addresses "1000000" are mapped to different physical memory cells (for example to "2000000" for first process and to "3000000" for second. Shared dll does not change anything in this.



来源:https://stackoverflow.com/questions/1979303/is-global-variable-in-a-shared-library-dll-shared-across-process

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