The method of establishing and releasing memory mapping in C language programming _c language

Source: Internet
Author: User
Tags gopher

C language Mmap () function: Creating Memory mappings
header file:

#include <unistd.h>  #include <sys/mman.h>

Definition functions: void *mmap (void *start, size_t length, int prot, int flags, int fd, off_t offsize);

Function Description: Mmap () is used to map a file content into memory, access to the memory area is directly read and write to the contents of the file.

Parameter description:

Return value: Returns the memory start address of the mapping area if the mapping succeeds, otherwise returns map_failed (-1), and the reason for the error is stored in the errno.

Error code:

    • EBADF parameter FD is not a valid file descriptor.
    • Eacces access permissions are incorrect. If the file must be readable if it is map_private, use map_shared to have prot_write and the file to write to.
    • Einval parameter start, length, or offset has an illegal.
    • The Eagain file is locked or there is too much memory locked.
    • Enomem not enough memory.

Example: Use Mmap () to read the contents of/etc/passwd files.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include < unistd.h>
#include <sys/mman.h>
main () {
  int fd;
  void *start;
  struct STAT sb;
  FD = open ("/etc/passwd", o_rdonly); /* Open/etc/passwd
  /Fstat (FD, &SB);/* Get file Size/
  start = Mmap (NULL, Sb.st_size, Prot_read, Map_private, FD, 0 );
  if (start = = map_failed)/* To determine whether mapping success/return
    ;
  printf ("%s", start); Munma (start, sb.st_size); /* Unlock mapping *
  /closed (FD);
}

Execution results:

ROOT:X: 0:root:/root:/bin/bash
bin:x: 1:1: Bin:/bin: daemon:x: 2:2
:d Aemon:/sbin
ADM : X:3:4:adm:/var/adm:
lp:x: 4:7: LP:/VAR/SPOOL/LPD: sync:x:
5:0: Sync:/sbin:bin/sync: 
   SHUTDOWN:X: 6:0: Shutdown:/sbin:/sbin/shutdown halt:x: 7:0: Halt:/sbin:/sbin/halt mail:x
: 8:12:mail:/var/spool/mail: news:x: 9:13:news:/var/spool/news: uucp:x: 10:14:UUCP:/var/spo
OL/UUCP: operator:x: 11:0: operator:/root: games:x: 12:100:games:/usr/games:
gopher:x:
13 : 30:gopher:/usr/lib/gopher-data:
ftp:x: 14:50:ftp User:/home/ftp: nobody:x:
99:99:nobody:/:
xfs:x: 100:101:x Font Server:/etc/xll/fs:/bin/false gdm:x: 42:42:
:/HOME/GDM:/bin/bash
Kids : x:500:500:/home/kids:/bin/bash

C language Munmap () function: Unlock memory mappings
header file:

#include <unistd.h>    #include <sys/mman.h>

To define a function:

int Munmap (void *start, size_t length);

Function Description: The Munmap () is used to cancel the mapped memory start address that the parameter start refers to, and the parameter length is the memory size to be canceled. When the process ends or the exec-related function is used to execute other programs, the mapped memory is automatically lifted, but no mappings are removed when the corresponding file descriptor is closed.

Return value: Returns 0 if the mapping succeeds, or 1. The error reason is stored in errno error code einval parameter start or length is not valid.

Example: Reference mmap ()

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.