linux - shm_open() 不创建共享内存段 ipcs

标签 linux shared-memory ipcs

我得到了以下成功执行的 C 代码:

...
fd = shm_open(memory_package_name, O_CREAT | O_RDWR | O_EXCL , S_IRUSR | S_IWUSR);

if (fd == -1) { //will fail if file already exists because of flag O_EXCL
    printf("\n shm_open() failed with error [%s]\n",strerror(errno));
    exit(EXIT_FAILURE);
} else {
    printf("shm_open success\n");
}
...

使用 ls -l/dev/shm 我看到正确创建了 name memory_package_name 的文件,但是当我使用 ipcs 时它没有t 显示任何共享内存段:

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

为什么?

最佳答案

你试过 shmget()shmat() 了吗? 无论如何,如果您需要在两个不相关的进程之间共享内存,您确实需要 shm_open() ,但我认为您的案例中缺少的一点是使用 附加到该内存区域的调用shmat().

关于linux - shm_open() 不创建共享内存段 ipcs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764549/

相关文章:

java - 当代码推送到远程服务器时,maven 依赖不支持

algorithm - 什么是好的(半)异步算法?

client-server - 为什么某些 posix 共享内存段和 posix 信号量对 ipc 不可见

linux - PHP fopen "failed to open stream: resource temporarily unavailable"

java - Windows 和 Linux 的应用程序开发

linux - (nodemon) 应用程序崩溃 - 在开始之前等待文件更改

java - 在 WebSphere7 中,当对象保留内存和共享类缓存已满 100% 时,Dozer 中出现 ClassCastException

c++ - 共享内存初始化和访问

c++ - 使用 C++ 程序删除 unix 共享内存段

shared-memory - 在 Linux 中使用 ipcrm 删除共享内存