English 中文(简体)
How to share global variables in a shared library(.so) across instances of the same process that use the shared library in Linux?
原标题:

I have a shared library(.so) that I preload before executing an application and I have a few global data structures in the shared library that the application uses. The application can create other processes say using fork() and these processes can update the global data structures in the shared library. I would like to keep a consistent view of these global data structures across all the processes. Is there any way I can accomplish this in Linux?

I have tried using shm_* calls and mmap() to map the global data of the shared library to a shared segment but it does not work.

问题回答

To phrase this most clearly: you cannot do exactly what you asked. Linux does not support sharing of global variables that are laid out by the linker. That memory will be in unsharable mapped-to-swap space.

A general recipe I can offer is this:

  1. define a struct that lays out your data. No Pointers! Just offsets.
  2. first process creates a file in /tmp, sets access rw as needed. Open, mmap with MAP_SHARED.
  3. Subsequent processes also open, mmap with MAP_SHARED.
  4. everybody uses the struct to find the pieces they reference, read, or write.
  5. Look Out For Concurrency!

If you really only care about a parent and it s forked children, you can use an anonymous mapping and not bother with the file, and you can store the location of the mapping in a global (which can be read in the children).

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). The library should then store data structures to be shared within that mmaped memory segment (just as if it was memory returned from malloc). Obviously you may need some kind of locking.

Constructor functions for libraries can be indicated using the gcc __constructor__ function attribute.

You don t need to worry about cleaning-up this kind of shared memory - when the last process with an anonymous mapping exits, the memory will be cleaned up.

How about creating a simple pipe in a known directory location, then get other processes to fopen the pipe for reading/writing a la fread/fwrite respectively, to share data...the tricky part is ensuring that the data is passed through the pipe in a manner not to cause corruption in this case. The above you mentioned using shared memory shm_ and mmap is tied to the process, when you fork code, that s no problem since the fork d code is part of the original process! Hope this helps.

Best regards, Tom.





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签