您的位置:首页 > 其它

mmap

2013-09-25 01:03 239 查看
//mmap把磁盘上文件映象到内存中。

//GNU/Linux===p259

#include<sys/types.h>

#include<sys/mman.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<stdlib.h>

#include<stdio.h>

void err_quit(char *msg)

{

perror(msg);

exit(EXIT_FAILURE);

}

int main(int argc,char *argv[]){

int fd;

struct stat buf;

char *src;

off_t len;

if((fd=open(argv[1],O_RDONLY))<0)

err_quit("open");

fstat(fd,&buf);

len=buf.st_size;//获得要映射文件的大小

src=mmap(0,len,PROT_READ,MAP_SHARED,fd,0);

printf("%s",src);

close(fd);

exit(EXIT_SUCCESS);

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