Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1474476
  • 博文数量: 338
  • 博客积分: 2695
  • 博客等级: 少校
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-05 11:37
个人简介

小鱼儿游啊游啊。。。。

文章分类

全部博文(338)

文章存档

2019年(4)

2018年(8)

2017年(6)

2016年(10)

2015年(49)

2014年(48)

2013年(98)

2012年(115)

分类: Android平台

2013-11-11 14:05:45

说明:这个例子是采用共享文件映射形式,写端写入数据,读端读取数据。

写端: 

  1. /*-------------map_normalfile1.c-----------*/  
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6.   
  7. #include   
  8. #include   
  9.   
  10. typedef struct{  
  11.     char name[4];  
  12.     int  age;  
  13. }people;  
  14.   
  15. main(int argc, char** argv) // map a normal file as shared mem:  
  16. {  
  17.     int fd,i;  
  18.     people *p_map;  
  19.     char temp;  
  20.       
  21.     //open file  
  22.     fd=open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);  
  23.     printf("file opened\n");  
  24. /*  man open 
  25. NAME 
  26.      open, openat - open a file 
  27.  
  28. SYNOPSIS 
  29.      #include  
  30.      #include  
  31.      #include  
  32.  
  33.      int open(const char *path, int oflag,  mode_t mode ...); 
  34.  
  35. DESCRIPTION 
  36.      The open() function establishes  the  connection  between  a 
  37.      file and a file descriptor. It creates an open file descrip- 
  38.      tion that refers to a file and a file descriptor that refers 
  39.      to  that  open file description. The file descriptor is used 
  40.      by other I/O functions to refer  to  that  file.   The  path 
  41.      argument points to a pathname naming the file. 
  42.  
  43.      The file offset used to mark the current position within the 
  44.      file is set to the beginning of the file. 
  45.  
  46.      The file status flags and file access modes of the open file 
  47.      description  are  set  according  to the value of oflag. The 
  48.      mode argument is used only when O_CREAT  is  specified  (see 
  49.      below.) 
  50.  
  51.      Values for oflag are constructed by  a  bitwise-inclusive-OR 
  52.      of  flags  from  the  following  list, defined in . 
  53.      Applications must specify exactly one  of  the  first  three 
  54.      values (file access modes) below in the value of oflag: 
  55.  
  56.      O_RDONLY 
  57.            Open for reading only. 
  58.      O_WRONLY 
  59.            Open for writing only. 
  60.      O_RDWR 
  61.            Open for reading and writing. The result is  undefined 
  62.            if this flag is applied to a FIFO. 
  63.      Any combination of the following may be used: 
  64.      O_APPEND 
  65.            If set, the file offset is set to the end of the  file 
  66.            prior to each write. 
  67.      O_CREAT 
  68.            Create the file  if  it  does  not  exist.  This  flag 
  69.            requires that the mode argument be specified. 
  70.      ..... 
  71. */      
  72.     lseek(fd,sizeof(people)*5-1,SEEK_SET);  
  73.     write(fd,"",1);  
  74. /* 
  75. NAME 
  76.      lseek - move read/write file pointer 
  77.  
  78. SYNOPSIS 
  79.      #include  
  80.      #include  
  81.  
  82.      off_t lseek(int fildes, off_t offset, int whence); 
  83.  
  84. DESCRIPTION 
  85.      The lseek() function sets the file pointer  associated  with 
  86.      the open file descriptor specified by fildes as follows: 
  87.  
  88.         o  If whence is SEEK_SET, the pointer is  set  to  offset 
  89.            bytes. 
  90.  
  91.         o  If whence is SEEK_CUR,  the  pointer  is  set  to  its 
  92.            current location plus offset. 
  93.  
  94.         o  If whence is SEEK_END, the pointer is set to the  size 
  95.            of the file plus offset. 
  96.  
  97.      The symbolic constants SEEK_SET, SEEK_CUR, and SEEK_END  are 
  98.      defined in the header . 
  99. */      
  100.     //create mmap  
  101.     p_map = (people*) mmap(NULL, sizeof(people)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);  
  102.     close( fd );  
  103. /* 
  104. NAME 
  105.      mmap - map pages of memory 
  106.  
  107. SYNOPSIS 
  108.      #include  
  109.  
  110.      void *mmap(void *addr, size_t len, int prot, int flags,  int 
  111.      fildes, off_t off); 
  112.  
  113. DESCRIPTION 
  114.      The  mmap()  function  establishes  a  mapping   between   a 
  115.      process's  address space and a file or shared memory object. 
  116.      The format of the call is as follows: 
  117.  
  118.           pa = mmap(addr, len, prot, flags, fildes, off); 
  119.  
  120.      The  mmap()  function  establishes  a  mapping  between  the 
  121.      address  space of the process at an address pa for len bytes 
  122.      to the memory object  represented  by  the  file  descriptor 
  123.      fildes  at  offset  off  for len bytes. The value of pa is a 
  124.      function of the  addr argument and values of flags,  further 
  125.      described  below. A successful mmap() call returns pa as its 
  126.      result. The address range starting at pa and continuing  for 
  127.      len  bytes  will  be legitimate for the possible (not neces- 
  128.      sarily current) address space of the process. The  range  of 
  129.      bytes  starting  at off and continuing for len bytes will be 
  130.      legitimate  for  the  possible  (not  necessarily   current) 
  131.      offsets  in  the file or shared memory object represented by 
  132.      fildes. 
  133. */      
  134.     //write mmap  
  135.     temp = 'a';  
  136.     for(i=0; i<100; i++)  
  137.     {  
  138.         temp += 1;  
  139.         memcpy(( *(p_map+i)).name, &temp, 2);  
  140.         (*(p_map+i)).age = 20+i;  
  141.     }  
  142.     printf("initialize over\n");  
  143.       
  144.     //unmap the mmap  
  145.     sleep(10);  
  146.     munmap((char*)p_map, sizeof(people)*100);  
  147.     printf("umap ok\n");  
  148.       
  149. /* 
  150. NAME 
  151.      munmap - unmap pages of memory 
  152.  
  153. SYNOPSIS 
  154.      #include  
  155.  
  156.      int munmap(void *addr, size_t len); 
  157.  
  158. DESCRIPTION 
  159.      The munmap() function removes the mappings for pages in  the 
  160.      range  [addr,  addr  + len), rounding the len argument up to 
  161.      the  next  multiple  of  the  page  size  as   returned   by 
  162.      sysconf(3C).  If  addr is not the address of a mapping esta- 
  163.      blished by a prior call to mmap(2), the  behavior  is  unde- 
  164.      fined.  After  a  successful call to munmap() and before any 
  165.      subsequent mapping of the unmapped pages, further references 
  166.      to  these  pages  will result in the delivery of a SIGBUS or 
  167.      SIGSEGV signal to the process. 
  168. */  
  169. }  

读端:


 

  1. /*-------------map_normalfile2.c-----------*/  
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6.   
  7. #include   
  8. #include   
  9.   
  10. typedef struct{  
  11.     char name[4];  
  12.     int  age;  
  13. }people;  
  14.   
  15. main(int argc, char** argv)  // map a normal file as shared mem:  
  16. {  
  17.     int fd,i;  
  18.     people *p_map;  
  19.       
  20.     //open file  
  21.     fd=open( argv[1],O_CREAT|O_RDWR,00777 );  
  22.       
  23.     //create mmap and read  
  24.     p_map = (people*)mmap(NULL,sizeof(people)*100, PROT_READ|PROT_WRITE, MAP_SHARED,fd,0);  
  25.     for(i = 0;i<100;i++)  
  26.     {  
  27.         printf( "name: %s age %d;\n",(*(p_map+i)).name, (*(p_map+i)).age );  
  28.     }  
  29.       
  30.     //unmap  
  31.     munmap((char*)p_map,sizeof(people)*100);  
  32. }  

阅读(1636) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~