简体   繁体   English

我该如何更改shm_open路径?

[英]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. 我目前正在ubunto上开发一个应用程序并调用shm_open,目前默认路径在/ var / run / shm内。 however i need to change this to /tmp. 但是我需要将其更改为/ tmp。 simply trying the following does not work: 只是尝试以下操作不起作用:

fd = shm_open( "/tmp/test", O_RDWR | O_CREAT, 0777 ); fd = shm_open(“/ tmp / test”,O_RDWR | O_CREAT,0777);

can anyone please advise? 任何人都可以建议吗?

From the man page of shm_open(3) : shm_open(3)的手册页:

name specifies the shared memory object to be created or opened. name指定要创建或打开的共享内存对象。 For portable use, a shared memory object should be identified by a name of the form /somename ; 对于便携式使用,共享内存对象应使用/somename形式的名称/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 . 也就是说,一个以空字符结尾的字符串,最多NAME_MAX (即255个)字符,由一个初始斜杠组成,后跟一个或多个字符, 其中没有一个是斜杠

The name parameter of shm_open(3) is an object name, not a file path! shm_open(3)name参数是对象名,而不是文件路径! 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. 只是GLIBC将所有共享内存对象放在/dev/shm/var/run/shm是在路径前加上对象名,并在结果名称上调用open() If you specify /tmp/test as the shared object name then Linux would try to open or create /var/run/shm/tmp/test . 如果指定/tmp/test作为共享对象名,则Linux将尝试打开或创建/var/run/shm/tmp/test Open with O_CREAT creates new files but does not create new directories . 使用O_CREAT打开会创建新文件,但不会创建新目录

Your test will work if your first create the directory /var/run/shm/tmp before the call to shm_open("/tmp/test", ...) . 如果您在调用shm_open("/tmp/test", ...)之前首先创建目录/var/run/shm/tmp ,那么您的测试将起作用。 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. 并且还要注意,使用带有两个斜杠的对象名称可能无法移植到其他Unix系统。

You need to mount a tmpfs filesystem in /tmp for this: 你需要在/tmp安装一个tmpfs文件系统:

mihai@keldon:~$ mount | grep shm
shm on /dev/shm type tmpfs (rw,nosuid,nodev,relatime)

Otherwise, it is not possible. 否则,这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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