Linux under inter-process communication of shared memory

The concept : This mechanism allows two or more processes by a common data structure into a shared memory area to access them. If the process is to access the shared memory area where this data structure, it is necessary to add a new linear region in its own address space, the new linear zone map page frame associated with this shared memory area. Such a page frame can be performed by simple processing by the kernel paging requests.

Advantages : shared memory (shared memory) is one of the means of communication between the Linux process the simplest maximum freedom. Use shared memory, different processes can read and write to the same memory. Since all processes to access its own memory space and access to the same shared memory, without the need for additional operating system or kernel calls, but also to avoid redundant copies of memory, so, in this way it is the most efficient and fastest inter-process communication.

Drawback : The kernel does not provide any mechanism for synchronizing access to shared memory, such as shared memory at the same time to the same address before the data write operation, the write data will be overwritten. Therefore, the use of shared memory typically also need to use other IPC mechanisms (such as semaphores) read and write synchronization and mutual exclusion.

Fundamental

Learn Linux memory management mechanism, it is easy to understand the principle of shared memory. As we all know, it is a kernel memory management page (page) for the unit, under Linux in general a page size is 4k. The program itself is linear virtual address space, the kernel manages the process from the virtual address space mapped to the corresponding page from. After you create a shared memory space, the kernel virtual address map different processes to the same page: So in different processes, access to the memory address of the shared memory where the final will be mapped to the same page. The following diagram illustrates the working mechanism of shared memory:

 

Instructions

Shared memory usage can be divided into creation -> Connections -> Use -> separation -> destroy these steps.

Shmget created using shared memory function (SHared Memory GET) function is used as follows:

shmget segment_id = int (shm_key, getpagesize (), IPC_CREAT | S_IRUSR | S_IWUSER);
shmget create a shared memory space of size page_size according shm_key, 3 is a series of parameters to create a parameter. If shm_key has been created, use the shm_key returns can connect to it to create a shared memory id.
Once created, in order to make the shared memory can be used by the current process, which must in turn connected to the operation. Use function shmat (SHared Memory ATtach), parameters passed via shared memory id returned to shmget:

= shared_memory (char *) the shmat (segment_id, 0, 0);
the shmat returns the address pointer mapped into the process virtual address space, such as the process can access a common memory buffer as access to shared memory. After the example we all passed two parameters 0, the default connection. In fact, the second parameter is the hope that the process of connecting the virtual address of the address space, if 0, Linux will they choose a suitable address; third parameter is the connectivity options may be:
SHM_RND: The second parameter points memory space automatically promoted to an integer multiple of the page size, I do not know if the next parameter, you need to control the size of the second parameter own memory space referred to.

SHM_RDONLY: The shared memory is read-only, non-writable.

When finished using the shared memory, using a function shmdt (SHared Memory DeTach) de connection. The shmat function to return the memory address as a parameter. After each of the last process using shared memory detach the shared memory, the kernel will automatically destroy the shared memory. Of course, the best we can explicitly be destroyed to avoid unnecessary waste of resources shared memory. Function shmctl (SHared Memory ConTroL) can return information shared memory and its control, such as

shmctl (segment_id, IPC_STAT, & shmbuffer );
can return information about the shared memory, and the information is stored in shmid_ds structure pointed to by the third parameter.
When an incoming IPC_RMID to the second argument, the shared memory will use the shared memory of the process of separation of shared memory shared memory is destroyed in the last one.

shmctl there are many other use, not repeat them.

The sample program

The following example program, process A is written every second random number to a shared memory, the number of the one second from the shared memory every read process b.

/*
* a.c
* write a random number between 0 and 999 to the shm every 1 second
*/
#include<stdio.h>
#include<unistd.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<error.h>
int main(){
int shm_id;
int *share;
int num;
srand(time(NULL));
shm_id = shmget (1234, getpagesize(), IPC_CREAT);
if(shm_id == -1){
perror("shmget()");
}
share = (int *)shmat(shm_id, 0, 0);
while(1){
num = random() % 1000;
*share = num;
printf("write a random number %d\n", num);
sleep(1);
}
return 0;
}

/*
* b.c
* read from the shm every 1 second
*/
#include<stdio.h>
#include<unistd.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<error.h>
int main(){
int shm_id;
int *share;
shm_id = shmget (1234, getpagesize(), IPC_CREAT);
if(shm_id == -1){
perror("shmget()");
}
share = (int *)shmat(shm_id, 0, 0);
while(1){
sleep(1);
printf("%d\n", *share);
}
return 0;
}

 

Guess you like

Origin www.cnblogs.com/best-farmer/p/10985387.html