我正在尝试打开一个共享内存,它给我没有这样的文件或目录错误。 但是我在名称区域中有一个文件和一个目录。
fd_sh = shm_open("/home/angus/c_tutorials/interview/linux_sys_pgm/mmap/region", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if(fd_sh == -1){
perror("fd_sh:ERROR");
return -1;
}
最佳答案
在 Linux 中(我假设这是你的操作系统,给定你的代码),名称应该以斜杠开头,但后面没有任何其他内容,例如"/myshm"
-- 不是常规文件名。
来自手册页:
The operation of shm_open() is analogous to that of open(2). name
specifies the shared memory object to be created or opened. For porta‐
ble 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.
这样做会很好。
事实上,给定的名称是在 /dev/shm
中创建的文件,因此您需要创建目录结构以使用路径;这不是一个好主意,因为该目录仅在内存中。
关于c - shm_open() 函数未创建共享内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15644489/