在 Linux 下,POSIX 共享内存通常由tmpfs文件系统安装在/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/fstab or by systemd
.
在MacOS下,这个manual表示文件系统中没有共享内存段的可见条目:
在此实现中,创建的对象在文件系统中没有可见的条目。
因此,底层实现与 Linux 中的实现不同。您可能只能通过添加调用来访问共享内存ftruncate()
设置内存段的大小并使用mmap()
将内容映射到进程地址空间,因为这是我们通常使用共享内存的方式。任何想要访问该区域的进程都会执行相同的操作,只是只有一个进程应该指定O_CREAT to 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;
}
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. The manual后者明确规定了类似的操作read()
or write()
在生成的文件描述符上返回错误:
在共享内存对象上或在 shm_open() 返回的描述符上使用 open(2)、read(2) 或 write(2) 的结果是未定义的。共享内存对象本身或其内容是否在重新启动后仍然存在也是未定义的。
在 FreeBSD 中,共享内存对象上的 read(2) 和 write(2) 将因 EOPNOTSUPP 而失败,并且共享内存对象及其内容在重新启动后都不会保留。