如何通过mmap技术高效实现文件复制操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计727个文字,预计阅读时间需要3分钟。
c#include #include #include #include #include
int main(int argc, char *argv[]) { if (argc !=3) { fprintf(stderr, Usage: %s \n, argv[0]); return 1; }
const char *source_file=argv[1]; const char *destination_file=argv[2];
int source_fd=open(source_file, O_RDONLY); if (source_fd==-1) { perror(Error opening source file); return 1; }
int destination_fd=open(destination_file, O_RDWR | O_CREAT | O_TRUNC, 0644); if (destination_fd==-1) { perror(Error opening destination file); close(source_fd); return 1; }
off_t source_size=lseek(source_fd, 0, SEEK_END); if (source_size==-1) { perror(Error getting source file size); close(source_fd); close(destination_fd); return 1; }
char *source_map=mmap(NULL, source_size, PROT_READ, MAP_PRIVATE, source_fd, 0); if (source_map==MAP_FAILED) { perror(Error mapping source file); close(source_fd); close(destination_fd); return 1; }
char *destination_map=mmap(NULL, source_size, PROT_READ | PROT_WRITE, MAP_SHARED, destination_fd, 0); if (destination_map==MAP_FAILED) { perror(Error mapping destination file); munmap(source_map, source_size); close(source_fd); close(destination_fd); return 1; }
memcpy(destination_map, source_map, source_size);
munmap(source_map, source_size); munmap(destination_map, source_size);
close(source_fd); close(destination_fd);
return 0;}
利用mmap实现的一个文件拷贝例子,供大家参考,具体内容如下
/* * gcc -Wall -O3 -o copy_mmap copy_mmap.c */ #include < stdio.h > #include < stdlib.h > #include < string .h > /* for memcpy */ #include < strings.h > #include < sys / mman.h > #include < sys / types.h > #include < sys / stat.h > #include < fcntl.h > #include < unistd.h > #define PERMS 0600 int main ( int argc, char * argv[] ) { int src, dst; void * sm, * dm; struct stat statbuf; if ( argc != 3 ) { fprintf( stderr, " Usage: %s \n " , argv[ 0 ] ); exit( EXIT_FAILURE ); } if ( ( src = open( argv[ 1 ], O_RDONLY ) ) < 0 ) { perror( " open source " ); exit( EXIT_FAILURE ); } /* 为了完成复制,必须包含读打开,否则mmap()失败 */ if ( ( dst = open( argv[ 2 ], O_RDWR | O_CREAT | O_TRUNC, PERMS ) ) < 0 ) { perror( " open target " ); exit( EXIT_FAILURE ); } if ( fstat( src, & statbuf ) < 0 ) { perror( " fstat source " ); exit( EXIT_FAILURE ); } /* * 参看前面man手册中的说明,mmap()不能用于扩展文件长度。所以这里必须事 * 先扩大目标文件长度,准备一个空架子等待复制。 */ if ( lseek( dst, statbuf.st_size - 1 , SEEK_SET ) < 0 ) { perror( " lseek target " ); exit( EXIT_FAILURE ); } if ( write( dst, & statbuf, 1 ) != 1 ) { perror( " write target " ); exit( EXIT_FAILURE ); } /* 读的时候指定 MAP_PRIVATE 即可 */ sm = mmap( 0 , ( size_t )statbuf.st_size, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, src, 0 ); if ( MAP_FAILED == sm ) { perror( " mmap source " ); exit( EXIT_FAILURE ); } /* 这里必须指定 MAP_SHARED 才可能真正改变静态文件 */ dm = mmap( 0 , ( size_t )statbuf.st_size, PROT_WRITE, MAP_SHARED, dst, 0 ); if ( MAP_FAILED == dm ) { perror( " mmap target " ); exit( EXIT_FAILURE ); } memcpy( dm, sm, ( size_t )statbuf.st_size ); /* * 可以不要这行代码 * * msync( dm, ( size_t )statbuf.st_size, MS_SYNC ); */ return ( EXIT_SUCCESS ); }
mmap()好处是处理大文件时速度明显快于标准文件I/O,无论读写,都少了一次用户空间与内核空间之间的复制过程。操作内存还便于设计、优化算法。
文件I/O操作/proc/self/mem不存在页边界对齐的问题,但至少Linux的mmap()的最后一个形参offset并未强制要求页边界对齐,如果提供的值未对齐,系统自动向上舍入到页边界上。malloc()分配得到的地址不见得对齐在页边界上。
/proc/self/mem和/dev/kmem不同。root用户打开/dev/kmem就可以在用户空间访问到内核空间的数据,包括偏移0处的数据,系统提供了这样的支持。显然代码段经过/proc/self/mem可写映射后已经可写,无须mprotect()介入。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计727个文字,预计阅读时间需要3分钟。
c#include #include #include #include #include
int main(int argc, char *argv[]) { if (argc !=3) { fprintf(stderr, Usage: %s \n, argv[0]); return 1; }
const char *source_file=argv[1]; const char *destination_file=argv[2];
int source_fd=open(source_file, O_RDONLY); if (source_fd==-1) { perror(Error opening source file); return 1; }
int destination_fd=open(destination_file, O_RDWR | O_CREAT | O_TRUNC, 0644); if (destination_fd==-1) { perror(Error opening destination file); close(source_fd); return 1; }
off_t source_size=lseek(source_fd, 0, SEEK_END); if (source_size==-1) { perror(Error getting source file size); close(source_fd); close(destination_fd); return 1; }
char *source_map=mmap(NULL, source_size, PROT_READ, MAP_PRIVATE, source_fd, 0); if (source_map==MAP_FAILED) { perror(Error mapping source file); close(source_fd); close(destination_fd); return 1; }
char *destination_map=mmap(NULL, source_size, PROT_READ | PROT_WRITE, MAP_SHARED, destination_fd, 0); if (destination_map==MAP_FAILED) { perror(Error mapping destination file); munmap(source_map, source_size); close(source_fd); close(destination_fd); return 1; }
memcpy(destination_map, source_map, source_size);
munmap(source_map, source_size); munmap(destination_map, source_size);
close(source_fd); close(destination_fd);
return 0;}
利用mmap实现的一个文件拷贝例子,供大家参考,具体内容如下
/* * gcc -Wall -O3 -o copy_mmap copy_mmap.c */ #include < stdio.h > #include < stdlib.h > #include < string .h > /* for memcpy */ #include < strings.h > #include < sys / mman.h > #include < sys / types.h > #include < sys / stat.h > #include < fcntl.h > #include < unistd.h > #define PERMS 0600 int main ( int argc, char * argv[] ) { int src, dst; void * sm, * dm; struct stat statbuf; if ( argc != 3 ) { fprintf( stderr, " Usage: %s \n " , argv[ 0 ] ); exit( EXIT_FAILURE ); } if ( ( src = open( argv[ 1 ], O_RDONLY ) ) < 0 ) { perror( " open source " ); exit( EXIT_FAILURE ); } /* 为了完成复制,必须包含读打开,否则mmap()失败 */ if ( ( dst = open( argv[ 2 ], O_RDWR | O_CREAT | O_TRUNC, PERMS ) ) < 0 ) { perror( " open target " ); exit( EXIT_FAILURE ); } if ( fstat( src, & statbuf ) < 0 ) { perror( " fstat source " ); exit( EXIT_FAILURE ); } /* * 参看前面man手册中的说明,mmap()不能用于扩展文件长度。所以这里必须事 * 先扩大目标文件长度,准备一个空架子等待复制。 */ if ( lseek( dst, statbuf.st_size - 1 , SEEK_SET ) < 0 ) { perror( " lseek target " ); exit( EXIT_FAILURE ); } if ( write( dst, & statbuf, 1 ) != 1 ) { perror( " write target " ); exit( EXIT_FAILURE ); } /* 读的时候指定 MAP_PRIVATE 即可 */ sm = mmap( 0 , ( size_t )statbuf.st_size, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, src, 0 ); if ( MAP_FAILED == sm ) { perror( " mmap source " ); exit( EXIT_FAILURE ); } /* 这里必须指定 MAP_SHARED 才可能真正改变静态文件 */ dm = mmap( 0 , ( size_t )statbuf.st_size, PROT_WRITE, MAP_SHARED, dst, 0 ); if ( MAP_FAILED == dm ) { perror( " mmap target " ); exit( EXIT_FAILURE ); } memcpy( dm, sm, ( size_t )statbuf.st_size ); /* * 可以不要这行代码 * * msync( dm, ( size_t )statbuf.st_size, MS_SYNC ); */ return ( EXIT_SUCCESS ); }
mmap()好处是处理大文件时速度明显快于标准文件I/O,无论读写,都少了一次用户空间与内核空间之间的复制过程。操作内存还便于设计、优化算法。
文件I/O操作/proc/self/mem不存在页边界对齐的问题,但至少Linux的mmap()的最后一个形参offset并未强制要求页边界对齐,如果提供的值未对齐,系统自动向上舍入到页边界上。malloc()分配得到的地址不见得对齐在页边界上。
/proc/self/mem和/dev/kmem不同。root用户打开/dev/kmem就可以在用户空间访问到内核空间的数据,包括偏移0处的数据,系统提供了这样的支持。显然代码段经过/proc/self/mem可写映射后已经可写,无须mprotect()介入。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

