Linux Environment interprocess Communication (V): Shared memory (bottom)
In the shared memory (above), mainly around the system call Mmap () for discussion, this section will discuss the System V shared memory, and through the comparison of experimental results to illustrate the similarities and differences. System V shared memory refers to the sharing of all shared data in the shared memory region (IPC), and any process that wants to access that data must have a new memory area in the address space of the process to map the physical memory pages that hold the shared data.
System call MMAP () implements shared memory by mapping an ordinary file. System V enables shared memory communication between processes by mapping files in the special file system SHM. That is, each shared memory area corresponds to a file in the special file system SHM (which is linked by the shmid_kernel structure), which is also explained later.
1, System V shared memory principle
The data that needs to be shared between processes is placed in a region called IPC shared memory, and all processes that need to access the shared zone map the shared area to the address space of the process. System V shared memory obtains or creates an IPC shared memory region through Shmget, and returns the appropriate identifier. The kernel guarantees that the shmget obtains or creates a shared memory area, initializes the corresponding Shmid_kernel structure of the shared memory area, and also creates and opens a file of the same name in the special file system SHM, and establishes the corresponding dentry and inode structure of the file in memory. The newly opened file does not belong to any one process (any process can access the shared memory area). All of this is done by the system call Shmget.
Note: Each shared memory area has a control structure struct Shmid_kernel,shmid_kernel is a very important data structure in the shared memory area, which is a bridge between storage management and file system, which is defined as follows:
struct Shmid_kernel/* Private to the kernel */{struct kern_ipc_permshm_perm;struct file *shm_file;intid;unsigned longshm _nattch;unsigned Longshm_segsz;time_tshm_atim;time_tshm_dtim;time_tshm_ctim;pid_tshm_cprid;pid_tshm_lprid;};
The most important domain in the structure should be shm_file, which stores the address of the file that will be mapped. Each shared memory area object corresponds to a file in the special file system SHM, in general, the file in the special file system SHM cannot be accessed using the read (), write () method, and when the shared memory is taken to map the file to the process address space, Access to the memory can be accessed directly using it.
Here we use the chart in [1] to give a data structure related to System V shared memory:
Like Message Queuing and beacons, the kernel maintains all shared memory areas in the system through the data structure struct Ipc_ids shm_ids. The shm_ids.entries variable in the pointer points to an array of ipc_id structures, and each IPC_ID structure array has a pointer to the KERN_IPC_PERM structure. Readers should be familiar with this, for the System V shared memory area, the host of the kern_ipc_perm is the shmid_kernel structure, Shmid_kernel is used to describe a shared memory area, so that the kernel can control all the shared areas of the system. At the same time, the file type pointer shm_file in the SHMID_KERNEL structure points to the corresponding files in the file system SHM, so that the shared memory area corresponds to the file in the Shm file system.
After a shared memory region has been created, it is also mapped to the process address space, and the system calls Shmat () to complete this function. Because a file with the same name in the file system SHM has been created when you call Shmget (), the process of calling Shmat () is equivalent to a file procedure with the same name in the map file system SHM, similar to Mmap ().
2. System V Shared Memory API
For System V shared memory, there are mainly the following api:shmget (), Shmat (), SHMDT (), and Shmctl ().
#include <sys/ipc.h>clude <sys/shm.h>
Shmget () is used to obtain the ID of the shared memory area, and if the specified shared area does not exist, the corresponding zone is created. Shmat () maps the shared memory area to the address space of the calling process so that the process can easily access the shared area. The SHMDT () call is used to dismiss a process's mapping to a shared memory region. SHMCTL implements control operations on shared memory areas. Here we do not specifically introduce these system calls, readers can refer to the corresponding manual page, the following example will give their calling method.
Note: The internal implementation of Shmget contains many important system V shared memory mechanisms, and Shmat does not really change the page table of a process when it maps the shared memory area to process space. When a process accesses a memory-mapped region for the first time, it causes a page fault exception because there is no allocation of the physical page table, and the kernel then assigns the corresponding table of pages to the shared memory-mapped region based on the appropriate storage management mechanism.
3. System V Shared memory limit
In the/proc/sys/kernel/directory, the limit of System V shared memory is recorded, such as the maximum number of bytes in a shared memory area Shmmax, the system-wide maximum number of shared memory area identifiers Shmmni, etc., can be adjusted manually, but this is not recommended.
In [2], the test methods for these limitations are given, and are not described.
4, System V shared memory example
This section will give the use of the System V shared memory API, and compare the analysis of the System V shared memory mechanism with MMAP () mapping ordinary files to realize the difference between shared memory, first given two processes through System V shared memory Communication example:
/***** testwrite.c *******/#include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #include <unistd.h>typedef Struct{char name[4];int Age;} people;main (int argc, char** argv) {int shm_id,i;key_t Key;char Temp;people *p_map;char* name = "/DEV/SHM/MYSHM2", key = Ftok (name,0), if (key==-1) perror ("Ftok error"); Shm_id=shmget ( Key,4096,ipc_creat); if (shm_id==-1) {perror ("Shmget error"); return;} P_map= (people*) Shmat (shm_id,null,0); temp= ' a '; for (i = 0;i<10;i++) {temp+=1;memcpy ((* (P_map+i)). name,&temp,1 );(* (P_map+i)). Age=20+i;} if (SHMDT (p_map) ==-1) perror ("Detach Error");} /********** testread.c ************/#include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h > #include <unistd.h>typedef struct{char name[4];int age;} people;main (int argc, char** argv) {int Shm_id,i;key_ T key;people *p_map;char* name = "/DEV/SHM/MYSHM2", key = Ftok (name,0), if (key = =-1) perror ("Ftok error"); shm_id = Shmget (ke Y,4096,ipc_creat); if (shm_id = =-1) {perror ("ShmgetError "); return;} P_map = (people*) shmat (shm_id,null,0); for (i = 0;i<10;i++) {printf ("name:%s\n", (* (P_map+i)). Name);p rintf ("Age%d\n ", (* (P_map+i)). age);} if (SHMDT (p_map) = =-1) perror ("Detach error");}
TESTWRITE.C creates a System V shared memory area where it writes formatted data, and TESTREAD.C accesses the same system V shared memory area to read out the formatted data. Two programs were compiled into Testwrite and Testread, successively executed./testwrite and./testread./testread output is as follows:
Name:bage 20;name:cage 21;name:dage 22;name:eage 23;name:fage 24;name:gage 25;name:hage 26;name:iage 27;name:jage 28;name:kage 29;
Through the analysis of the test results, compared with the System V and mmap () mapping ordinary files to achieve shared memory communication, you can draw the following conclusions:
1, the System V shared in-memory data, never write to the actual disk file, and through mmap () mapping ordinary file implementation of shared memory communication can specify when to write data to the disk file. Note: As mentioned earlier, the System V shared memory mechanism is actually implemented by mapping files in the special file system SHM, the installation point of the filesystem SHM on the swap partition, and after the system reboots, all the content is lost.
2, System V shared memory is persistent with the kernel, even if all access to the shared memory process has terminated gracefully, the shared memory area still exists (unless the shared memory is explicitly removed), before the kernel reboots, any overwrite operations on the shared memory area will remain.
3. When calling Mmap () to map ordinary files for interprocess communication, it is important to consider when the process terminates the impact on the communication. The process of communicating through System V shared memory is not. Note: The use of shmctl is not given here, and the principle is similar to Message Queuing.
Conclusion:
Shared memory allows two or more processes to share a given store, because the data does not need to replicate back and forth, so it is the fastest inter-process communication mechanism. Shared memory can be implemented through the mmap () mapping of ordinary files (in special cases, anonymous mappings), or through the System V shared memory mechanism. The application interface and principle are simple and the internal mechanism is complex. In order to achieve more secure communication, it is often used in conjunction with synchronization mechanisms such as semaphores.
Shared memory involves knowledge of storage management and file system, and it is difficult to understand its internal mechanism, and the key is to hold tightly to the important data structure used by the kernel. System V shared memory is organized in the form of files in the special file system SHM. The identifier for shared memory can be created or obtained through Shmget. Once the shared memory identifier is obtained, the memory area is mapped to the virtual address space of the process through Shmat.
(reprint) Linux zombie process processing SIGCHLD signal Linux Environment interprocess communication (V): Shared memory (bottom)