C ++ shared object through memory

I have a quick question for those who are familiar with interprocess communication.

Situation

  • I have a program (program A) in which I can add code, but very limited. This is the main program that generates a lot of data.
  • The way of formulating data is limited, so I would like to create a second program (program B) and, therefore, you need to get data from A to B. And even sometimes make A run some functions without a return value.
  • I know about Named Pipes, but do I feel they can be bulky? - not sure though - I have, for example, the following problems (may be unreasonable):
    • data stream => convert to binary → put data into memory → Read server → convert to string → through probably the switch statement determine what is requested → receive what is requested → convert to binary → a place in memory → read by the client and convert to string / any acceptable format.
    • It should use mainly switch statements on both sides, and if you need a different format for information other than a string, you need to take this into account
    • One message may have to wait until another is completed so that it can be slower during multiple calls to it at the same time? - not sure though
  • Other methods of interaction between processes are likely to have the same problem.
  • , , "". , "" A B :

    • ..
    • / .
    • , (.. bool/int/string/double ..)

    , , / , .

  • :
  • ++, ? :
    • A B, /, . , , , / ?
    • WriteProcessMemory - , - , / B, A.
  • ? , boost, - ? → ?

.

+4
3

Boost.Interprocess , shared_memory

, boost, ( , )

include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <cstring>
#include <cstdlib>
#include <string>

int main(int argc, char *argv[])
{
   using namespace boost::interprocess;

   if(argc == 1){  //Parent process
      //Remove shared memory on construction and destruction
      struct shm_remove
      {
         shm_remove() { shared_memory_object::remove("MySharedMemory"); }
         ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
      } remover;

      //Create a shared memory object.
      shared_memory_object shm (create_only, "MySharedMemory", read_write);

      //Set size
      shm.truncate(1000);

      //Map the whole shared memory in this process
      mapped_region region(shm, read_write);

      //Write all the memory to 1
      std::memset(region.get_address(), 1, region.get_size());

      //Launch child process
      std::string s(argv[0]); s += " child ";
      if(0 != std::system(s.c_str()))
         return 1;
   }
   else{
      //Open already created shared memory object.
      shared_memory_object shm (open_only, "MySharedMemory", read_only);

      //Map the whole shared memory in this process
      mapped_region region(shm, read_only);

      //Check that memory was initialized to 1
      char *mem = static_cast<char*>(region.get_address());
      for(std::size_t i = 0; i < region.get_size(); ++i)
         if(*mem++ != 1)
            return 1;   //Error checking memory
   }
   return 0;
}
+1

!

shared state , .

ØMQ . , , . ØMQ "" , .

: : msgpack-rpc protobuf-remote

+1

Do you create software for a client or just for data analysis? If only for yourself, you can write data to your hard drive and divide your analysis into two stages.

0
source

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


All Articles