免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1737 | 回复: 3
打印 上一主题 下一主题

mmap求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-07 09:53 |只看该作者 |倒序浏览
  1. /*mycp.c*/
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <sys/mman.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>

  10. int main(int argc,char *argv[])
  11. {
  12. int fdin,fdout;
  13. void* src;
  14. void* dst;
  15. struct stat statbuf;

  16. if(argc != 3)
  17. {
  18.     printf("please input two file!\n");
  19.     exit(1);
  20. }

  21. if((fdin=open(argv[1],O_RDONLY))<0) /*打开原文件*/
  22.     perror(argv[1]);

  23. if((fdout=open(argv[2],O_RDWR|O_CREAT|O_TRUNC))<0)/*创建并打开目标文件*/
  24.     perror(argv[2]);

  25. if(fstat(fdin,&statbuf)<0) /*获得文件大小信息*/
  26.     printf("fstat error");

  27. [color=Red]if(lseek(fdout,statbuf.st_size-1,SEEK_SET)==-1)
  28.     printf("lseek error");

  29. if(write(fdout,"1",1)!=1)
  30.     printf("write error");[/color]

  31. if((src=mmap(0,statbuf.st_size,PROT_READ,MAP_SHARED,fdin,0))==MAP_FAILED)
  32.     printf("mmap error");

  33. if((dst=mmap(0,statbuf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fdout,0)) ==MAP_FAILED)
  34.     printf("mmap error");

  35. memcpy(dst,src,statbuf.st_size);/*复制映射存储区*/
  36. munmap(src,statbuf.st_size); /*解除输入映射*/
  37. munmap(dst,statbuf.st_size); /*解除输出映射*/

  38. close(fdin);
  39. close(fdout);

  40. return 0;
  41. }
复制代码

不解的是上面红色的有什么作用,为什么不加write那句话就会产生SIGBUS,为什么要设置fdout的偏移量呢??

论坛徽章:
0
2 [报告]
发表于 2009-04-07 10:11 |只看该作者
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <sys/mman.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>

  10. int main(int argc,char *argv[])
  11. {
  12.     int fdin,fdout;
  13.     void *src,*dst;
  14.     size_t pagesize;

  15.     struct stat statbuf;

  16.     if(argc != 3)
  17.     {
  18.         printf("usage : %s <fromfile> <tofile>\n",argv[0]);
  19.         return 1;
  20.     }
  21.     pagesize = sysconf(_SC_PAGESIZE);
  22.     printf("PageSize: %ld\n", (long)pagesize);
  23.     if((fdin = open(argv[1],O_RDONLY)) == -1)
  24.     {
  25.         printf("can't open file %s\n",argv[1]);
  26.         return 1;
  27.     }
  28.     if((fdout = open(argv[2],O_RDWR | O_CREAT | O_TRUNC,0644)) == -1)
  29.     {
  30.         printf("can't create file %s\n",argv[2]);
  31.         return 1;
  32.     }
  33.     assert(fstat(fdin,&statbuf) == 0);
  34.     printf("filein size = %ld\n",statbuf.st_size);
  35.     assert(ftruncate(fdout, statbuf.st_size) == 0
  36.         && fstat(fdout, &statbuf) == 0);
  37.     printf("fileout size = %ld\n",statbuf.st_size);
  38.     src = mmap(0,statbuf.st_size,PROT_READ,MAP_SHARED,fdin,0);
  39.     if( src == MAP_FAILED )
  40.     {
  41.         printf("mmap error for fdin.\n");
  42.         return 1;
  43.     }
  44.     dst = mmap(0,statbuf.st_size,PROT_READ | PROT_WRITE,MAP_SHARED,fdout,0);
  45.     if( dst == MAP_FAILED )
  46.     {
  47.         printf("mmap error for fdout.\n");
  48.         return 1;
  49.     }
  50.     memcpy(dst,src,statbuf.st_size);
  51.     close(fdin);
  52.     close(fdout);

  53.     return 0;
  54. }
复制代码

貌似有点懂了,
lseek了一个空洞 如果没write,也就是说如果lseek之后没有发生i/o操作时,lseek只是把偏移量给改了一下
并没有在磁盘上分配存储区
而你的文件是新建的 存储的空间长度为0 lseek之后还是为0
把mmap到系统的线性地址空间,然后进行复制的话就会出错。

论坛徽章:
0
3 [报告]
发表于 2009-04-07 10:13 |只看该作者
告诉我哪里是红色的?

我什么时候成了色盲了。。

论坛徽章:
0
4 [报告]
发表于 2009-04-07 12:26 |只看该作者
似乎没看懂啥意思
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP