分享

uc中怎么建立映射mmap,解除映射munmap,从英文到汉语过程

 GLL_ 2018-04-05
首先需要包含头文件 <sys/mman.h>

查询帮助手册  man mmap
NAME
       mmap, munmap - map or unmap files or devices into memory
解释:mmap和munmap是文件或者设备映射到内存(建立映射),解除内存
SYNOPSIS
       #include <sys/mman.h>
       void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);
       int munmap(void *addr, size_t length);

void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);

功能:建立设备与内存的映射
参数:
1、addr:
​英文:If addr is NULL, then the kernel chooses the address at which to create
       the mapping; this is the most portable method of creating  a  new  map‐
       ping.   If  addr  is not NULL, then the kernel takes it as a hint about
       where to place the mapping; on Linux, the mapping will be created at  a
       nearby  page  boundary.   The address of the new mapping is returned as
       the result of the call.
​解释:
如果addr是NULL,那么内核就会选择创建映射的地址;这是创建新地图ping的最便携的方法。如果addr不是空的,那么内核将

​它作为一个提示,告诉它在哪里放置映射;
在Linux上,映射将在附近的页面边界上创建。新的映射的地址作为呼叫的结果返回。
2、ength:指的映射到虚拟内存的长度
英文:The length argument specifies the length of the mapping.
解释:就是指的映射到虚拟内存的长度
3、prot:
英文:The prot argument describes the desired memory protection of  the  map‐
       ping  (and  must  not  conflict with the open mode of the file).It is
       either PROT_NONE or the bitwise OR of one  or  more  of  the  following
       flags:
       PROT_EXEC  Pages may be executed.
       PROT_READ  Pages may be read.
       PROT_WRITE Pages may be written.
       PROT_NONE  Pages may not be accessed.
解释:prot描述的mapping所需的内存保护(并且不能与文件的打开模式相冲突)。它要么是原生的,要么是位的,要么是下面的一个或多个
       PROT_EXEC 内存页面可能会执行 
       PROT_READ 内存页可能会读取  //这里一会要用到
       PROT_WRITE 内存页可能会写   //这里一会要用到
       PROT_NONE 内存页可能不会被访问
4、flags:
英文:  The  flags argument determines whether updates to the mapping are visible
        to other processes mapping the same region, and whether updates are
       carried through to the underlying file.  This behavior is determined by
       including exactly one of the following values in flags:
       MAP_SHARED Share this mapping.  Updates to the mapping are  visible  to
                  other  processes that map this file, and are carried through
                  to the underlying  file.   The  file  may  not  actually  be
                  updated until msync(2) or munmap() is called.
       MAP_PRIVATE
                  Create a private copy-on-write mapping.  Updates to the map‐
                  ping are not visible to other  processes  mapping  the  same
                  file,  and  are  not carried through to the underlying file.
                  It is unspecified whether changes made to the file after the
                  mmap() call are visible in the mapped region.
 In addition, zero or more of the following values can be ORed in flags:
       MAP_ANONYMOUS  //后面还有好些个,这里举了一个,一会要用
              The mapping is not backed by any file; its contents are initial‐
              ized to zero.  The fd and offset arguments are ignored; however,
              some implementations require fd to be -1  if  MAP_ANONYMOUS  (or
              MAP_ANON)  is specified, and portable applications should ensure
              this.  The use of MAP_ANONYMOUS in conjunction  with  MAP_SHARED
              is only supported on Linux since kernel 2.4.

解释:flags参数决定是否对映射的更新对映射同一区域的其他进程可见,以及是否将更新传递到底层文件。这种行为是通过在flags中包含下列值之一来确定的
            MAP_SHARED      分享这个映射。映射的更新对映射这个文件的其他进程是可见的,并被传递到底层文件。在msync(2)或munmap()被调用之前,该文件可能不会被更新。
           MAP_PRIVATE      创建一个私有的拷贝-写映射。对mapping的更新对映射同一文件的其他进程是不可见的,并且不会被传递到底层文件。在映射区域中可见mmap()调用之后对文件的更改是否可见,这是不确定的。

          此外,在flags中可以使用下列值的零个或多个值:
      MAP_ANONYMOUS

          映射不受任何文件的支持;它的内容被初始化为0。fd和偏移量的参数被忽略;然而,如果指定mapanonymous(或mapanon),有些实现要求fd为-1,而便携式应用程序应该确保这一点。自内核2.4以来,在Linux上只支持使用mapanonymous与mapshared一起使用。
