Linux mmap Mapping file

Source: Internet
Author: User

void *mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset);
function function: Map the contents of the file to a piece of memory (virtual memory), through the memory read and modify, to achieve the file read and modify.
Parameter description
Addr: Specifies the starting address of the map, usually set to null (indicated by the system).
Length: Represents how much part of the file is mapped to memory.
PORT: How the Map area is protected. There are several ways to combine:
Prot_exec Mapped areas can be executed


Prot_read Mapped areas can be read


Prot_writeMapped regions can be written


Prot_none Map area cannot be accessed (read/write)
Flags The attributes of the mapped area.
Map_fixed If the address that the parameter addr refers to cannot successfully establish the mapping, the mapping is discarded.
Map_shared Write data to the mapped region is copied back into the file, and other processes that map the file are allowed to be shared.
Map_private Writing to a mapped region results in a copy of the mapped file, which is a private copy on write, and any modifications made to this area will not be written back to the original file.
Map_anonymous Establish an anonymous mapping. The parameter FD is ignored, the file is not involved, and the mapped area cannot be shared with other processes.
Map_denywrite Only write operations to the mapped region are allowed, and other operations that write directly to the file will be rejected.
Map_lockedLocks the mapped area, which means that the zone is not displaced (swap).
FD: The file descriptor to map to memory. If anonymous memory mapping is used, MAP_ANONYMOUS,FD is set to-1 in flags. Some systems do not support anonymous memory mappings, so you can open the/dev/zero file by opening it and then map the file to the same effect as an anonymous memory map.
Offset: The offsets of the mapped files, usually set to 0, represent the corresponding starting point from the front of the file, and offset must be an integer multiple of the paging size.


Return value: If the mapping succeeds, it returns the memory start address of the mapping area, otherwise map_failed (-1) is returned, and the cause of the error is stored in errno.


int Munmap (void *addr, size_t length);
function function: Turn off memory mapping
Addr: The start address of the memory map that needs to be closed (the return value of the MMAP function)
Length: The duration of the mapping (that is, the specified mapping file length when the mmap function is called)


Return value: Successfully returned 0, failed to return-1, the cause of the error is stored in errno.


Example code:

#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <string.h> #include <errno.h> #define FILE_NAME "test _file "//exception check function, print error message void print_error (int f_line) {fprintf (stderr,"%s%s%d\n ", Strerror (errno), __file__,f_line);}    int main (int argc, char** argv) {int fd;    void *map_ptr;    struct stat m_stat;         Gets the status of the file if (stat (file_name,&m_stat) = =-1) {print_error (__line__);    return-1;        } if (fd = open (file_name,o_rdwr)) = =-1) {print_error (__line__);    return-1; }//maps the entire file, m_stat.st_size the size of the entire file if (Map_ptr = mmap (Null,m_stat.st_size,prot_read |        prot_write,map_shared,fd,0) = = (void *) map_failed) {print_error (__line__);    return-1;    }//Print the contents of the file printf ("%s", (char *) map_ptr);    Modify the contents of the map area, since the use of prot_write, after the end of the program, the content will be written to the original file memcpy (map_ptr, "Hello", 5); Disconnect Memory Map Munmap (map_ptr,m_stat.st_size);    Close (FD); return 0;}

Test results:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux mmap Mapping file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.