增强进程间共享内存的删除,权限和输出文件 [英] Boost Interprocess share memory deletion, permissions and output files
问题描述
我正在使用Boost Interprocess库在两个进程之间共享内存.
I am using Boost Interprocess library to share memory between two processes.
我正在使用以下方法分配共享内存块,一个附加的向量,名为互斥体和一个命名的条件变量:
I am using the following to allocate shared memory block, an attached vector, named mutex and a named condition variable:
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);
要检查内存是否已正确删除,我进行了压力测试:
To check the memory was being deleted correctly I ran a stress test:
while(counter < 1000000)
{
MySharedMemoryObj s;
++counter;
}
(依靠RAII的析构函数来删除共享内存)
(Relying on RAII for the destructor to delete the shared memory)
我有三个问题:
-
我是否需要删除向量,因为它仍然是该片段的一部分?
Do I need to delete the vector, because it's part of the segment anyway?
上面的方法有效,但在特定情况下却没有,并引发了Boost Interprocess异常,表示它没有访问内存的权限.是什么原因导致此/是否有避免的方法?
The above works, but on one particular occasion it didn't and threw a Boost Interprocess exception, saying it did not have permission to access the memory. What causes this/is there a way to avoid it?
我注意到上面的代码似乎在/tmp中生成名为outputXXXXXXXXXXX
的二进制文件.这些是什么?它们不会被删除,因此会累积.
I have noticed that the above code seems to generate binary files in /tmp named outputXXXXXXXXXXX
. What are these? They aren't being deleted and therefore accumulate.
推荐答案
- 我是否需要删除矢量,因为它仍然是细分的一部分?
从技术上讲,在这里不是真的(假设您也使用共享内存分配器).但是,跳过销毁是一个坏习惯,尤其是在您的析构函数具有逻辑(不是琐碎)的情况下.
Technically, not really here (assuming you use the shared-memory allocator too). However, it's a bad practice to skip destruction especially if your destructor has logic (is not trivial).
- 以上方法有效,但在特定情况下却没有,并引发了Boost Interprocess异常,表示它没有访问内存的权限.是什么原因导致这一/是否有避免的方法?
在创建共享段时,请确保程序与目标用户一起运行.这就是赋予它文件级访问权限的原因.
Make sure that the program runs as with the intended user when creating the shared segment. This is what lends it the file-level access permissions.
例如如果您将段创建为root
,则您将不能以其他用户身份打开它.
E.g. if you create the segment as root
, you should not be able to open it as another user.
- 我注意到上面的代码似乎在/tmp中生成了名为outputXXXXXXXXXXX的二进制文件.这些是什么?它们不会被删除,因此会累积.
那没有什么意义.这些路径表明您使用的是POSIX系统.在POSIX上,shmem通常存在于/dev/shm
中,我看不到需要临时文件.
That doesn't make a lot of sense. The paths suggest you're on a POSIX system. On POSIX shmem usually exists in /dev/shm
and I don't see a need for temporary files.
我建议这些临时文件可能是其他程序(例如您的IDE?)的产物
I'd suggest that the temporary files might be an artifact of other programs (like your 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);
}
或者,根据要同步的内容,使同步原语成为共享数据的成员:
Or, depending on what you want to synchronize, make the synchronization primitives members of the shared data:
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个元素的队列建模.
Simple Multi-Producer/Multi Consumer Queue:
Models a queue with maximum capacity of 10 elements.
#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 depleted\n";
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, bye\n";
}
}
这篇关于增强进程间共享内存的删除,权限和输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!