问题
i am currently developing an application on ubunto and calling shm_open, currently the default path is within /var/run/shm. however i need to change this to /tmp. simply trying the following does not work:
fd = shm_open( "/tmp/test", O_RDWR | O_CREAT, 0777 );
can anyone please advise?
回答1:
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 toNAME_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.
回答2:
You need to mount a tmpfs
filesystem in /tmp
for this:
mihai@keldon:~$ mount | grep shm
shm on /dev/shm type tmpfs (rw,nosuid,nodev,relatime)
Otherwise, it is not possible.
来源:https://stackoverflow.com/questions/11102599/how-do-i-change-the-shm-open-path