Sample Header Ad - 728x90

Which consistency guarantees do POSIX shared memory objects have?

2 votes
0 answers
233 views
On Linux, POSIX shared memory objects use a tmpfs via /dev/shm. A tmpfs in turn is said to "live completely in the page cache" (I'm assuming swap has not been enabled). I am wondering what the consistency / no-tearing guarantees are when using a POSIX SHM object, mmaped into a programs address space. Example: Assume a POSIX SHM object shared between two processes A and B, both mmaped into their respective address space. The size of that object is 8kB or two pages, assuming 4kB pages and the object being page-aligned. 1. A issues two sequential writes, the first writes into the first page (first 4k block), the second into the second page. 2. B polls the shared object / both pages. Is it possible that the reads of B are torn, meaning that B reads a fresh and updated second page but a stale first page? \ https://www.man7.org/linux/man-pages/man7/shm_overview.7.html \ https://www.kernel.org/doc/html/latest/filesystems/tmpfs.html \ This would be the associated pseudo-code in C:
int fd = shm_open(...);
void *share = mmap(0, 8192, $flags, fd, 0);
memcpy(share       , data1, 4096);
memcpy(share + 4096, data2, 4096);
Asked by Philipp Friese (21 rep)
Mar 19, 2024, 08:54 AM