Sed*_*ien 5 c macos posix shared-memory
我正在尝试写入然后从使用打开的文件描述符中读取shm_open。它在 Linux 上按我的预期工作,但在 macOS 上却不行(特别是 macOS Monterey 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;
}
Run Code Online (Sandbox Code Playgroud)
在 Linux 上,输出是我所期望的:
$ cc main.c
$ ./a.out
read: hello world
Run Code Online (Sandbox Code Playgroud)
在 macOS 上我得到这个:
$ cc main.c
$ ./a.out
write() failed Device not configured (6)
Run Code Online (Sandbox Code Playgroud)
在 Linux 下,POSIX 共享内存通常由安装在/dev/shm上的tmpfs文件系统支持:
$ cat /proc/mounts | grep /dev/shm
tmpfs /dev/shm tmpfs rw,nosuid,nodev,inode64 0 0
Run Code Online (Sandbox Code Playgroud)
传递给的名称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
Run Code Online (Sandbox Code Playgroud)
上述文件系统通常在启动时通过/etc/fstab或systemd.
在 MacOS 下,本手册说文件系统中没有共享内存段的可见条目:
在此实现中,创建的对象在文件系统中没有可见的条目。
因此,底层实现与 Linux 中的实现不同。您可能只能通过添加调用来ftruncate()设置内存段的大小并使用mmap()将内容映射到进程地址空间来访问共享内存,因为这是我们通常使用共享内存的方式。任何想要访问该区域的进程都会执行相同的操作,只是只有一个进程应该指定O_CREAT并shm_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;
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
MacOS 源自BSD。后者的手册明确指出,类似或对结果文件描述符的操作会返回read()错误write():
在共享内存对象上或在 shm_open() 返回的描述符上使用 open(2)、read(2) 或 write(2) 的结果是未定义的。共享内存对象本身或其内容是否在重新启动后仍然存在也是未定义的。
在 FreeBSD 中,共享内存对象上的 read(2) 和 write(2) 将因 EOPNOTSUPP 而失败,并且共享内存对象及其内容在重新启动后都不会保留。