[英]What is the functionality of munmap, mmap
它将磁盘缓存的大块映射到进程空间,以便可以在字节级别上操作映射的文件,而不需要使用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, 但是,此系统调用允许对分配的区域行为进行精细控制,例如,
MAP_FIXED
flag) MAP_FIXED
标志) 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.