Skip to content
Advertisement

POSIX shared memory model

Is there a memory model specification for POSIX shared memory (across processes)?

I’m hoping that there’s something to comparable to the C++ memory model, in order to answer questions like the following:

  1. Is there a definition of a data race?
  2. Are data races undefined behavior (as in C++)?
  3. Is there something to constrain memory visibility order across processes? I know that a pthread_mutex can be put in shared memory and used by multiple processes, but does that induce a happens before relation?
  4. Do memory barriers play a roll?

Advertisement

Answer

According to POSIX specification, a shared memory object is defined as

An object that represents memory that can be mapped concurrently into the address space of more than one process.

On platforms as Linux, the memory object when mapped is equivalent to an anonymous mapping: this is a mapping of the physical RAM (or swap memory) to the virtual address space of the process. This is what is usually the memory of any thread or process.

On such plateforms as Linux you can expect that low memory level synchronization primitive provided by atomic will provide interprocess synchronization. According to your search pthread_mutex will work (probably because they are only implement in term of a compare and exchange loop and the futex system call). But std::mutex may also be broken.

Nevertheless I do not see in the POSIX specification anything that would forbid shared memory to be backed by the file system. On such platform memory synchronization would be broken. But I suppose no such platform exists.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement