我正在尝试写入然后从使用打开的文件描述符中读取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), …Run Code Online (Sandbox Code Playgroud)