Inter-process communication in Linux (5) shared memory (lower)

Source: Internet
Author: User
Inter-process communication in Linux (5): Shared Memory (2)

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_perm shm_perm;

Struct file * shm_file;

Int ID;

Unsigned long shm_nattch;

Unsigned long shm_segsz;

Time_t shm_atim;

Time_t shm_dtim;

Time_t shm_ctim;

Pid_t shm_cprid;

Pid_t shm_lprid;

};

 

The most important field in this structure should be shm_file, which 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 maintains all the shared memory areas in the system through the data structure struct ipc_ids shm_ids. 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
The shmid_kernel structure's file type pointer shm_file points to the corresponding file in the file system SHM, so that the shared memory area corresponds to the file in the SHM file system.

After creating a shared memory area, you need to map it to the process address space. The system calls shmat () to complete this function. When shmget () is called, a file with the same name in the file system SHM has been created, which corresponds to the shared memory region. Therefore, the shmat () the process is equivalent to the file process with the same name in the shm of the ing file system. The principle is similar to that of MMAP.

Back to Top

2. System V shared memory API

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

# Include <sys/IPC. h>

# Include <sys/SHM. h>

 

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 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.

Back to Top

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.

Back to Top

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 <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 (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: B age 20; Name: C age 21; Name: d age 22; Name: e age 23; Name: F age 24;

Name: G age 25; Name: H age 26; Name: I age 27; Name: J age 28; Name: K age 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.

Back to Top

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, 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.

 

References

[1] understanding the Linux kernel, 2nd edition, by Daniel P. bovet, marcocesati, which focuses on each topic and has a clear context.

[2] UNIX Network Programming Volume 2: inter-process communication, Author: W. Richard Steven S, Translator: Yang jizhang, Tsinghua University Press. MMAP () is described in detail.

[3] source code analysis for Linux kernel (I): source code analysis related to MMAP () is provided by Mao decao, Hu Ximing, and Zhejiang University Press.

[4] shmget, shmat, shmctl, and shmdt manuals

About the author

Zheng yanxing, a doctorate degree from the National Defense University. Contact info:
Mlinux@163.com

Score for this article

 

Related Article

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.