views:

1162

answers:

2

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.

+4  A: 

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?"

casperOne
@CasperOne: By default will the shared library data segment duplicated across each process?
Andy
@Andy: I don't know what you mean by duplicated. Basially, you will be able to share the values across processes if you link to the same dll, if that is what you are asking. Otherwise, could you elaborate?
casperOne
@CasperOne: I think the code(text) segment of dll is shared across process. So I wanted to know what happens to the data segment of the dll. Whether the contents of data segment from dll is copied into process data segment. And how this happens during dynamic loading of dll.
Andy
A: 

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.

Sorry, but that's simply not true.
casperOne
Try to read something, for example starting with this: http://msdn.microsoft.com/en-us/library/ms810627.aspx about memory management/virtual memory mapping and managing memory in different processes at win32 platform, before writing anything here :)
Ironic how you say to "try to read something", when in fact I posted a link to MSDN showing exactly what is done, as well as why your answer is wrong.
casperOne
I only do not see what is wrong. Is dll global variable shared across several processes?No. We can see that from YOUR link: "Win32 DLLs are mapped into the address space of the calling process. By default, each process using a DLL has its own instance of all the DLLs global and static variables."What did i answer? "No." So what's wrong?
I've also written that each process has its own 4gb virtual memory addresses space, with some regions (which are in use) mapped to physical memory addresses. What's wrong here? Again, nothing.