首页 > 解决方案 > 修复地址的 mmap 失败

问题描述

我想将存储在 ramdisk 中的文件映射到固定地址,但 mmap 失败。我在做什么错误?有没有其他方法可以做到这一点?

int fd =  open("/mnt/tmpfs/hello.txt",
                            (O_RDONLY),
                            (S_IRWXU | S_IRWXG | S_IRWXO) );
if ( fd < 0 )
     perror("open() error");

struct stat buf;
fstat(fd, &buf);
void * address = aligned_alloc(PGSIZE, buf.st_size)
void *new_address = mmap(address,
                      buf.st_size,
                      PROT_READ,
                      (MAP_SHARED|MAP_FIXED),
                      fd,
                      0);
if (new_address != address) {
    perror("mmap failed!")
}

标签: clinuxmmap

解决方案


推荐阅读