相关疑难解决方法(0)

无法在 MacOS 上从 shm_open 写入 fd

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

c macos posix shared-memory

5
推荐指数
1
解决办法
856
查看次数

标签 统计

c ×1

macos ×1

posix ×1

shared-memory ×1