From the man page of shm_open(3)
:
name
specifies the shared memory object to be created or opened. For portable use, a shared memory object should be identified by a name of the form /somename
; that is, a null-terminated string of up to NAME_MAX
(i.e., 255) characters consisting of an initial slash, followed by one or more characters, none of which are slashes.
The name
parameter of shm_open(3)
is an object name, not a file path! It just happens that GLIBC places all shared memory objects in /dev/shm
or /var/run/shm
by prepending the path to the object name and calling open()
on the resulting name. If you specify /tmp/test
as the shared object name then Linux would try to open or create /var/run/shm/tmp/test
. Open with O_CREAT
creates new files but does not create new directories.
Your test will work if your first create the directory /var/run/shm/tmp
before the call to shm_open("/tmp/test", ...)
. Remember to remove it once you have finished working with the shared memory object. And also note that using object name with two slashes inside might not be portable to other Unix systems.