5、fd  文件描述符 0

英文:A file descriptor refers to a non-regular file.  Or  MAP_PRIVATE
              was  requested,  but  fd is not open for reading.  Or MAP_SHARED
              was requested and PROT_WRITE is set,  but  fd  is  not  open  in
              read/write (O_RDWR) mode.  Or PROT_WRITE is set, but the file is
              append-only.

解释:文件描述符是指一个非规则文件。或者mapprivate是被要求的,但是fd是不允许阅读

的。
或者mapshared被请求,并设置了protwrite,但是fd在读/写(ordwr)模式下是不打开的。

或者protwrite被设置,但是这个文件是附加的。


 6、  offset :      文件的偏移位置
英文:The contents of a file mapping (as opposed to an anonymous mapping; see
       MAP_ANONYMOUS  below),  are  initialized using length bytes starting at
       offset offset in the file (or other object) referred  to  by  the  file
       descriptor  fd.  offset must be a multiple of the page size as returned
       by sysconf(_SC_PAGE_SIZE).
解释:文件映射的内容(与匿名映射相反;请参阅下面的地图匿名者),使用长度字节初始化,从文件描述符fd所引用的文件(或其他对象)的偏移偏移量开始。偏移量必须是sysconf(scpagesize)返回的页面大小的倍数。


返回值:成功返回0 失败返回-1,失败errno被设置
英文:On success, mmap() returns a pointer to the mapped area.  On error, the
       value MAP_FAILED (that is, (void *) -1) is returned, and errno  is  set
       appropriately.   On  success,  munmap()  returns  0, on failure -1, and
       errno is set (probably to EINVAL).
解释: 在成功上,mmap()返回一个指向映射区域的指针。在错误的情况下,返回值映射失败(即(void)-1),errno被适当设置。
这是mmap函数的,下面是munmap解除映射函数,这个就比较简单了
            int munmap(void *addr, size_t length);
功能:解除映射

英文: The munmap() system call deletes the mappings for the specified address
       range,  and  causes further references to addresses within the range to
       generate invalid memory references.  The region is  also  automatically
       unmapped  when  the  process is terminated.  On the other hand, closing
       the file descriptor does not unmap the region.

解释:munmap()系统调用删除指定addressrange的映射,并进一步引用范围内的

​地址来生成无效的内存引用。
当流程终止时,该区域也会自动取消映射。另一方面,

​关闭文件描述符并没有对该区域进行映射。



参数:

addr:就是之前创建映射的那个指针addr

length :创建映射时的空间,与前面对应


返回值:

​英文: On  success,  munmap()  returns  0, on failure -1, and
       errno is set (probably to EINVAL).

解释:在成功的情况下,munmap()返回0,在失败-1上,errno被设置(可能是EINVAL)。

​代码如下:
  1 #include<stdio.h>
  2 #include<sys/mman.h>
  3 #include<string.h>
  4 int main(void){
  5
  6     //建立映射
  7     void *p = mmap(NULL,128,PROT_READ|PROT_WRITE,\
  8             MAP_ANONYMOUS|MAP_PRIVATE,0,0);
  9     if(p == MAP_FAILED){// 映射失败
 10     //    printf("mmap failed...\n");
 11         perror("mman");
 12         return 1;
 13     }
 14     char *str = (char *)p;
 15     strcpy(str,"tarena");
 16     printf("%s\n",str);
 17
 18     munmap(p,128); // 解除映射
 19
 20
 21     return 0;
 22 }
~     
​运行结果后    

tarena@ubuntu:~/uc/day04$ gcc mman.c
tarena@ubuntu:~/uc/day04$ a.out
tarena
映射创建成功,解除成功


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多