问题
I am creating my shared memory using the below, trying either to open or create and setting unrestricted permissions.
void createMemory(const int numBytes)
{
permissions perm;
perm.set_unrestricted();
segment.reset(new managed_shared_memory(open_or_create, memory_name, numBytes, 0, perm));
// Exception throw on this line
mutex.reset(new named_mutex(open_or_create, mutex_name, perm));
cond_empty.reset(new named_condition(open_or_create, cv_name, perm));
const ShmemAllocator alloc_inst(segment->get_segment_manager());
vec = segment->find_or_construct<MyVector>(vector_name)(alloc_inst);
}
This creates two files for named_mutex, the shared memory and the (semaphore?) file:
mutex_name
sem.mutex_name
The first file has permissions which are fine, but the second file is created with restrictive permissions, preventing the second user from opening it.
I have configured user in the bashrc profile of the first user, but that doesn't seem to fix the problem. How else can I force this sem file to have relaxed permissions?
回答1:
This answers it:
POSIX shared memory and semaphores permissions set incorrectly by open calls
You set umask programatically to zero and then restore the previous umask value.
来源:https://stackoverflow.com/questions/50348027/boost-interprocess-named-mutex-semaphore-file-permissions