首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++服务器-客户端提升:共享内存中的进程间阵列访问

c++服务器-客户端提升:共享内存中的进程间阵列访问
EN

Stack Overflow用户
提问于 2020-10-05 08:10:24
回答 2查看 468关注 0票数 1

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

我得到的输出是:

代码语言:javascript
复制
*Server.cpp:*

 managed_shared_memory created

 ADDRESS: 0000023B68A10000

 Vector constructed

 Vector filled

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

代码语言:javascript
复制
*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.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-05 12:54:06

代码看起来本质上是正确的,但是问题可能是由于在server.cpp VectorAllocator中定义为int而在client.cpp中它们是double这一事实而产生的冲突。看看这个。

票数 1
EN

Stack Overflow用户

发布于 2020-10-05 09:30:01

答案是向量有一个数据成员,它是指向向量中元素的指针。没有被翻译到客户端。

在服务器上,数据可能指向0000023B68A11000

在客户机上,它仍然指向0000023B68A11000,但正确的地址可能是000002EE6BD91000。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64204744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档