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

4
  • 3
    Data is private for each process - how do Windows DLLs differ? I think what you want is Shared Memory to share data between processes, right? Commented Jan 28, 2013 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. Commented Jan 28, 2013 at 13:28
  • 1
    A "singleton" typically means one instance per process. Commented Jan 28, 2013 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? Commented Jan 28, 2013 at 13:48

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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)

3 Comments

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 ?
You can share the data with and among descendent processes not among arbitrary processes
A little confused here :( I was referring to this example: codemaestro.com/reviews/11 The lib creates the shared memory then each process can access the same file. Sorry... me n00b...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.