C++ Boost进程间共享内存删除、权限和输出文件
我正在使用Boost进程间库在两个进程之间共享内存 我使用以下方法分配共享内存块、附加向量、命名互斥体和命名条件变量:C++ Boost进程间共享内存删除、权限和输出文件,c++,memory-management,boost,shared-memory,boost-interprocess,C++,Memory Management,Boost,Shared Memory,Boost Interprocess,我正在使用Boost进程间库在两个进程之间共享内存 我使用以下方法分配共享内存块、附加向量、命名互斥体和命名条件变量: using ShmemAllocator = allocator<T, managed_shared_memory::segment_manager>; using MyVector = vector<T, ShmemAllocator>; segment.reset(new managed_shared_memory(create_only, blo
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));
为了检查内存是否被正确删除,我运行了一个压力测试:
while(counter < 1000000)
{
MySharedMemoryObj s;
++counter;
}
while(计数器<1000000)
{
MySharedMemoryObj s;
++计数器;
}
(析构函数依赖RAII删除共享内存)
我有三个问题:
outputxxxxxxxxxx
的二进制文件。这些是什么?它们不会被删除,因此会累积根用户
,则不能以其他用户的身份打开它
/dev/shm
中,我认为不需要临时文件
我建议临时文件可能是其他程序(比如IDE?)的产物
建议的简化:
#包括
#包括
#包括
#包括
名称空间bip=boost::进程间;
静态自动块名称=“4f72909d-8265-4260-9bb1-6bd58f63812c”;
静态自动共享VectorName=“54714711”;
静态自动mutexName=“b4eb63e0”;
静态自动条件vname=“f7a95857”;
使用ShmemAllocator=bip::allocator的模板;
使用MyVector=std::vector的模板;
int main(){
bip::受管_共享_内存段(bip::仅创建_,“块名”,10使用anomymous互斥体/条件和队列Demous提供更多建议谢谢你的回复,我真的很感激。我的两个进程将从不同的用户帐户运行。有没有办法创建共享内存,以便可以在任何用户下启动读取进程?是的。修改文件权限,使两个用户都有访问权限ss(例如,将其添加到同一组)。在windows上,有安全属性。请参阅,谢谢您的回复。这是Linux。第一个进程的用户拥有最高的用户权限。第二个进程在为软件供应商创建的用户下运行,他们的软件必须在该帐户下运行。您的建议会给供应商用户与我们的用户相同的权限吗?显然,我们不希望重新阅读:“修改文件权限,使两个用户都能访问(例如,通过将他们添加到同一个组)”这是基本的UNIX文件权限。通常要做的是创建一个组,使共享文件归该组所有(chgrp
/chown
),授予组相应的权限(chmod g+rw
),并添加需要访问该组的所有用户(adduser
)。这与大多数服务相同,如wwwroot
,adm
,plugdev
,docker
等。在我的家庭桌面上,有一些用户在kids
组:)这需要一些计划,但UNIX权限在概念上很简单
while(counter < 1000000)
{
MySharedMemoryObj s;
++counter;
}
#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());
}
#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";
}
}