简体   繁体   English

c ++服务器-共享内存中的客户端boost ::进程间数组访问

[英]c++ Server - Client boost::interprocess Array access in shared memory

I am developing a Server/Client program that makes use of the boost::interprocess shared memory library.我正在开发一个使用 boost::interprocess 共享内存库的服务器/客户端程序。 I would like my Sever to create a shared memory object, which will be filled with a vector of int.我希望我的服务器创建一个共享内存对象,它将用一个 int 向量填充。 Once this object is constructed, before its destruction, I would like, my Client to have access to the shared memory object and read this vector.一旦构建了这个对象,在它销毁之前,我希望我的客户端能够访问共享内存对象并读取这个向量。 The problem I have now is that my Server creates the shared memory object, stores the vector, and fills it whit 100 numbers int.我现在遇到的问题是我的服务器创建共享内存对象,存储向量,并用 100 个整数填充它。 On the Client side, I open the shared memory object and read this, but the results are different.在Client端,我打开共享内存对象,读取这个,但是结果不一样。 What I realized, after printing the shared memory object address created by server and client, is that they are different, ie The Client is looking at the wrong memory address (I guess this is likely to be the main issue), however, I cannot explain why this happens and where is the mistake in my code.在打印服务器和客户端创建的共享内存对象地址后,我意识到它们是不同的,即客户端正在查看错误的内存地址(我想这可能是主要问题),但是,我不能解释为什么会发生这种情况以及我的代码中的错误在哪里。

The output I get is:我得到的输出是:

*Server.cpp:*

 managed_shared_memory created

 ADDRESS: 0000023B68A10000

 Vector constructed

 Vector filled

Now launch Client and wait for analysis...现在启动客户端并等待分析...

*Client.cpp:*

 managed_shared_memory opened

 ADDRESS: 000002EE6BD90000

 Vector Found

 2.122e-314 6.36599e-314 1.061e-313 1.4854e-313 1.9098e-313                                 

Server and Client codes are below.服务器和客户端代码如下。 Thanks.谢谢。

Server.cpp服务器.cpp

#include <cstdlib>
#include <string>
#include <iostream>

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef vector<int, ShmemAllocator> MyVector;

//Main function. For parent process argc == 1, for child process argc == 2
int main()
{
     //Remove shared memory on construction and destruction
    struct shm_remove
    {
      shm_remove() { shared_memory_object::remove("MySharedMemoryNew"); }
      ~shm_remove() { shared_memory_object::remove("MySharedMemoryNew"); }
    } remover;

    //Create a new segment with given name and size
    managed_shared_memory segment(create_only, "MySharedMemoryNew", 65536);
    std::cout << " managed_shared_memory created" << std::endl;

    //Initialize shared memory STL-compatible allocator
    const ShmemAllocator alloc_inst(segment.get_segment_manager());
    std::cout << " ADDRESS: " <<segment.get_address() << std::endl;

    //Construct a vector named "MyVector" in shared memory with argument alloc_inst
    MyVector* myvector = segment.construct<MyVector>("MySharedVector")(alloc_inst);
    std::cout << " Vector constructed" << std::endl;

    for (int i = 0; i < 5; ++i)  //Insert data in the vector
      myvector->push_back(i);
    std::cout << " Vector filled" << std::endl;
    
    std::cout << " Now launch Client and wait for analysis... " << std::endl;
    int carryon;
    //only after client has been launched
    std::cin >> carryon;

  return 0;
}

Client.cpp客户端.cpp

#include <cstdlib>
#include <string>
#include <iostream>

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

using namespace boost::interprocess;

//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<double, managed_shared_memory::segment_manager>  ShmemAllocator;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef vector<double, ShmemAllocator> MyVector;

int main()
{

  //Open a segment with given name
  managed_shared_memory segment(open_only, "MySharedMemoryNew");
  std::cout << " managed_shared_memory opened" << std::endl;
  std::cout << " ADDRESS: " << segment.get_address() << std::endl;

  // access to the vector
  MyVector* myvector = segment.find<MyVector>("MySharedVector").first;
  std::cout << " Vector Found" << std::endl;

  for (unsigned int k = 0; k < 5; k++) {
    std::cout << (*myvector)[k] << " ";
  }

  return 0;
}

The code looks essentially correct, however the problem might be some conflict generated by the fact that in server.cpp Vector and Allocator are defined as int whilst in client.cpp they are double .代码看起来基本上是正确的,但是问题可能是由于在 server.cpp 中VectorAllocator被定义为int而在 client.cpp 中它们是double产生的一些冲突。 Check this.检查这个。

The answer is that vector has a data member that is a pointer to the elements in the vector.答案是向量有一个数据成员,它是指向向量中元素的指针。 That is not translated to client.那不会翻译给客户端。

On the server the data might point to 0000023B68A11000在服务器上,数据可能指向 0000023B68A11000

On the client it will still point to 0000023B68A11000 but the correct address might be 000002EE6BD91000在客户端它仍然会指向 0000023B68A11000 但正确的地址可能是 000002EE6BD91000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM