What is the functionality of munmap, mmap

旧巷老猫 提交于 2019-12-11 07:04:44

问题


When I try to study some piece of code that deals with FPGA, I came across with munmap, mmap.

I go through the manual provided here. I am still not understanding the purpose of this function. What exactly this does?


回答1:


The manual is clear:

mmap() creates a new mapping in the virtual address space of the calling process

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.

If the fd is some FPGA's device memory, then the mem becomes the content of the FPGA's content.

It is very convenient to use and efficient in some cases.




回答2:


It maps a chunk of disk cache into process space so that the mapped file can be manipulated at a byte level instead of requiring the application to go through the VFS with read(), write(), et alia.




回答3:


mmap() is a system call, which helps in memory-mapped I/O operations. 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.

Similarly, munmap() removes the mapping so no further access to the allocated memory remains legal.

These are lower level calls, behaviourally similar to what is offered by memory allocator functions like malloc() / free() on a higher level. 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)
  • the initial content of the mapped area (see MAP_UNINITIALIZED flag)

etc.

You can also refer to the wikipedia article if you think alternate wordings can help you.



来源:https://stackoverflow.com/questions/41480184/what-is-the-functionality-of-munmap-mmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!