gpt4 book ai didi

c++ - 提升 IPC 字符串长度段错误?

转载 作者:行者123 更新时间:2023-11-30 02:51:42 25 4
gpt4 key购买 nike

我有一个非常小的两个程序示例,一个用于写入共享内存段,另一个用于从中读取。我意识到 std::string (和其他容器)存在潜在问题,所以尝试了 boost::interprocess::string 这是一个 boost::containers::字符串。我很确定这缺少一些非常基本和简单的东西,但看不到它!

在任何情况下,概要是当字符串很小(我认为虽然比 SSO 大)时,运行第一个程序写入内存,第二个读取完美。但是,如果我像此处的示例那样使字符串非常大,那么读取程序就会出现段错误。如果读写都在同一个进程中,但功能不同(所以除了通过 ipc 之外不共享任何数据,我就成功了。下面的代码writeipc.cc

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/containers/string.hpp>
#include <iostream>
#include <utility>

int main()
{
typedef boost::interprocess::string bi_string;

boost::interprocess::shared_memory_object::remove("Test");
boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::create_only, "Test", 65536);


bi_string* i = managed_shm.construct<bi_string>("string")("abcdefghijklmnopqrstuvwxyzaskl;dfjaskldfjasldfjasdl;fkjwrotijuergonmdlkfsvmljjjjjjjjjjjjjj"); // make smaller (ie "jjjjjjjjjjjjjj" and test passes
std::cout << "inserting into shm" << *i << std::endl;

std::pair<bi_string*, size_t> q = managed_shm.find<bi_string>("string");
std::cout << *q.first << std::endl;
while(true)
std::cout << "still running"; // hack to keep process running (not required)
}

readipc.cc

#include <boost/interprocess/managed_shared_memory.hpp> 
#include <boost/interprocess/containers/string.hpp>
#include <iostream>

int main()
{
typedef boost::interprocess::string bi_string;
boost::interprocess::managed_shared_memory managed_shm(boost::interprocess::open_only, "Test");

std::pair<bi_string*, std::size_t> p = managed_shm.find<bi_string>("string");
std::cout << "existing value" << *p.first << std::endl;

}

最佳答案

好的,答案与分配器相关。

我发布我认为是让其他人快速了解如何开始使用 IPC 的答案。我发现很多关于网络的例子都没有很好地解释这个过程,给人的印象是使用普通字符串等就可以了,直到你从不同的过程中读取。因此,要完全实现解决方案,需要一些额外的小步骤。

本质上,您不能使用普通 STL 类型容器提供的分配器。这记录在案 here

Where is this being allocated?

Boost.Interprocess containers are placed in shared memory/memory mapped files, etc... using two mechanisms at the same time:

Boost.Interprocess construct<>, find_or_construct<>... functions. These functions place a C++ object in the shared memory/memory mapped

file. But this places only the object, but not the memory that this object may allocate dynamically. Shared memory allocators. These allow allocating shared memory/memory mapped file portions so that containers can allocate dynamically fragments of memory to store newly inserted elements.

This means that to place any Boost.Interprocess container (including Boost.Interprocess strings) in shared memory or memory mapped files, containers must:

Define their template allocator parameter to a Boost.Interprocess allocator.
Every container constructor must take the Boost.Interprocess allocator as parameter.
You must use construct<>/find_or_construct<>... functions to place the container in the managed memory.

If you do the first two points but you don't use construct<> or find_or_construct<> you are creating a container placed only in your process but that allocates memory for contained types from shared memory/memory mapped file.

我已经创建了几个可以找到的示例函数 here

上面缺少的是

namespace bi = boost::interprocess;
typedef bi::allocator<char, bi::managed_shared_memory::segment_manager> CharAllocator;
typedef bi::basic_string<char, std::char_traits<char>, CharAllocator> bi_string;

现在这是一个字符串,当我们可以为它获取分配器时可以存储它。这意味着像这样从你的内存段获取分配器

  bi::managed_shared_memory segment(bi::create_only, "shm name",
65536);
// Create an object of Type initialized to type
CharAllocator charallocator(segment.get_segment_manager());

现在 charallocator 可用于构造一个字符串类型,该字符串类型将在共享内存位置工作并能够正确读回。字符串创建为:

 bi_string str(charallocator);

然后你可以给它分配一个cstring。例如std::string test("一个测试字符串");

str = test.c_str();

如果你构造一个段,你的字符串将被存储

segment.construct<bi_string>("some name")(str)

您可以在共享内存存储中存储多个段。我希望这对其他人有帮助。代码都可以通过上面的链接中的测试获得。

关于c++ - 提升 IPC 字符串长度段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19538368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com