尽管再次使用O_CREAT,但以相同的名称重新打开时shm_open()失败

年轻:

根据http://man7.org/linux/man-pages/man3/shm_open.3.html,它表示

 After a successful shm_unlink(), attempts to shm_open() an object with the same name fail (unless O_CREAT was
 specified, in which case a new, distinct object is created).

S,我尝试过这个。我正在使用以下示例,该示例在执行shm_unlink之后创建了新的共享内存对象,正如他们所说的,我使用了O_CREAT。

但是当我运行此问题时,它给我带来了与错误有关的信息bus error

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

int main(void) {
    // Open shared memory
    int fd = shm_open("TEST", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    ftruncate(fd, sizeof(int));

    // Map shared memory
    int *shm = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
    close(fd);

    // Access shared memory
    *shm = 0;

    // Unmap shared memory
    munmap(shm, sizeof(int));

    if(shm_unlink("TEST")){
        printf("************success****************");
    }

    fd = shm_open("TEST", O_CREAT |O_RDWR, S_IRUSR | S_IWUSR);
    int *shm2 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
    *shm2 = 0;

    return 0;
}

在执行shm_unlink之后,再次创建具有相同名称的共享内存的正确过程是什么?

解开:

第二次尝试访问错误的共享内存(应该是shm2,而不是shm),并且不要忘记截断。

同样不相关,但是shm_unlink成功返回0。

#include <sys/mman.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(void) {
    // Open shared memory
    int fd = shm_open("TEST", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    ftruncate(fd, sizeof(int));

    // Map shared memory
    int *shm = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
    close(fd);

    // Access shared memory
    *shm = 0;

    // Unmap shared memory
    munmap(shm, sizeof(int));

    if(!shm_unlink("TEST")){
        printf("************success****************");
    }

    fd = shm_open("TEST", O_CREAT |O_RDWR, S_IRUSR | S_IWUSR);
    int *shm2 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);
    ftruncate(fd, sizeof(int));
    *shm2 = 0;

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么要使用shm_open?

在/ dev / shm的子目录中创建共享内存时,shm_open()失败并显示EINVAL

open(名称,O_CREAT | O_DIRECTORY,模式)的预期行为是什么?

shm_open()和ftruncate()的目的?

设置O_CREAT位但仅提供两个open()参数时会发生什么?

如何在mmap中正确使用shm_open

shm_open()导致没有这样的文件或目录

即使使用-pthread -lrt进行编译,也未定义对“ shm_open”的引用

打开:没有这样的文件或目录,带有O_CREAT

共享内存-shm_open导致权限被拒绝

在Lua中使用相同名称重新声明变量是否合法?

shm_open使用什么类型的内存对象?

未定义对“ shm_open”的引用

在shm_open()之后使用fallocate()导致在shm_unlink()之后没有释放内存

无法使用相同的名称重新创建已删除的Kafka主题

shm_open函数的正确Java映射是什么?

如何使用mmap和shm_open在多个独立进程之间共享内存

O_CREAT定义错误

如何知道C ++中的`shm_open`使用的共享内存的可用大小?

现在,对ios7的shm_open调用导致O_CREAT(EPERM)期间不允许进行操作

删除工作表并使用相同的工作表名称重新创建

RunOnce在重新启动时使用随机名称重命名计算机名

如何使用shm_open打开现有的共享内存对象

使用O_CREAT进行open()调用的默认模式是什么,以及在打开/创建文件时如何正确设置它

OSX:shm_open返回ENAMETOOLONG

文件已存在时,使用O_CREAT的Open返回(-1)

shm_open 是否提交固定数量的物理内存?

使用 shm_open 将整数从父进程复制到子进程

在 debian Bulleye 上编译 ocaml 项目时出现问题:对 shm_unlink 和 shm_open 的未定义引用