8

Is it possible to use global variable located in a shared library (.so) as a singleton if the library is used by more than one process?

As example the initial value is 0, process 1 increments the var then proc2 increments the val and prints it.

My experiments so far showed that the both process keep copies of the variable and if 1st increments it the second will still read 0. So the behavior is not like Windows DLLs...

I read in one article here that if the global variable is not static (in the lib) and it's declared as extern in the lib header the var is unique for all the process. But so far I haven't been able to accomplish this - var is still copy for each process.

Can someone please offer good explanation of this? And how to do it...

V0idd
  • 95
  • 1
  • 7
  • 3
    Data is private for each process - how do Windows DLLs differ? I think what you want is [Shared Memory](http://stackoverflow.com/questions/5436730/shared-memory-and-ipc) to share data between processes, right? – Andreas Fester Jan 28 '13 at 13:24
  • I've been told that this is not the case with Win DLLs but I guess it's not true... I want a singleton but don't know how to do this... I have 2 process and 1 lib. – V0idd Jan 28 '13 at 13:28
  • 1
    A "singleton" typically means one instance per process. – Vaughn Cato Jan 28 '13 at 13:37
  • OK so following the comments bellow I need SharedMemory. Is it possible to create such memory from the Lib and then use it from every process that uses the lib? – V0idd Jan 28 '13 at 13:48

3 Answers3

7

If a shared library (or a Windows DLL) is used by more than one process, any modifyable data is still private to the process. There are mechanisms like Copy on Write where the same data is shared as long as it is only read, but copied as soon as it is written by either process. So, for each process the data effectively is still separate. See also shared library address space.

If you want to share data between processes, you need to use Shared Memory and make sure that access to the shared memory is synchronized between the processes.

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
1

Processes each live in their own memory space. (Imagine the havoc you could wreak on a machine if you could, just by loading a library that some other process is using, completely arbitrarily trash their address space!) So, globals are global, but only within a process.

sheu
  • 5,653
  • 17
  • 32
1

Linux does not support sharing of global variables that are laid out by the linker. That memory will be in unsharable space.

If you only want to share the data with and among descendent processes (and not with arbitrary processes that are started up seperately, that just happen to link to the same shared library), then the easiest way to do this is have the library create a mapping with mmap() in a constructor function (that is called when the library is initially loaded in the parent process).

Pass the MAP_ANONYMOUS and MAP_SHARED flags to mmap - this will mean that child processes that inherit the mapping will have a mapping that is shared with the parent (and the other children)

sr01853
  • 6,043
  • 1
  • 19
  • 39
  • You mean in LIB shm_open() and mmap() to init and then from each process that uses the LIB do the same - shm_open+mmap to use the space ? – V0idd Jan 28 '13 at 14:32
  • You can share the data with and among descendent processes not among arbitrary processes – sr01853 Jan 28 '13 at 14:34
  • A little confused here :( I was referring to this example: http://www.codemaestro.com/reviews/11 The lib creates the shared memory then each process can access the same file. Sorry... me n00b... – V0idd Jan 28 '13 at 15:07