小编beg*_*_EN的帖子

总线错误打开和mmap'文件

我想创建一个文件并将其映射到内存中.我认为我的代码可以工作,但是当我运行它时,我得到一个"总线错误".我搜索谷歌,但我不知道如何解决问题.这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>

int main(void)
{
    int file_fd,page_size;
    char buffer[10]="perfect";
    char *map;

    file_fd=open("/tmp/test.txt",O_RDWR | O_CREAT | O_TRUNC ,(mode_t)0600);

    if(file_fd == -1)
    {
        perror("open");
        return 2;
    }

    page_size = getpagesize();

    map = mmap(0,page_size,PROT_READ | PROT_WRITE,MAP_SHARED,file_fd,page_size);

    if(map == MAP_FAILED)
    {
        perror("mmap");
        return 3;
    }

    strcpy(map, buffer);

    munmap(map, page_size);
    close(file_fd);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

linux filesystems operating-system

8
推荐指数
1
解决办法
6409
查看次数

标签 统计

filesystems ×1

linux ×1

operating-system ×1