我有一个GNU /
Linux应用程序,它使用了许多共享内存对象.它可能会在同一系统上运行多次.为了保持整洁,我首先在/ dev / shm中为每个共享内存对象集创建一个目录.
问题是在较新的GNU / Linux发行版上,我似乎不再能够在/ dev / shm的子目录中创建它们.
以下是一个最小的C程序,说明了我在说什么:
/*****************************************************************************
* shm_minimal.c
*
* Test shm_open()
*
* Expect to create shared memory file in:
* /dev/shm/
* └── my_dir
* └── shm_name
*
* NOTE: Only visible on filesystem during execution. I try to be nice, and
* clean up after myself.
*
* Compile with:
* $gcc -lrt shm_minimal.c -o shm_minimal
*
******************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[]) {
int shm_fd = -1;
char* shm_dir = "/dev/shm/my_dir";
char* shm_file = "/my_dir/shm_name"; /* does NOT work */
//char* shm_file = "/my_dir_shm_name"; /* works */
// Create directory in /dev/shm
mkdir(shm_dir, 0777);
// make shared memory segment
shm_fd = shm_open(shm_file, O_RDWR | O_CREAT, 0600);
if (-1 == shm_fd) {
switch (errno) {
case EINVAL:
/* Confirmed on:
* kernel v3.14, GNU libc v2.19 (ArchLinux)
* kernel v3.13, GNU libc v2.19 (Ubuntu 14.04 Beta 2)
*/
perror("FAIL - EINVAL");
return 1;
default:
printf("Some other problem not being tested\n");
return 2;
}
} else {
/* Confirmed on:
* kernel v3.8, GNU libc v2.17 (Mint 15)
* kernel v3.2, GNU libc v2.15 (Xubuntu 12.04 LTS)
* kernel v3.1, GNU libc v2.13 (Debian 6.0)
* kernel v2.6.32, GNU libc v2.12 (RHEL 6.4)
*/
printf("Success !!!\n");
}
// clean up
close(shm_fd);
shm_unlink(shm_file);
rmdir(shm_dir);
return 0;
}
/* vi: set ts=2 sw=2 ai expandtab:
*/
当我在一个相当新的发行版上运行这个程序时,对shm_open()的调用返回-1,并且errno设置为EINVAL.但是,当我运行较旧的东西时,它会按预期在/ dev / shm / my_dir中创建共享内存对象.
对于更大的应用程序,解决方案很简单.我可以使用公共前缀而不是目录.
如果你可以帮助我了解这种明显的行为改变,那将会非常有帮助.我怀疑其他人可能会尝试做类似的事情.