Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 616
Andy Avatar asked Dec 30 '09 09:12

Andy


People also ask

Are global variables shared between processes?

No, since global variables are not shared between processes unless some IPC mechanism is implemented. The memory space will be copied. As a consequence, the global variable in both processes will have the same value inmediately after fork, but if one changes it, the other wont see it changed.

Are global variables accessible by other files?

Variables declared outside of a function are called external, or global,variables and can be accessed by any function.

Can global variables be accessed anywhere?

You can access the global variables from anywhere in the program. However, you can only access the local variables from the function. Additionally, if you need to change a global variable from a function, you need to declare that the variable is global. You can do this using the "global" keyword.

Do global variables live on the heap?

The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation.


1 Answers

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

like image 73
casperOne Avatar answered Oct 05 '22 07:10

casperOne