interprocess communication (8)-Shared memory (POSIX)

Source: Internet
Author: User
Tags message queue posix

1. Preface All examples of this post are based on the RHEL6.5 platform (Linux kernal:2.6.32-431.el6.i686).
2. Introduction to Shared Memory
The various inter-process communication methods described earlier in Linux, such as pipe (pipeline), FIFO (named pipe), message queue, and their common denominator are communication through the kernel (assuming that POSIX Message Queuing is also implemented in the kernel). Because the POSIX standard does not specify how it is implemented). When writing data to the Pipe,fifo,message queue, the data needs to be copied from the user space (the user process) to the kernel, and when the data is read from these IPC, the data needs to be copied from the kernel to the user space. Therefore, all of these IPC methods require 2 data replication between the kernel and the user process, that is, inter-process communication must pass through the kernel, as shown in:

Inter-process communication through the kernel (IPC)

shared memory is also aIPC, it is currently the fastest IPC, and it is used in a way that maps the same memory area to the address space of the different processes that share it, so that communication between these processes is no longer required through the kernel, just a process operation on that shared memory area. and otherIPCThe difference is that the use of shared memory requires the user to synchronize their own operations. Is the shared memory areaIPCof communication:

3. Mapping function Mmap
each process has its own virtual address space, we know that in addition to the virtual memory in the heap can be flexibly allocated and released by the programmer, the rest of the virtual memory is controlled by the system, then there is no other way to allow programmers to control virtual memory? This is the MMAP function under Linux. The MMAP function can create a new virtual memory for our virtual space in the process, which can map a file to this new virtual memory, so the operation of the new virtual memory is the operation of this file. Therefore,mmapThe main function of a function is to map a file or device to the address space of the calling process when using themmapafter mapping files to a process,can directly manipulate this virtual address for file read and write operations,no longer need to callRead,Writeand other system calls.
UNIX Network Programming the second volume interprocess communication describes the Mmap function. There are three main uses for this function:
A) mapping a normal file into memory, usually in the need of frequent read and write files, so that memory read and write instead of I/O read and write, in order to achieve higher performance. Communication between unrelated processes can be provided;
b) A special file for anonymous memory mapping, can provide shared memory space for the affinity process;
c) provides shared memory space for unrelated processes, typically mapping an ordinary file into memory.

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:12PX;" > #include <sys/mman.h>  void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t offset);                 Successful return of the start address mapped to the process address space, failed to return map_failed</span>
parameter start:point to the memory start address that you want to map, which is usually set to NULL, which means that the system automatically selects the address, which is returned when the mapping succeeds.
parameter len:the number of bytes mapped to the process address space,it is from the beginning of the mapped fileOffsetbyte at the beginning of aOffsetis usually set to0.

parameter prot:How the map area is protected. Can be combined in several ways:

    ·  prot_read: data is readable;
    ·  Prot_write : Data can be written;
    ·  prot_exec : Data executable;
    ·  Prot_none : Data not accessible;

Flags : Sets the type flag for the memory map area, POSIX A flag defines the following three flags:
· Map_shared: This flag indicates that the modification made by the calling process to the data in the mapped memory area is visible to all processes that share the memory area and does alter its underlying support object (a file object or a shared memory area object).
· Map_private: The call process's modifications to the data in the mapped memory area are only visible to the process, without altering its underlying support object.
    · Map_fixed: This flag represents an accurate explanation of the start parameter, which is generally not recommended, and for portable code, the start parameter should be set to null and the MAP_FIXED flag is not specified.

The above three flags are defined in the POSIX.1-2001 standard, where map_shared and Map_private must select one. Some non-standard flags, such as map_anonymous (Map_anon), map_locked, etc., are also defined in Linux to refer to the Linux manual.

FD : A valid file descriptor. If map_anonymous Span style= "Color:rgb (51,51,51)" >map_anon linux FD parameter, and some system implementations such as bsd FD -1

Offset : The starting offset of the relative file.

To remove a mapping relationship from the address space of a process, you need to use the following function:

4. Map Delete Munmap
To remove a mapping relationship from the address space of a process, you need to use the following function:
#include <sys/mman.h>  int munmap (void *start, size_t len);   Successfully returned 0, error returned-1

Start : The starting address of the memory area that is mapped to the process address space, which is mmap the address returned.

Len : The size of the map area.

5. Mapping synchronization

For a map_shared memory-mapped area, the kernel's virtual memory algorithm keeps the memory-mapped file synchronized with the memory-mapped area, meaning that the kernel will update the memory-mapped file at a later point in time for modifications to the memory-mapped area of the memory-mapped file. If we want the contents of the file on the hard disk to be consistent with the content in the memory map area, then we can call Msync to perform this synchronization:

#include <sys/mman.h>  int msync (void *start, size_t len, int flags);  Successfully returned 0, error returned-1  

Start : The starting address of the memory area that is mapped to the process address space, which is mmap the address returned.

Len : The size of the map area.

Flags : Sync flag, with following Three symbols:

· Ms_async : Writes asynchronously, once the write operation is queued by the kernel, it returns immediately;

· Ms_sync : Synchronous write is not returned until the write operation is complete.

    . Ms_invalidate: Invalidates all other memory-mapped copies of the file.



interprocess communication (8)-Shared memory (POSIX)

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.