免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2347 | 回复: 1

共享物理内存会定时刷新? [复制链接]

论坛徽章:
0
发表于 2010-07-05 20:58 |显示全部楼层
本帖最后由 kris_fei 于 2010-07-05 21:02 编辑
  1. //writemem.c
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <string.h>

  8. typedef struct{
  9.          char name[4];
  10.          int age;
  11. }people;
  12. main(int argc, char** argv) // map a normal file as shared mem:
  13. {
  14.          int fd,i;
  15.          people *p_map;
  16.          char temp;
  17.          fd=open("./tmpmem",O_CREAT|O_RDWR|O_TRUNC,00777);
  18.          lseek(fd,sizeof(people)*5-1,SEEK_SET);
  19.          write(fd,"",1);
  20.          p_map = (people*) mmap( NULL,sizeof(people)*10,PROT_READ|
  21. PROT_WRITE,MAP_SHARED,fd,0 );
  22.          //close( fd );
  23.          temp = 'a';
  24.          for(i=0; i<10; i++)
  25.          {
  26.                   temp += 1;
  27.                   memcpy( ( *(p_map+i) ).name, &temp,2 );
  28.                   ( *(p_map+i) ).age = 20+i;
  29.          }
  30.          printf(" initialize over \n ");
  31.          while(1)
  32.          sleep(1);
  33.          munmap( p_map, sizeof(people)*10 );
  34.          printf( "umap ok \n" );
  35. }


  36. //readmem.c
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <fcntl.h>
  40. #include <unistd.h>
  41. #include <stdio.h>

  42. typedef struct{
  43.          char name[4];
  44.          int age;
  45. }people;
  46. main(int argc, char** argv)        // map a normal file as shared mem:
  47. {
  48.          int fd,i;
  49.          people *p_map;
  50.          fd=open( "./tmpmem",O_CREAT|O_RDWR,00777 );
  51.          p_map = (people*)mmap(NULL,sizeof(people)*10,PROT_READ|
  52. PROT_WRITE,MAP_SHARED,fd,0);
  53.         while(1)
  54.         {
  55.          for(i = 0;i<10;i++)
  56.          {
  57.          printf( "name: %s age %d;\n",(*(p_map+i)).name, (*(p_map+i)).age );
  58.          }
  59.         sleep(1);
  60.         }
  61.   munmap( p_map,sizeof(people)*10 );
  62. }

复制代码
我映射10个结构体大小的内存,但是文件大小只有5个结构大小.
理论上说不超过一页是不会出错的.一开始我能读到数据,不过过了几十秒之后就后面5个结构体数据内容就都没了,就只有前5个数据,难道是内核自动刷新掉文件大小以外的内容了?

书上理论:
  在 linux 中,内存的保护是以页为基本单位的,即使被映射文件只有一个字节大小,内核也会为映
射分配一个页面大小的内存。当被映射文件小于一个页面大小时,进程可以对从 mmap()返回地址开始的
一个页面大小进行访问,而不会出错;但是,如果对一个页面以外的地址空间进行访问,则导致错误发
生,后面将进一步描述。因此,可用于进程间通信的有效地址空间大小不会超过文件大小及一个页面大
小的和。

论坛徽章:
0
发表于 2010-07-06 00:31 |显示全部楼层
本帖最后由 kgn28 于 2010-07-06 00:34 编辑

回复 1# kris_fei

是你没有考虑线性区的回收(映射的页帧回收时可以被同步回文件的)
    线性区(vm_area_struct)譬如A-B,对应页x,那么write在映射了80bytes之后,过段时间,内核想要刷回映射页的内容,将这个页回收了。下次在你read:
1,线性区还是A-B的时候发现缺页,然后再把文件内容读出来放到影射区,这时候A-B线性区对应的页可能是y了,内容就只有前40bytes了;
2,线性区变为C-D,但是过段时间C-D对应的页y被回收了,再度发现缺页,这时候C-D的页帧变为z了,所以也还是只有前40byets的内容,因为文件的大小就是40bytes;
根据你的程序应该是第二种情况。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP