简体   繁体   中英

how do i change the shm_open path?

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?

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 (ie, 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM