我正在try 写入和读取使用shm_open打开的文件描述符.它可以在Linux上运行,但不能在MacOS(特别是MacOS蒙特雷12.5 21G72)上运行. 以下是代码:

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

int main(int argc, const char * argv[]) {
    int fd = shm_open("/example", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
    if (fd < 0) {
        printf("shm_open() failed %s (%d)\n", strerror(errno), errno);
        return 1;
    }
    
    const char *buf = "hello world";
    unsigned long len = strlen(buf);
        
    ssize_t ret = write(fd, buf, len);
    if (ret < 0) {
        printf("write() failed %s (%d)\n", strerror(errno), errno);
        return 1;
    }
    
    ret = lseek(fd, 0, SEEK_SET);
    if (ret < 0) {
        printf("lseek() failed %s (%d)\n", strerror(errno), errno);
        return 1;
    }
    
    char *newbuf = calloc(len + 1, 1);
    
    ret = read(fd, newbuf, len);
    if (ret < 0) {
        printf("read() failed %s (%d)\n", strerror(errno), errno);
        return 1;
    }
    
    printf("read: %s\n", newbuf);
    return 0;
}

在Linux上,输出如我所料:

$ cc main.c
$ ./a.out
read: hello world

在MacOS上,我得到的信息是:

$ cc main.c
$ ./a.out
write() failed Device not configured (6)

推荐答案

在Linux下,POSIX共享内存通常由一个挂载在/dev/shm:

$ cat /proc/mounts | grep /dev/shm
tmpfs /dev/shm tmpfs rw,nosuid,nodev,inode64 0 0

传递给shm_open()的名称是与共享内存区对应的文件条目的名称:

$ gcc main.c -lrt  
$ ./a.out 
read: hello world
$ ls -l /dev/shm
total 4
-rw------- 1 xxx xxx 11 sept.  17 08:53 example

上述文件系统通常在启动时通过/etc/fstabsystemd挂载.

在MacOS下,这manual表示文件系统中没有共享内存段的可见条目:

在此实现中,对于创建的对象,文件系统中没有可见的条目.

因此,底层实现不同于Linux中的实现.您可能只能通过添加对ftruncate()的调用来设置内存段的大小,并使用mmap()将内容映射到进程地址空间,就像我们通常使用共享内存的方式一样,才能访问共享内存.任何想要访问该区域的进程都将执行相同的操作,只是只有一个进程应指定O_CREATshm_open()并调用ftruncate()来创建对象/调整对象大小:

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

int main(int argc, const char * argv[]) {

    int flags = O_RDWR;

    // Pass some parameter to trigger the creation of the object
    if (argc > 1) {
      flags  |= O_CREAT;
    }

    int fd = shm_open("/example", flags, S_IRUSR|S_IWUSR);
    if (fd < 0) {
        printf("shm_open() failed %s (%d)\n", strerror(errno), errno);
        return 1;
    }
    
    const char *buf = "hello world";
    unsigned long len = strlen(buf);

    if (argc > 1) {

      ssize_t ret = ftruncate(fd, len + 1);
      if (ret < 0) {
          printf("ftruncate() failed %s (%d)\n", strerror(errno), errno);
          return 1;
      }

    }

    char *newbuf = (char *)mmap(NULL, len + 1, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (newbuf == MAP_FAILED) {
      printf("mmap() failed %s (%d)\n", strerror(errno), errno);
      return 1;
    }

    if (argc > 1) {

      memcpy(newbuf, buf, len + 1);

    }
  
    printf("read: %s\n", newbuf);
    return 0;
}

Linux下的执行示例:

$ gcc main.c -lrt
$ ls -l /dev/shm
total 0
$ ./a.out
shm_open() failed No such file or directory (2)
$ ls -l /dev/shm
total 0
$ ./a.out creat
read: hello world
$ ls -l /dev/shm
total 4
-rw------- 1 xxx xxx 12 sept.  17 09:36 example
$ ./a.out      
read: hello world

更多信息

MacOS是从BSD派生出来的.后者的manual明确指定对结果文件描述符执行类似read()write()的操作返回错误:

The result of using open(2), read(2), or write(2) on a shared memory object, or on the descriptor returned by shm_open(), is undefined. It is also undefined whether the shared memory object itself, or its contents, persist across reboots.
In FreeBSD, read(2) and write(2) on a shared memory object will fail with EOPNOTSUPP and neither shared memory objects nor their contents persist across reboots.

C++相关问答推荐

C sscanf没有捕获第二个参数

你能用自己的地址声明一个C指针吗?

malloc实现:判断正确的分配对齐

sizeof结果是否依赖于字符串的声明?

在c++中使用堆栈的有效括号

Rust FFI--如何用给出返回引用的迭代器包装C风格的迭代器?

是什么让numpy.sum比优化的(自动矢量化的)C循环更快?

函数的限制限定指针参数允许优化调用方函数吗?

如何有效地编写代码来判断两个元素数量相同的数组即使在不同的位置也具有相同的元素?

GTK函数调用将完全不相关的char* 值搞乱

可变宏不能编译

C标准关于外部常量的说明

Fscanf打印除退出C代码为1的程序外的所有内容

当我在34mb的.mp4文件中使用FREAD时,我得到了一个分段错误,我如何解决它?

将size_t分配给off_t会产生符号转换错误

哪些C++功能可以在外部C块中使用

无法理解 fgets 输出

为什么 Linux 共享库 .so 在内存中可能比在磁盘上大?

使用复合文字数组初始化的指针数组

如何修复数组数据与列标题未对齐的问题?