您的位置:首页 > 其它

神奇的mmap系统调用

2017-04-25 18:21 260 查看
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <sys/mman.h>

int main(int argc, char **argv)
{
if(2 != argc)
{
printf("Usage: %s <file>\n", argv[0]);
return 0;
}

int fd = open(argv[1], O_RDONLY);
if(-1 == fd)
{
perror("open");
return -1;
}

// void *mmap(void *addr, size_t length, int prot, int flags,
//                  int fd, off_t offset);
// 将fd所代表的文件映射到用户的虚拟地址空间
char *p = mmap(NULL, 10240, PROT_READ, MAP_SHARED, fd, 0);
printf("%s", p);

close(fd);
}

mmap将打开的文件物理地址映射到用户虚拟地址空间,从而实现应用层直接操作物理文件

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mmap