Introduction to Linux mmap function, linuxmmap Function

Source: Internet
Author: User

Introduction to Linux mmap function, linuxmmap Function

I. Introduction

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.

Ii. Example

Example1.c

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <error. h> # include <fcntl. h> # include <sys/mman. h> # include <unistd. h> int main (int argc, char * argv []) {int fd, 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/write is consistent with the mark of the open function; otherwise, if (ptr = MAP_FAILED) {perror ("mmap error"); close (fd ); return-1;} close (fd); // close the file and OK printf ("length is % d \ n", strlen (ptr )); printf ("the % s content is: \ n % s \ n", argv [1], ptr); ptr [0] = 'C '; // modify one of the content printf ("the % s content is: \ n % s \ n", argv [1], ptr); munmap (ptr, len ); // write the changed file to the memory return 0 ;}

Compile

gcc -g -o example1 example1.c

Run

Related Article

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.