What does mmap do




















No, because of how virtual memory works. Each process has it's own virtual address space, with it's own virtual mappings. It's worth noting that mmap doesn't just work on files, you can also do other things with it such as: Directly mapping device memory if you have sufficient privileges. This is actually used on many embedded systems to avoid the need to write kernel mode drivers for new hardware.

Map shared memory segments. Explicitly map huge pages. Allocate memory that you can then call madvise 2 on which in turn lets you do useful things like prevent data from being copied to a child process on fork 2 , or mark data for KSM Linux's memory deduplication feature.

Improve this answer. Austin Hemmelgarn Austin Hemmelgarn 8, 1 1 gold badge 17 17 silver badges 27 27 bronze badges. Thanks for such a detailed answer. Just a clarification on point 1. If I try to access the returned virtual memory address. Yes, it will use the virtual memory mapping table. An implementation may generate SIGBUS signals when a reference would cause an error in the mapped object, such as out-of-space condition. The mmap function shall add an extra reference to the file associated with the file descriptor fildes which is not removed by a subsequent close on that file descriptor.

This reference shall be removed when there are no more mappings to the file. If there is no such call and if the underlying file is modified as a result of a write reference, then these fields shall be marked for update at some time after the write reference.

There may be implementation-defined limits on the number of memory regions that can be mapped per process or per system. The implementation does not support the combination of accesses requested in the prot argument.

Use of mmap may reduce the amount of memory available to other memory allocation functions. Once a file is mapped, all a process has to do to access it is use the data at the address to which the file was mapped. So, using pseudo-code to illustrate the way in which an existing program might be changed to use mmap , the following:. After considering several other alternatives, it was decided to adopt the mmap definition found in SVR4 for mapping memory objects into process address spaces.

The SVR4 definition is minimal, in that it describes only what has been built, and what appears to be necessary for a general and portable mapping facility. Note that while mmap was first designed for mapping files, it is actually a general-purpose mapping facility. It can be used to map any appropriate object, such as memory, files, devices, and so on, into the address space of a process. When a mapping is established, it is possible that the implementation may need to map more than is requested into the address space of the process because of hardware requirements.

An application, however, cannot count on this behavior. Implementations that do not use a paged architecture may simply allocate a common memory region and return the address of it; such implementations probably do not allocate any more than is necessary. References past the end of the requested area are unspecified. What does mmap do? Ask Question. Asked 11 years, 2 months ago. Active 2 years, 6 months ago. Viewed 6k times. What does this line of code do? Improve this question.

Add a comment. Active Oldest Votes. About the author Bamdeb Ghosh Bamdeb Ghosh is having hands-on experience in Wireless networking domain. He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python.

Follow his site: wifisharks. The mmap function is used for mapping between a process address space and either files or devices. When a file is mapped to a process address space, the file can be accessed like an array in the program. This is one of the most efficient ways to access data in the file and provides a seamless coding interface that is natural for a data structure that can be assessed without he abstraction of reading and writing from files.



0コメント

  • 1000 / 1000