简体   繁体   English

munmap,mmap的功能是什么

[英]What is the functionality of munmap, mmap

When I try to study some piece of code that deals with FPGA, I came across with munmap, mmap. 当我尝试研究一些与FPGA有关的代码时,遇到了munmap,mmap。

I go through the manual provided here . 我仔细阅读这里提供的手册。 I am still not understanding the purpose of this function. 我仍然不了解此功能的目的。 What exactly this does? 这到底是做什么的?

它将磁盘缓存的大块映射到进程空间,以便可以在字节级别上操作映射的文件,而不需要使用read()write()等方法使应用程序通过VFS。

mmap() is a system call, which helps in memory-mapped I/O operations. mmap()是系统调用,可帮助进行内存映射的I / O操作。 It allocates a memory region and maps that into the calling process virtual address space so as to enable the application to access the memory. 它分配一个内存区域并将其映射调用进程虚拟地址空间,以使应用程序能够访问内存。

mmap() returns a pointer to the mapped area which can be used to access the memory. mmap()返回指向映射区域的指针,该指针可用于访问内存。

Similarly, munmap() removes the mapping so no further access to the allocated memory remains legal. 同样, munmap()删除映射,因此对分配的内存的进一步访问不再合法。

These are lower level calls, behaviourally similar to what is offered by memory allocator functions like malloc() / free() on a higher level. 这些是较低级别的调用,其行为与较高级别上的内存分配器函数(如malloc() / free()所提供的类似。 However, this system call allow one to have fine grained control over the allocated region behaviour, like, 但是,此系统调用允许对分配的区域行为进行精细控制,例如,

  • memory protection of the mapping (read, write, execute permission) 映射的内存保护(读取,写入,执行权限)
  • ( approximate ) location of the mapping (see MAP_FIXED flag) 近似 )映射位置(请参见MAP_FIXED标志)
  • the initial content of the mapped area (see MAP_UNINITIALIZED flag) 映射区域的初始内容(请参见MAP_UNINITIALIZED标志)

etc. 等等

You can also refer to the wikipedia article if you think alternate wordings can help you. 如果您认为其他措词可以帮助您,也可以参考Wikipedia文章

The manual is clear: 手册很清楚:

mmap() creates a new mapping in the virtual address space of the calling process mmap()在调用进程的虚拟地址空间中创建一个新的映射

In short, it maps a chunk of file/device memory/whatever into the process' space, so that it can directly access the content by just accessing the memory. 简而言之,它将大块文件/设备内存/任何内容映射到进程的空间,以便仅通过访问内存就可以直接访问内容。

For example: 例如:

fd = open("xxx", O_RDONLY);
mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);

Will map the file's content to mem , reading mem is just like reading the content of the file xxx . 将文件的内容映射到mem ,读取mem就像读取文件xxx的内容一样。

If the fd is some FPGA's device memory, then the mem becomes the content of the FPGA's content. 如果fd是某些FPGA的设备存储器,则mem成为FPGA内容的内容。

It is very convenient to use and efficient in some cases. 在某些情况下使用非常方便且高效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM