Linux interprocess communication--memory sharing mapping mmap and Munmap

Source: Internet
Author: User
Tags message queue

IPC Three communication mechanism refers to: semaphore, shared memory, message queue, signal Volume: Through the operating system of the PV operation to achieve;Shared Memory: Request a piece of memory, process A to write in shared memory, other processes can read out the contents of the shared memory to obtain the information transmitted by process A;Message Queuing: Create a message queue, process a writes to the queue, and process B gets the information that process a transmits by reading the volume in the queue.

  Mmap can map a portion of a disk file directly to memory, so that the location of the file directly has a corresponding memory address, read and write to the file can be directly with the pointer to do without the need for read/write function.

#include <sys/mman.h>void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);int munmap(void *addr, size_t length);

  

If the addr parameter is null, the kernel will itself select the appropriate address in the process address space to establish the mapping. If addr is not NULL, give the kernel a hint as to what address it should start mapping from, and the kernel will choose an appropriate address above addr to start mapping. Once the mapping is established, the true map header address can be obtained by the return value . The len parameter is the length of the portion of the file that needs to be mapped. The off parameter is the mapping from where the file begins, and must be an integer multiple of the page size (typically 4K on a 32-bit system structure). Filedes is a descriptor that represents the file.

The prot parameter has four values:
1) prot_exec represents this segment of the map executable, such as mapping shared libraries
2) Prot_read indicates that this section of the map is readable
3) Prot_write indicates that this section of the map can be written
4) Prot_none indicates that this section of the map is inaccessible

The flag parameter has a number of values, here only two common, other values can be viewed mmap (2)

    • Map_shared the mapping of multiple processes to the same file is shared, one process modifies the mapped memory, and another process sees this change.
    • Map_private the mapping of multiple processes to the same file is not shared, one process modifies the mapped memory, and the other process does not see this change, nor does it actually write to the file.

* Returns the first address of the map if Mmap succeeds, and returns a constant map_failed if an error occurs. When the process terminates, the mapped memory of the process is automatically dismissed, or munmap can be called . Munmap successfully returned 0, error returned-1.

/**/[email protected]:~$ VI hello[email protected]:~$ cat Hellohelloworld[email protected]:~$ od-tx1-tc Hello000000077    0a        h  e  l  l  o  w  o  r  L  D  \ n 0000013

#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/stat.h>#include<sys/types.h>#include<fcntl.h>#include<sys/mman.h>intMainvoid){    intFD, *p, Len; FD= Open ("Hello", O_RDWR); if(FD <0) {perror ("Open"); Exit (1); }    //Get file lengthLen = Lseek (FD,0, Seek_end); //Establish Mappingsp = mmap (NULL, Len, prot_read| Prot_write, map_shared, FD,0); if(p = =map_failed) {//If it fails, remember this detection methodPerror ("mmap"); Exit (1); }    //mapping is not released even if the file is closedClose (FD); //because it is a shared mapping, the mapping source is also modifiedp[0] =0x30313233;    Munmap (P, Len); return 0;}/*post-run results (reason for small-end storage)*/[email protected]:~$ cat Hello3210oworld[email protected]:~$ OD-TX1-TC Hello0000000  -  +  to  -6f the6f the6c -0a3  2  1  0o w o r l d \ n0000013
123, when the reported bus error, priority to see if the shared files have storage space

Linux interprocess communication--memory sharing mapping mmap and Munmap

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.