Inter-Process Communication Linux # # # shared memory (shared memory)

Shared Memory (Inter-Process Communication fastest): General operation data from data copied to the user mode kernel mode, when used, and then copied to the user mode kernel mode, but does not require two steps shared memory, virtual address space the operation of the physical memory is operated, then another virtual address space may also have this data, ie without copying. Because the shared memory directly apply a physical memory by mapping the page table to the virtual address space, virtual address space operation, the operation is in fact the same physical memory area, and therefore other means of communication when compared to data transmission, a two-step fewer user mode the kernel state data copy process, thus the memory is shared among the fastest-process communication.

Since a plurality of processes share the same memory region, inevitably require some user mode synchronization mechanism, mutex (inter-process may have a mutex) semaphore, condition variables, read-write locks, and so can lock the file. To simplify the data integrity of shared data and to avoid simultaneous access, the kernel provides a mechanism for special access to the shared memory resources. This is called a mutex or mutex object.

 

Memory model

On Linux systems, virtual memory per process is divided into a number of pages. These memory pages contains the actual data. Each process will maintain a mapping relationship between the virtual memory page from memory address to. Although each process has its own memory address, different processes can simultaneously map the same memory page to their own address space, so as to achieve the purpose of shared memory. Use a shared memory requires the following four steps:

  1. Process must first assign a new shared memory block creates a new memory page. Because all processes want to share access to the same memory, and should only be a process to create a new shared memory. Allocates a block of memory already exists does not create a new page again, but only returns a identifies the memory block identifier.

  2. Then you need to access the shared memory block of each process must bind the shared memory to its own address space, create a mapping from the process itself to the shared virtual address of the page.

  3. After the end of the process used for shared memory, the mapping will be deleted.

  4. This process no longer requires the use of shared memory blocks, you must have one (and only one) this process is responsible for the release of shared memory pages that is the release of the shared memory block

 

Shared memory (shared memory) can be divided into standard System V and POSIX standard, detailed reference

Room # Linux # # System V IPC Standard & POSIX standard

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103731059