Linux Process Communication-shared memory

Source: Internet
Author: User

The Unified Call uses 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 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, 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 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: 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.

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.

POSIX shared memory

1. POSIX shared memory zone involves two steps:

1) specify a name parameter to call shm_open to create a new shared memory area object or open an existing shared memory area object.

2) Call MMAP to map the shared memory area to the address space of the calling process. The name parameter passed to shm_open is subsequently used by any other process that wants to share the memory zone.

2. Functions

(1) Create

# Include <sys/Mman. h>

Int shm_open (const char * Name, int Oflag, mode_t mode );

1) Name of the shared memory area

2) Oflag flag

3) mode permission bit, which is used when the o_creat flag is specified.

(2) Delete

Int shm_unlink (const char * Name );

(3) resize

You can call ftruncate to modify the size of common files or shared memory objects.

Int ftruncate (int fd, off_t length );

(4) view the status

When you open an existing shared memory area object, you can call fstat to obtain information about the object.

# Include <unistd. h>

# Include <sys/types. h>

# Include <sys/STAT. h>

Int Stat (const char * file_name, struct stat * BUF );

The stat structure of a common file can obtain more than 12 member information. However, when FD refers to a shared memory area object, only four members contain information.

Struct stat

{

Mode_t st_mode;

Uid_t st_uid;

Gid_t st_gid;

Off_t st_size;

};

G ++-g-o statt fstatt. cpp-LRT

3. Example

Example 1

View code

# Include <sys/STAT. h> # include <fcntl. h> # include <sys/Mman. h> # include <unistd. h> int main (INT argc, char ** argv) {int shm_id; struct stat Buf; char * PTR; If (argc! = 2) {printf ("Usage: shm_open <pathname> \ n"); exit (1);} shm_id = shm_open (argv [1], o_rdwr | o_creat, 0644 ); /* create a shared memory zone */ftruncate (shm_id, 100);/* modify the size of the shared area */fstat (shm_id, & BUF); PTR = MMAP (null, Buf. st_size, prot_read | prot_write, map_shared, shm_id, 0);/* connect to the shared memory zone */strcpy (PTR, "Hello Linux "); /* write to the shared memory zone */printf ("% s \ n", PTR);/* read the shared memory zone */shm_unlink (argv [1]); /* Delete the shared memory zone */}
Copy code

Example 2

Pshm_w

#include <sys/stat.h>#include <fcntl.h>#include <sys/mman.h>#include <unistd.h>#include <semaphore.h>#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc,char **argv){    int shm_id;    char *ptr;    sem_t *sem;        if(argc != 2)    {        printf("usage:shm_open <pathname>\n");        exit(1);    }    shm_id = shm_open(argv[1], O_RDWR, 0644);    sem = sem_open(argv[1], O_RDWR);    ptr = (char*)mmap(NULL,100,PROT_READ | PROT_WRITE, MAP_SHARED, shm_id, 0);    while(1)    {         sem_wait(sem);         fgets(ptr, 15, stdin);         printf("user:%s", ptr);         if((strcmp(ptr, "q\n")) == 0)             exit(0);         sem_post(sem);         sleep(1);     }     return (0);}
Copy the code pshm_r

# Include <sys/STAT. h> # include <fcntl. h> # include <sys/Mman. h> # include <unistd. h> # include <semaphore. h> # include <stdio. h> # include <stdlib. h> # include <string. h> int main (INT argc, char ** argv) {int shm_id; char * PTR; sem_t * SEM; If (argc! = 2) {printf ("Usage: shm_open <pathname> \ n"); exit (1);} shm_id = shm_open (argv [1], o_rdwr | o_creat, 0644 ); ftruncate (shm_id, 100); SEM = sem_open (argv [1], o_creat, 0644, 1); PTR = (char *) MMAP (null, 100, prot_read | prot_write, map_shared, shm_id, 0); strcpy (PTR, "\ 0"); // clear the shared memory while (1) {If (strcmp (PTR, "\ 0") = 0) {continue;} else {If (strcmp (PTR, "Q \ n") = 0) break; sem_wait (SEM); printf ("server: % s", PTR); strcpy (PTR, "\ 0"); sem_post (SEM );} sem_unlink (argv [1]); shm_unlink (argv [1]);}

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.