Article Title: Linux inter-process communication shared memory. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Shared Memory)
The shared memory area is a part of the physical memory shared by multiple processes. If multiple processes map the memory area to their virtual address space, these processes can directly access the shared memory area for communication. Shared memory is the fastest way to share data between processes. When a process writes data to the shared memory area, all the processes that share the memory area can immediately see the content. This page of shared virtual memory appears in the page table of each process that shares the page. However, it does not need to have the same virtual address in the virtual memory of all processes.
Figure shared memory ing
Like all System v ipc objects, access to shared memory objects is controlled by keys and access permission check is required. After the memory is shared, the process will no longer check how to use the memory. They must rely on other computer systems, such as the System V signal lights to synchronize access to the shared memory area.
Each newly created shared memory object is expressed in a shmid_kernel data structure. All shmid_kernel data structures in the system are stored in the shm_segs vector table. Each element of this vector table is a pointer to the shmid_kernel data structure. The shm_segs vector table is defined as follows:
Struct shmid_kernel * shm_segs [SHMMNI];
The SHMMNI value is 128, indicating that the system can have a maximum of 128 shared memory objects.
The data structure shmid_kernel is defined as follows:
Struct shmid_kernel
{
Struct shmid_ds u;/* the following are private */
Unsigned long shm_npages;/* size of segment (pages )*/
Unsigned long * shm_pages;/* array of ptrs to frames-> SHMMAX */
Struct vm_area_struct * attaches;/* descriptors for attaches */
};
Shm_pages is the page table of the shared memory object. Each shared memory object has one page table. It describes how to map the shared memory area to the address space of the process.
Shm_npages is the size of the shared memory area, in pages.
Shmid_ds is a data structure that describes the authentication information, the size of bytes, the last adhesion time, the separation time, the change time, and the process for creating the shared area, the last process to operate on it. The number of processes currently using it and other information. It is defined as follows:
Struct shmid_ds {
Struct ipc_perm shm_perm;/* operation perms */
Int shm_segsz;/* size of segment (bytes )*/
_ Kernel_time_t shm_atime;/* last attach time */
_ Kernel_time_t shm_dtime;/* last detach time */
_ Kernel_time_t shm_ctime;/* last change time */
_ Kernel_ipc_pid_t shm_cpid;/* pid of creator */
_ Kernel_ipc_pid_t shm_lpid;/* pid of last operator */
Unsigned short shm_nattch;/* no. of current attaches */
Unsigned short shm_unused;/* compatibility */
Void * shm_unused2;/* ditto-used by DIPC */
Void * shm_unused3;/* unused */
};
Attaches describes the virtual memory area of each process mapped to the shared physical memory object. Every process that wants to share this memory must attach it to its virtual memory through system calls. This process creates a new vm_area_struct data structure that describes the shared memory for the process. The process can select the location of the virtual address space that exists in the share, or allow Linux to select a free area for it.
The new vm_area_struct structure is not only connected to the memory structure mm of the process, but also to a linked list of the shmid_kernel shared memory data structure. The attaches pointer in this structure points to the linked list. The vm_area_struct data structure provides two pointers: vm_next_shared and vm_prev_shared, used to connect the vm_area_struct data structure of the shared area in the processes in which it is used. In fact, the virtual memory is not created when it is attached, but will not be created until the first process tries to access it.
Figure System v ipc Mechanism-shared memory
[1] [2] [3] Next page