简体   繁体   中英

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.

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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