提升进程间共享内存删除、权限和输出文件
Boost Interprocess share memory deletion, permissions and output files
我正在使用Boost Interprocess库在两个进程之间共享内存。
我正在使用以下内容来分配共享内存块、附加向量、名为互斥锁和命名条件变量:
using ShmemAllocator = allocator<T, managed_shared_memory::segment_manager>;
using MyVector = vector<T, ShmemAllocator>;
segment.reset(new managed_shared_memory(create_only, blockName, numBytes));
const ShmemAllocator alloc_inst(segment->get_segment_manager());
vec = segment->construct<MyVector>(sharedVectorName)(alloc_inst);
named_mutex.reset(new named_mutex(create_only, mutexName));
cond_empty.reset(new named_condition(create_only, conditionVName));
以及要删除的以下内容:
named_mutex::remove(mutexName);
named_condition::remove(conditionVName);
shared_memory_object::remove(blockName);
为了检查内存是否被正确删除,我运行了压力测试:
while(counter < 1000000)
{
MySharedMemoryObj s;
++counter;
}
(依靠 RAII 让析构函数删除共享内存(
我有三个问题:
我是否需要删除矢量,因为它无论如何都是段的一部分?
上述方法有效,但在一个特定情况下它没有,并抛出了一个 Boost 进程间异常,说它没有访问内存的权限。是什么原因导致这种情况/有没有办法避免它?
我注意到上面的代码似乎在/tmp 中生成名为
outputXXXXXXXXXXX
的二进制文件。这些是什么?它们不会被删除,因此会累积。
- 我需要删除向量吗,因为它无论如何都是段的一部分?
从技术上讲,不是真的在这里(假设你也使用共享内存分配器(。但是,跳过销毁是一种不好的做法,尤其是当您的析构函数具有逻辑(并非微不足道(时。
- 上述方法有效,但在一个特定情况下它没有,并抛出了一个 Boost 进程间异常,说它没有访问内存的权限。是什么原因导致这种情况/有没有办法避免它?
确保程序在创建共享分段时与预期用户一样运行。这就是赋予它文件级访问权限的原因。
例如,如果您将细分创建为root
,则不应以其他用户身份打开它。
- 我注意到上面的代码似乎在/tmp 中生成了名为 outputXXXXXXXXXX 的二进制文件。这些是什么?它们不会被删除,因此会累积。
这没有多大意义。这些路径表明您使用的是 POSIX 系统。在POSIX上,shmem通常存在于/dev/shm
中,我认为不需要临时文件。
我建议临时文件可能是其他程序的工件(例如您的IDE?
建议的简化:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/named_mutex.hpp>
#include <boost/interprocess/sync/named_condition.hpp>
#include <vector>
namespace bip = boost::interprocess;
static auto blockName = "4f72909d-8265-4260-9bb1-6bd58f63812c";
static auto sharedVectorName = "54714711";
static auto mutexName = "b4eb63e0";
static auto conditionVName = "f7a95857";
template <typename T> using ShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>;
template <typename T> using MyVector = std::vector<T, ShmemAllocator<T> >;
int main() {
bip::managed_shared_memory segment(bip::create_only, "blockName", 10<<20u);
auto vec = segment.construct<MyVector<int> >(sharedVectorName)(segment.get_segment_manager());
bip::named_mutex named_mutex(bip::create_only, mutexName);
bip::named_condition named_condition(bip::create_only, conditionVName);
}
或者,根据要同步的内容,使同步基元成为共享数据的成员:
struct SharedData {
using allocator_type = ShmemAllocator<int>;
template <typename A>
SharedData(A alloc) : _vec(alloc) {}
MyVector<int> _vec;
bip::interprocess_mutex _mx;
bip::interprocess_condition _cond;
};
int main(int argc, char**) {
bip::managed_shared_memory segment(bip::open_or_create, "2fc51845-3d9b-442b-88ee-f6fd1725e8b0", 10<<20u);
auto& data = *segment.find_or_construct<SharedData>("sharedData")(segment.get_segment_manager());
}
简单的多生产者/多消费者队列:
对最大容量为 10 个元素的队列进行建模。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <vector>
#include <mutex> // unique_lock
namespace bip = boost::interprocess;
template <typename T> using ShmemAllocator = bip::allocator<T, bip::managed_shared_memory::segment_manager>;
template <typename T> using MyVector = std::vector<T, ShmemAllocator<T> >;
struct SharedData {
using allocator_type = ShmemAllocator<int>;
template <typename A>
SharedData(A alloc) : _vec(alloc) {}
MyVector<int> _vec;
bip::interprocess_mutex _mx;
bip::interprocess_condition _cond;
using lock_type = std::unique_lock<bip::interprocess_mutex>;
lock_type wait_for_empty() {
lock_type lk(_mx);
_cond.wait(lk, [=] { return _vec.empty(); });
return lk;
}
void push(int v) {
lock_type lk(_mx);
_cond.wait(lk, [=] { return _vec.size() < 10; }); // wait for free space
_vec.push_back(v);
_cond.notify_all();
}
bool pop(boost::posix_time::time_duration timeout, int& out) {
lock_type lk(_mx);
// wait for message
auto deadline = boost::posix_time::microsec_clock::universal_time() + timeout;
if (_cond.timed_wait(lk, deadline, [=] { return !_vec.empty(); })) {
out = _vec.back();
_vec.pop_back();
_cond.notify_all();
return true;
}
return false;
}
};
int main(int argc, char**) {
bip::managed_shared_memory segment(bip::open_or_create, "2fc51845-3d9b-442b-88ee-f6fd1725e8b0", 10<<20u);
auto& data = *segment.find_or_construct<SharedData>("sharedData")(segment.get_segment_manager());
if (argc>1) {
// "server"
std::cout << "Waiting for queue to be depletedn";
data.wait_for_empty();
for (int i = 0; i<20; ++i) {
std::cout << "Pushing " << i << "n";
data.push(i);
}
} else {
// "client"
int what;
while (data.pop(boost::posix_time::seconds(1), what))
std::cout << "Popped " << what << "n";
std::cout << "Timeout reached, byen";
}
}
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 提升进程间共享内存删除、权限和输出文件
- CLION无法打开输出文件,拒绝了权限
- 无法打开输出文件___.exe:权限被拒绝
- 无法打开输出文件:binDebugPrimeChecker.exe权限被拒绝
- 我不断收到一个错误,指出对输出文件的权限被拒绝,因此我的代码无法运行