Linux memory ing-MMAP Function

Source: Internet
Author: User

Linux provides the memory ing function MMAP, which maps the file content to a memory segment (specifically, virtual memory). By reading and modifying this memory segment, to read and modify files, Let's first look at the MMAP function declaration:

  • Header file:
    • <Unistd. h>
    • <Sys/Mman. h>
  • Prototype: void * MMAP (void * ADDR, size_t length, int Prot, int flags, int FD, off_t offsize );
  • Returned value: if the operation succeeds, the start address of the ing area is returned. If the operation fails, map_failed (-1) is returned ).
  • Parameters:
    • ADDR: Specifies the starting address of the ing, which is usually set to null and specified by the system.
    • Length: how long the file length is mapped to the memory.
    • Prot: Protection Method of the ing area, which can be:
      • Prot_exec: The ing area can be executed.
      • Prot_read: The ing area can be read.
      • Prot_write: The ing area can be written.
      • Prot_none: The ing area cannot be accessed.
    • Flags: the features of the ing area, which can be:
      • Map_shared: write data to the ing area will be copied back to the file, and other processes that map the file can share.
      • Map_private: write operations on the ing area will generate a copy-on-write operation. modifications made to this area will not be written back to the original file.
      • In addition, several other flags are not frequently used. For details, refer to the Linux C function description.
    • FD: file descriptor returned by open, representing the file to be mapped.
    • Offset: the offset starting from the file. It must be an integer multiple of the page size, usually 0, indicating the ing from the file header.

The following describes the steps for memory ing:

  • Open the file with an open system call and return the descriptor FD.
  • Use MMAP to create a memory ing and return the ing first address pointer start.
  • Perform Various operations on the ing (file), display (printf), and modify (sprintf ).
  • Use munmap (void * Start, size_t lenght) to disable memory ing.
  • Use the close system call to close the file FD.

Note:

When modifying a ing file, you can only modify the original length, but cannot increase the file length, because the memory has been allocated.


Memory ing, in short, is to map a memory area of the user space to the kernel space. After the ing is successful, the user's modifications to this memory area can be directly reflected in the kernel space, modifications made to this region by the kernel space also directly reflect the user space. Therefore, if the kernel space <----> user space requires a large amount of data transmission and other operations, the efficiency is very high.

# Include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <stdio. h> # include <sys/Mman. h> # include <string. h> # include <errno. h> # include <unistd. h>/* void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offset ); // This function maps a file or POSIX shared memory area object to the process that calls the process. 1. start: Generally, it can be null; 2. length: the size of the mapped bytes. 3. prot: Access to ing storage (prot_none: inaccessible; prot_read: readable; prot_write: writable; prot_exec: executable); 4. flags: map_fixed, map_shared (operations on memory also affect files), and map_private5. file description 6. the position to be offset (seek_set, seek_cur, seek_end) int munmap (void * Start, size_t length); // This function is used to cancel ing */# define filename1 ". /lhw1 "# define filename2 ". /lhw2 "# define open_flag o_rdwr | o_creat # define open_mode 00777 # define file_size 4096*4 static int my_mmap (int dst, int SRC) {int ret =-1; void * add_src = NULL; void * add_dst = NULL; struct stat Buf = {0}; // obtain detailed information about the opened file (mainly to obtain the size of the Read File) ret = fstat (SRC, & BUF); If (-1 = RET) {perror ("fstat failed:"); goto _ out ;} // map the source file's storage zone add_src = MMAP (null, Buf. st_size, prot_read, map_shared, SRC, seek_set); If (null = add_src) {perror ("mmap src failed:"); goto _ out ;} // lseek DST (manufacturing file holes, so that it has a certain size, no size error) ret = lseek (DST, Buf. st_size, seek_set); If (-1 = RET) {perror ("lseek DST faile:"); goto _ out;} // write dstret = write (DST, "W", 1); If (-1 = RET) {perror ("Write DST faile:"); goto _ out ;} // map the destination file's storage zone add_dst = MMAP (null, Buf. st_size, prot_write, map_shared, DST, seek_set); If (null = add_dst) {perror ("mmap src failed:"); goto _ out ;} // memcpy copies the content of the Source File Memory add_src to the target file add_dst, and shares memcpy (add_dst, add_src, Buf. st_size); // cancel the RET ing ret = munmap (add_src, Buf. st_size); If (-1 = RET) {perror ("munmap SRC faile:"); goto _ out;} ret = munmap (add_dst, Buf. st_size); If (-1 = RET) {perror ("munmap DST faile:"); goto _ out;} _ out: return ret;} int main (void) {int ret =-1; int fd1 =-1; int fd2 =-1; // open fd1fd1 = open (filename1, open_flag, open_mode ); if (-1 = (ret = fd1) {perror ("Open fd1 failed:"); goto _ out;} // write fd1ret = write (fd1, "howaylee", sizeof ("howaylee"); If (-1 = RET) {perror ("Write failed:"); goto _ out ;} // open fd2fd2 = open (filename2, open_flag, open_mode); If (-1 = (ret = fd2) {perror ("Open fd2 failed :"); goto _ out;} // mmapmy_mmap (fd2, fd1); _ out: return ret ;}

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.