c++ - 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;
}
解决方案
代码看起来基本上是正确的,但是问题可能是由于在 server.cppVector
中Allocator
定义为int
而在 client.cpp 中它们是double
. 检查这个。
推荐阅读
- python - 尝试在 Python 中调用变量值时出错
- c++ - 为什么我在以下代码中会出现堆栈溢出?
- c# - 从 XAML 到 C# 的 WPF 故事板
- flutter - 断言失败:第 854 行 pos 14:'file != null':不正确
- node.js - NodeJS API 如何通过 JDBC 连接到受 kerberos 保护的资源
- git - 拉后如何正确删除合并提交?(处理分离的 HEAD)
- node.js - Webpack handlebars-loader 模板文件在 mocha 单元测试中无法解析
- angular - 是否可以覆盖@Component.providers 中的默认对话框选项?
- javascript - 经过文件值到个人
- javascript - 如何在两个不总是矩形的元素之间画一条线(路径,svg)