#include <stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> #include<sys/mman.h> #include <string.h> int main() { int fd; char *start; char buf[100]; /*打开文件*/ fd = open("testfile",O_RDWR); start=mmap(NULL,100,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); //这个函数就是把一纸文件内容,映射到内存特定未位置的函数,null 为随机分配内存位置, //100为分配内存的大小,PORT_READ此映射内容为可读,fd文件返回地址,0为offset 就是从文件内容第一个数读起。 /* 读出数据 */ strcpy(buf,start); printf("buf = %s\n",buf); /* 写入数据 */ strcpy(start,"Buf Is Not Null!"); munmap(start,100); /*解除映射*/ close(fd); return 0; }
linux内核:地址映射机制来读取文件内容
原创TheRoodToDeath ©著作权
©著作权归作者所有:来自51CTO博客作者TheRoodToDeath的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:嵌入式工程师笔试面试题目
下一篇:C++运算优先级
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章