How to get a shared object in shared memory

Our application depends on the external, supplied third-party configuration (including custom motion control / decision functions), loaded as an .so file.

Regardless, it interacts with CGI external modules using a piece of shared memory, where almost all its volatile state is stored, so that external modules can read it and change it where applicable.

The problem is that CGI modules require a lot of persistent .so configuration data, and the main application does absolutely unnecessary copying between two memory areas to make the data available. The idea is to make the entire shared object loaded into shared memory and make it available directly to CGI. The problem is this: how?

  • dlopen and dlsym provide no way to designate where to load the SO file.
  • We tried shmat (). This seems to work only until some external CGI tries to access the shared memory. Then the area pointed to looks as confidential as if it had never been shared. Maybe we are doing something wrong?
  • loading .so into every script that needs it is out of the question. The explicit size of the structure is related to the frequency of calls (some scripts are called once per second to create live updates), and this built-in application makes it invalid.
  • just memcpy () in .so in shm is also not good - some structures and all functions are interconnected via pointers.
+1
source share
4 answers

I believe that the easiest option would be to use a memory mapped file that Neal has already suggested. If this parameter does not fill out well, an alternative would be to define a dedicated allocator. Here is a good article on this: Creating STL containers in shared memory

There is also the excellent Boost.Interprocess Ion Gaztaรฑaga library with shared_memory_object and related functions. Ion proposed a solution to the C ++ Standardization Committee for the future TR: Files with memory and shared memory for C ++, which may indicate that it is worth considering the issue.

+2
source

The first thing to consider when using shared memory is that the same physical memory can be mapped to the virtual address space of two processes as different addresses. This means that if pointers are used anywhere in your data structures, they will cause problems. Everything should work with an index or offset to work properly. To use shared memory, you will have to clear all pointers from your code.

When you download the .so file, only one copy of the code for the .so file is downloaded (hence the term shared object).

fork can also be your friend here. Most modern operating systems implement copy-on-write semantics. This means that when you fork , your data segments are copied only to a separate physical memory, when one process writes to this data segment.

+3
source

Placing real C ++ objects in shared memory is very, very difficult, as you already found. I would highly recommend that you donโ€™t go this way - storing the data you need to use in shared memory, or a memory mapped file is much simpler and will probably be much more reliable.

+2
source

You need to implement the Serialization object. The Serialization function converts your object to bytes, then you can write bytes in SharedMemory and have your CGI module for deserializing bytes back to the object.

0
source

Source: https://habr.com/ru/post/1395968/


All Articles