Overview
Shared memory is one of the simplest ways to communicate between processes. Shared memory allows two or more processes to access the same piece of memory, as if the malloc () function returns a pointer to the same physical memory area to a different process. When a process changes the contents of this address, other processes will be aware of the change.
Features of shared memory:
1) Shared memory is one of the fastest ways to share data between processes.
A process writes data to a shared memory area, and all processes that share the memory area can immediately see the contents.
2) using shared memory It is important to note that multiple processes are mutually exclusive to a given store access.
If a process is writing data to a shared memory area, the other process should not read or write the data until it is done with this step.
Common Functions
1) Create shared memory
Required header file:
#include <sys/ipc.h>
#include <sys/shm.h>
int Shmget (key_t key, size_t size,int SHMFLG);
Function:
Create or open a piece of shared memory area.
Parameters:
key : The return value of the inter-process communication key value, Ftok ().
size : The length of the shared bucket (in bytes).
SHMFLG : Identifies the function's behavior and shared memory permissions, with the following values:
ipc_creat: Creates if it does not exist
IPC_EXCL: Returns failure if it already exists
bit or permission bit: Shared memory bit or permission bit can be set to access the shared memory, the format and the open () function of the same mode_t (the use of open () Click this link), but the executable permission is not used.
return value:
Success: Shared memory identifier.
Failed:-1.
The sample code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys /types.h> #include <sys/ipc.h> #include <sys/shm.h> #define BUFSZ 1024int Main (int argc, char *argv[]) {int shmid;key_t Key;key = Ftok ("./", 2015); if (key = =-1) {perror ("Ftok");} Create shared memory Shmid = Shmget (key, BUFSZ, ipc_creat|0666), if (Shmid < 0) {perror ("Shmget"); exit ( -1);} return 0;}
The results of the operation are as follows:
2) Shared Memory mapping
Required header file:
#include <sys/types.h>
#include <sys/shm.h>
void *shmat (int shmid, const void *shmaddr, int shmflg);
Function:
put a shared memory segment is mapped to the data segment of the calling process. Simple to understand, let the process and shared memory establish a connection, let the process a pointer point to this shared memory.
Parameters:
Shmid: The return value of the shared memory identifier, Shmget ().
shmaddr: Shared memory mapped address (if NULL is automatically specified by the system), NULL is recommended.
SHMFLG: Access permissions and mapping criteria for shared memory segments ( typically 0 ), with the following values:
0: Shared memory has read writable permissions.
shm_rdonly: Read only.
shm_rnd: (valid only if SHMADDR is not empty)
return value:
Success: Shared memory segment mapping address (equivalent to this pointer pointing to this shared memory)
Failed:-1
3) de-shared memory mapping
Required header file:
#include <sys/types.h>
#include <sys/shm.h>
int SHMDT (const void *SHMADDR);
Function:
Separating shared memory from the current process (simply disconnecting does not remove shared memory, which is equivalent to having a previous pointer pointing to this shared memory, no longer pointing to).
Parameters:
shmaddr: Shared memory mapped address.
return value:
Success: 0
Failed:-1
4) Shared Memory control
Required Header files:
#include <sys/ipc.h>
#include <sys/shm.h>
int shmctl (int shmid, int cmd, struct shmid_ds *buf);
Function:
Control of shared memory properties.
Parameters:
shmid : Shared memory identifier.
cmd : Control of function functions, with the following values:
ipc_rmid : Delete. ( common )
ipc_set : Sets the Shmid_ds parameter, which is equivalent to replacing the original attribute value of shared memory with the attribute value in BUF.
ipc_stat : Save the Shmid_ds parameter and back up the original attribute value of shared memory into BUF.
shm_lock : Locks the Shared memory segment (Superuser).
shm_unlock : Unlocks the shared memory segment.
< Span style= "font-size:18px" >shm_lock is used to lock memory and disable memory swapping. Does not prevent other processes from accessing the shared memory after it is locked. The real implication is that the locked memory is not allowed to be swapped into virtual memory. The advantage of this is that shared memory is kept in memory, which improves program performance.
buf: The address of the Shmid_ds data type (click this link for specific types) to store or modify the properties of the shared memory.
return value:
Success: 0
Failed:-1
Practical Examples
So here's an example: Create two processes, create a shared memory in a process, write data to it, and read the data from shared memory through the B process.
The write-side code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys /types.h> #include <sys/ipc.h> #include <sys/shm.h> #define BUFSZ 512int Main (int argc, char *argv[]) {int Shmid;int ret;key_t Key;char *shmadd;//Create key value key = Ftok (".. /", 2015); if (key = =-1) {perror ("Ftok");} Create shared memory Shmid = Shmget (key, BUFSZ, ipc_creat|0666), if (Shmid < 0) {perror ("Shmget"); exit (-1);} Map Shmadd = Shmat (shmid, NULL, 0), if (Shmadd < 0) {perror ("Shmat"); _exit (-1);} Copy data to the shared memory area printf ("Copy data to shared-memory\n"); Bzero (Shmadd, BUFSZ); Shared Memory Empty strcpy (Shmadd, "How is You, mike\n"); return 0;}
The read-only code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys /types.h> #include <sys/ipc.h> #include <sys/shm.h> #define BUFSZ 512int Main (int argc, char *argv[]) {int Shmid;int ret;key_t Key;char *shmadd;//Create key value key = Ftok (".. /", 2015); if (key = =-1) {perror ("Ftok");} System ("ipcs-m"); View shared memory//open Shared memory Shmid = Shmget (key, BUFSZ, ipc_creat|0666), if (Shmid < 0) {perror ("Shmget"); exit (-1);}//Map Shmadd = s Hmat (Shmid, NULL, 0), if (Shmadd < 0) {perror ("Shmat"); exit (-1);} Read the shared memory area data printf ("data = [%s]\n", shmadd);//detach shared memory and current process ret = SHMDT (Shmadd), if (Ret < 0) {perror ("SHMDT"); exit (1);} else{printf ("deleted shared-memory\n");} Delete Shared memory Shmctl (Shmid, Ipc_rmid, NULL); System ("ipcs-m"); View shared memory return 0;}
The results of the operation are as follows:
For this tutorial sample code download please click here.
Linux system Programming-interprocess communication: Shared memory