etherice
etherice

Reputation: 1821

Is it safe to perform file-based (fd-based) access on a POSIX shared memory object?

shm_open returns an fd associated with a "shared memory object". And normally, this object is then mapped into virtual memory (with mmap) to access as a memory-mapped file.

However, is it safe to access the shared memory object through its fd (as if it were a normal file)? And by "safe", I mean guaranteed by POSIX to have well-defined behavior?

The reason I'm asking is that I'm working with a third-party interface that will only accept an fd (instead of a memory address) for writing a fixed number of bytes.

Upvotes: 2

Views: 159

Answers (1)

caf
caf

Reputation: 239311

Unfortunately, no. POSIX says:

If fildes refers to a shared memory object, the result of the write() function is unspecified.

..and the same for read().

It does work on Linux, though, where a shared memory object is a file backed by the tmpfs filesystem.

Upvotes: 4

Related Questions