1

I currently created a shared library(libshared.so), which contains a variable "a", which will be modified by shared library api. i have two applications app1 and app2. app1 is using shared library api, which changes the value of "a". now simultaneously when app2 is running should see the changed value. This is against the actual usage of shared library, as separate instance of shared library will be created for every application. now i want to use a single instance of shared library between two app1 and app2, so that they can see same code and data segment of shared library. Is there any possible way to achieve this, by altering gcc linker flags

anikhan
  • 1,147
  • 3
  • 18
  • 44
  • You are looking for [Shared Memory (wikipedia)](https://en.wikipedia.org/wiki/Shared_memory#In_software) – dvhh Mar 04 '16 at 05:58
  • Additional informations http://stackoverflow.com/a/19374253 , http://stackoverflow.com/a/14563172 – dvhh Mar 04 '16 at 06:03
  • No dvhh, i am not looking for shared memory, i have set of api's which will be used app1 and app2. how to achieve this using shared memory, if it is possible with shared memory – anikhan Mar 04 '16 at 06:12
  • 1
    the shared object would provide a common api for the two library, but the memory space would be still private to the server – dvhh Mar 04 '16 at 06:15
  • 1
    Yes you do. If you want to solve your problem, that is. Shared libraries only share code and sometimes immutable data. Not variables. – n. m. could be an AI Mar 04 '16 at 06:37
  • @anikhan Could you let me know your motivation doing this? I've trying Dependency Inversion recently, and placing a shared memory in shared library just came in my mind. – Andy Lin Dec 02 '21 at 07:52

1 Answers1

0

These answer to :

Would shatter your expectation from the shared library approach.

I recommend you take a look at Shared Memory and the POSIX API shmget.

One process would act as a server and create the shared memory and provide the shared memory key to the client.

Another approach would be to create a pipe to share data between the process

You could read this page to have more information about inter process communication on linux

Community
  • 1
  • 1
dvhh
  • 4,724
  • 27
  • 33