首页 > 解决方案 > c++ 服务器 - 客户端 boost::interprocess 共享内存中的数组访问

问题描述

我正在开发一个使用 boost::interprocess 共享内存库的服务器/客户端程序。我希望我的服务器创建一个共享内存对象,该对象将填充一个 int 向量。一旦构造了这个对象,在它被销毁之前,我希望我的客户能够访问共享内存对象并读取这个向量。我现在遇到的问题是我的服务器创建了共享内存对象,存储了向量,并用 100 个整数填充它。在客户端,我打开共享内存对象并读取它,但结果不同。在打印服务器和客户端创建的共享内存对象地址后,我意识到它们是不同的,即客户端正在查看错误的内存地址(我想这可能是主要问题),但是,

我得到的输出是:

*Server.cpp:*

 managed_shared_memory created

 ADDRESS: 0000023B68A10000

 Vector constructed

 Vector filled

现在启动客户端并等待分析...

*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                                 

服务器和客户端代码如下。谢谢。

服务器.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;
}

客户端.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;
}

标签: c++

解决方案


代码看起来基本上是正确的,但是问题可能是由于在 server.cppVectorAllocator定义为int而在 client.cpp 中它们是double. 检查这个。


推荐阅读