1.http://blog.youkuaiyun.com/mg0832058/article/details/5890688
该博客对mmap的原理讲述的很清晰
2.http://blog.youkuaiyun.com/yinjiabin/article/details/7575653
该博客讲述其用法
我的代码:
#include <stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
int fdin, fdout;
void *src = NULL;
void *dest = NULL;
struct stat statbuf;
if(argc != 3)
{
printf("usage:%s <fromfile> <tofile>\n", argv[0]);
}
if((fdin = open(argv[1], O_RDONLY)) < 0)
{
printf("can't open %s for reading", argv[1]);
}
if((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666)) < 0)
{
printf("can' creating %s for writing", argv[2]);
}
if(fstat(fdin, &statbuf) < 0)
{
perror("fstat error");
}
if(lseek(fdout, statbuf.st_size -1, SEEK_SET) == -1)
{
perror("lseek error");
}
if(write(fdout, "", 1) != 1)
{
perror("write error");
}
if((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) == MAP_FAILED)
{
perror("map error for input");
}
if((dest = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0)) == MAP_FAILED)
{
perror("map error for output");
}
memcpy(dest, src, statbuf.st_size);
munmap(src, statbuf.st_size);
munmap(dest, statbuf.st_size);
close(fdin);
close(fdout);
exit(0);
}
用seq命令产生输入数据