Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the mmap () function

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the mmap() function, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.

The mmap() function maps a file or other object out of memory. The file is mapped onto multiple pages, and if the file size is not the sum of all the page sizes, the unused space on the last page is cleared. The mmap() system call enables processes to share memory by mapping the same common file. After a normal file is mapped to the process address space, the process can access the file as if it were normal memory, without having to call read(),write(), etc.

Note: In fact, the mmap() system call was not designed entirely for shared memory. It itself provides access to ordinary files differently than normal, and processes can operate on ordinary files like reading and writing memory. POSIX or System V shared memory IPC is purely used to provide shared purpose, of course, mmap() to achieve shared memory is also one of its main applications.

The Mmap() function has three main purposes: mapping an ordinary file to memory, usually used when frequent read and write operations are required for files, so that memory reads and writes replace I/O reads and writes to obtain higher performance. Anonymous memory mapping of special files can provide shared memory space for associated processes. Shared memory space for unrelated processes is generally mapped to memory. Linux provides a memory mapping function mmap, which maps the contents of a file to a segment of memory (to be precise, virtual memory), and reads and modifies files by reading and modifying the segment of memory.

The mmap() function

#includeVoid*mmap(void *start, size_t length, int port, int flags, int fd,off_t offsize);

Meaning of parameter: Start: points to the initial address of memory to be mapped, usually set to NULL, which means that the system automatically selects the address, and returns the address after mapping is successful. Length: Represents how much of the file is mapped to memory. Port: how mapped areas are protected PROT_EXEC mapped areas can be executed PROT_READ mapped areas can be read PROT_WRITE mapped areas can be written to PROT_NONE mapped areas cannot be accessed Flags: various properties affecting mapped areas MAP_SHARED or MAP_PRIVATE MAP_FIXED must be specified when calling mmap() If the address specified by the parameter start cannot be successfully mapped, the mapping is abandoned and no correction is made to the address. MAP_SHARED writes to mapped areas are copied within the file and are allowed to be shared by processes of other mapped files. MAP_PRIVATE writes to the mapped area result in a copy of the mapped file, i.e., a private "copy-on-write" where any modifications to the area are not written back to the original file contents. MAP_ANONYMOUS establishes anonymous mapping. The fd parameter is ignored, no files are involved, and the mapped area cannot be shared with other processes. MAP_DENYWRITE allows only writes to mapped areas, other direct writes to files will be denied. MAP_LOCKED locks the mapped region, which means that the region will not be swapped. fd: File descriptor to map to memory. If anonymous memory mapping is used, i.e. MAP_ANONYMOUS is set in flags, fd is set to-1. Some systems do not support anonymous memory mapping, you can use fopen to open the/dev/zero file, and then map the file, you can also achieve the effect of anonymous memory mapping. offset: offset of file mapping, usually set to 0, which means corresponding from the front of the file, offset must be an integer multiple of the page size. Return value: If mapping is successful, return the memory start address of the mapping area, otherwise return MAP_FAILS (-1), the error reason is stored in errno.

The system call mmap() is used in two ways to share memory: (1) using the memory mapping provided by the ordinary file: applicable to any process. At this point, you need to open or create a file, and then call mmap(). The typical call code is as follows:

fd=open(name, flag, mode);if(fd

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report