神奇的mmap系统调用

来源:互联网 发布:淘宝有没有隐藏订单 编辑:程序博客网 时间:2024/05/22 06:53
#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将打开的文件物理地址映射到用户虚拟地址空间,从而实现应用层直接操作物理文件

0 0
原创粉丝点击