how do i change the shm_open path?

后端 未结 2 347
粉色の甜心
粉色の甜心 2021-01-14 16:22

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 try

相关标签:
2条回答
  • 2021-01-14 16:27

    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.

    0 讨论(0)
  • 2021-01-14 16:52

    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.

    0 讨论(0)
提交回复
热议问题