Inter-process communication in Linux (5): Shared Memory (lower) (System V)

Source: Internet
Author: User

The system calls MMAP () to map a common file to implement shared memory. System V maps files in the SHM of a special file system to implement shared memory communication between processes.That is to say, each shared memory area corresponds to a file in the shm of the special File System (this is linked through the shmid_kernel structure), and will be elaborated later.

1. Principle of System V shared memory

Data to be shared between processes is stored in a region called IPC shared memory, all processes that need to access the shared area map the shared area to the address space of the process. System V shared memory obtains or creates an IPC shared memory area through shmget and returns the corresponding identifier. When the kernel ensures that shmget obtains or creates a shared memory area, initializes the shmid_kernel structure of the shared memory area, and creates and opens a file with the same name in the SHM of a special file system, and establish the corresponding dentry and inode structure of the file in the memory. The newly opened file does not belong to any process (any process can access the shared memory zone ). All of this is done by the system calling 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. It serves as a bridge between storage management and file systems, definition:

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 this structure should beShm_file stores the address of the file to be mapped.Each shared memory area object corresponds to a file in the shm of the special file system. Generally, files in the shm of the special file system cannot be accessed using methods such as read () and write, after the file is mapped to the process address space by means of shared memory, you can directly access the file by accessing the memory.

Here we use the chart in [1] to show the data structure related to the System V shared memory:

Like message queues and traffic signals, the kernel uses the data structure struct ipc_idsShm_ids maintains all shared memory areas in the system. The shm_ids.entries variable in points to an ipc_id structure array, 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 kern_ipc_perm is the shmid_kernel structure, and shmid_kernel is used to describe a shared memory area, in this way, the kernel can control all the shared areas of the system. At the same time, in the shmid_kernel structure, the file type pointer shm_file points to the corresponding file in the SHM file system, so that the shared memory area corresponds to the file in the SHM file system.

After creating a shared memory area, you must map it to the process address space.Shmat ()Complete this function. BecauseShmget ()A file with the same name in the file system SHM corresponds to the shared memory area. Therefore, the process of calling shmat () is equivalent to the process of ing files with the same name in the file system SHM, the principle is similar to that of MMAP.

2. System V shared memory API

The System V shared memory mainly includes the following APIs: shmget (), shmat (), shmdt (), and shmctl ().

#include #include 

Shmget () is used to obtain the ID of the shared memory area. If the specified shared area does not exist, a corresponding area is created.

Shmat () maps the shared memory area to the address space of the calling process, so that the process can conveniently access the shared area.

The shmdt () call is used to remove the process's ing to the shared memory area.

Shmctl controls the shared memory area.

Here we will not give a detailed introduction to these system calls. Readers can refer to the corresponding manual page and their call methods will be provided in the following examples.

Note: The internal implementation of shmget includes many important System V shared memory mechanisms;When the shmat maps the shared memory area to the process space, it does not actually change the page table of the process. When a process accesses the memory ing area for the first time, a page exception occurs because there is no physical page table allocation, then, the kernel allocates the corresponding page table for the shared memory ing area based on the corresponding storage management mechanism.

3. System V shared memory limit

In the/proc/sys/kernel/directory, record the limits of the system V shared memory, such as the maximum number of bytes in a shared memory area shmmax, shmmni, the maximum number of identifiers in the shared memory area, can be adjusted manually, but this is not recommended.

In [2], the test methods for these restrictions are provided.

4. Example of System V shared memory

This section describes how to use the System V shared memory API, and compares and analyzes the differences between the system V shared memory mechanism and MMAP () ing common files to achieve shared memory, first, an example of two processes communicating through the System V shared memory is given:

/***** testwrite.c *******/#include #include #include #include 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 #include #include #include 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(key,4096,IPC_CREAT);if(shm_id == -1){perror("shmget error");return;}p_map = (people*)shmat(shm_id,NULL,0);for(i = 0;i<10;i++){printf( "name:%s/n",(*(p_map+i)).name );printf( "age %d/n",(*(p_map+i)).age );}if(shmdt(p_map) == -1)perror(" detach error ");}

Testwrite. c creates a system V shared memory zone and writes formatted data to it. testread. c accesses the same System V shared memory zone and reads the formatted data. Compile the two programs testwrite and testread respectively, and execute./testwrite and./testread successively./testread. The output result 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, the following conclusions can be drawn by comparing the System V and MMAP () ing common files to achieve shared memory communication:

1. System V shares the data in the memory and never writes the data to the actual disk file () shared Memory communication implemented by ing common files can specify when data is written to disk files. Note: As mentioned above, the system V shared memory mechanism is actually implemented by ing files in the SHM of a special file system. The SHM Installation Point of the file system is on the swap partition. After the system restarts, all contents are lost.

2. System V shared memory continues with the kernel. Even if all processes accessing the shared memory have been terminated normally, the shared memory still exists (unless the shared memory is explicitly deleted ), any rewrite operations on the shared memory area will be retained until the kernel is rebooted.

3. When calling MMAP () to map common files for inter-process communication, you must consider the impact of Process Termination on communication. The process of communication through System V shared memory is not. Note: The shmctl usage example is not provided here. The principle is similar to that of message queue.

Conclusion:

Shared memory allows two or more processes to share a given storage zone, because data does not need to be replicated back and forth,Therefore, it is the fastest inter-process communication mechanism.Shared memory can be mapped to common files through MMAP () (anonymous ing can also be used in special cases), or through the System V shared memory mechanism. Application interfaces and principles are simple, and internal mechanisms are complex. To achieve safer communication, it is often used together with synchronization mechanisms such as traffic signals.

Shared Memory involves the knowledge of Storage Management and file systems. It is difficult to deeply understand its internal mechanism. The key is to closely grasp the important data structure used by the kernel. System V shared memory is organized as files in the SHM of a special file system. Shmget allows you to create or obtain the identifier of the shared memory. After obtaining the shared memory identifier, You need to map the memory area to the virtual address space of the process through shmat.

Source of the original article (Click here)

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.