Introduction to Linux mmap functions

Source: Internet
Author: User

First, Introduction

Linux provides a memory-mapped function mmap, which maps the contents of a file to a piece of memory (accurate virtual memory), through the memory read and modify, to realize the file read and modify, 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);
    • Return value: Success returns the mapping area start address, and failure returns map_failed (-1).
    • Parameters:
      • Addr: Specifies the starting address of the map, usually set to NULL, as specified by the system.
      • Length: Maps How long a file is to memory.
      • Prot: How the map area is protected can be:
        • Prot_exec: The map area can be executed.
        • Prot_read: The map area can be read.
        • Prot_write: The map area can be written.
        • Prot_none: The mapping area cannot be accessed.
      • Flags: The attributes of the map area, which can be:
        • Map_shared: Write data to the mapped region is copied back to the file, and other processes that map the file are allowed to be shared.
        • Map_private: Writes to a mapped region produce a mapped copy (Copy-on-write), and changes made to this area are not written back to the original file.
        • There are several other flags that are not very common, see the Linux C function descriptions.
      • FD: The file descriptor returned by open, representing the file to be mapped.
      • Offset: At the beginning of the file, it must be an integer multiple of the paging size, typically 0, which represents the start of the mapping from the file header.

Here are the steps for memory mapping:

    • Opens the file with the open system call and returns the descriptor FD.
    • Use MMAP to establish a memory map and return the map first address pointer start.
    • Perform various operations on the map (file), display (printf), modify (sprintf).
    • Close the memory map with munmap (void *start, size_t lenght).
    • Close the file fd with the close system call.

Precautions:

When you modify a mapped file, you can only modify it at the original length, and you cannot increase the file length because the memory is already allocated.

Ii. Examples

example1.c

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<error.h>#include<fcntl.h>#include<sys/mman.h>#include<unistd.h>intMainintargcChar*argv[]) {    intFd,len; Char*ptr; if(argc<2) {printf ("Please enter a file\n"); return 0; }    if((Fd=open (argv[1],O_RDWR)) <0) {perror ("Open File Error"); return-1; } Len=lseek (FD,0, Seek_end); PTR=mmap (null,len,prot_read| PROT_WRITE,MAP_SHARED,FD,0);//Read and write consistent with the sign of the open function, or it will be an error    if(ptr==map_failed) {Perror ("mmap Error");        Close (FD); return-1; } close (FD);//close file is OKprintf"length is%d\n", strlen (PTR)); printf ("The %s content is:\n%s\n", argv[1],ptr); ptr[0]='C';//modify one of the contentsprintf"The %s content is:\n%s\n", argv[1],ptr); Munmap (Ptr,len);//write the changed file to memory    return 0;}

Compile

Gcc-g-o example1 example1.c

Run

Introduction to Linux mmap functions

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.