Boost Interprocess share memory deletion, permissions and output files

喜你入骨 提交于 2019-12-08 05:33:43

问题


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));

and the following to delete:

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;
}

(Relying on RAII for the destructor to delete the shared memory)

I have three questions:

  1. Do I need to delete the vector, because it's part of the segment anyway?

  2. 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?

  3. 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.


回答1:


  1. Do I need to delete the vector, because it's part of the segment anyway?

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).

  1. 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?

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.

E.g. if you create the segment as root, you should not be able to open it as another user.

  1. 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.

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.

I'd suggest that the temporary files might be an artifact of other programs (like your IDE?)

Suggested Simplifications:

#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());
}

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";
    }

}


来源:https://stackoverflow.com/questions/50289336/boost-interprocess-share-memory-deletion-permissions-and-output-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!