Shared memory allows two or more processes to share a given store, because the data does not need to replicate back and forth, so it is the fastest inter-process communication mechanism. Shared memory can be implemented through the mmap () mapping of ordinary files (in special cases, anonymous mappings), or through the System V shared memory mechanism. The application interface and principle are simple and the internal mechanism is complex. In order to achieve more secure communication, it is often used in conjunction with synchronous mechanisms such as semaphores. The following main introduction to the System V shared memory mechanism, the main use of the system API includes:
1.shmget function: Gets a shared memory identifier.
int Shmget (key_t key, size_t size, int flag);
key: Rules for identifiersSize: Number of bytes in shared bucketflag: Permission to read and writereturn value: The ID of the shared store was successfully returned, failure returned-1
2.Shmat function: The process connects the shared memory to its address space.
void *shmat (int shmid, const void *addr, int flag);
Shmid: The ID of the shared store
Addr: typically 0, which indicates a connection to the first available address selected by the kernel
Flag: As mentioned above, generally 0 //Recommended value
Return value: If successful, returns the shared bucket address, error returned-1
3.SHMDT function: Derail the shared memory with the address space of the process.
int shmdt (void *addr);
Addr: The address of the shared bucket, the return value when the Shmat was previously called
SHMDT will reduce the Shm_nattch counter value in the related SHMID_DS structure by 1
4.shmctl function: Delete the shared memory.
int shmctl (int shmid,int cmd,struct shmid_ds *buf)
Shmid: ID of shared bucket
CMD: Some commands, have: Ipc_stat,ipc_rmid,shm_lock,shm_unlock
Note that shared memory will not be automatically eliminated with the end of the program, either by calling Shmctl Delete, or by tapping the command yourself to delete it, or forever remaining in the system.
A practical example:
Server-side:
/* Write data to a shared memory segment*/#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h& GT, #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <ctype.h> #include < Unistd.h> #define BUFSZ 4096int Main () {int shmid; char* Shmbuf; key_t key; char* msg; int Len; Key=ftok ("/tmp", 0); if ((Shmid=shmget (key,bufsz,ipc_creat|0666)) <0) {perror ("Shmget"); Exit (Exit_failure); } printf ("Segment created:%d\n", Shmid); System ("ipcs-m"); if ((shmbuf= (char*) Shmat (shmid,0,0)) <0) {perror ("Shmat"); Exit (Exit_failure); } msg= "This is the message written to the shared memory."; Len=strlen (msg); strcpy (SHMBUF,MSG); printf ("%s\ntotal%d characters has written to Gkfx memory.\n", Msg,len); if (SHMDT (SHMBUF) <0) {perror ("SHMDT"); Exit (Exit_failure); } exit (exit_success);}
Client:
/* Read data from a shared memory segment */#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm. h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <ctype.h> #include < Unistd.h> #define BUFSZ 4096int Main (int argc, char *argv[]) {int shmid; Char *shmbuf; key_t key; int Len; Key = Ftok ("/tmp", 0); if (Shmid = Shmget (key, 0, 0)) < 0) {perror ("Shmget"); Exit (Exit_failure); } if ((Shmbuf = (char *) Shmat (shmid, 0, 0)) < 0) {perror ("Shmat"); Exit (Exit_failure); } printf ("Info read form Shared memory is:\n%s\n", SHMBUF); if (SHMDT (SHMBUF) < 0) {perror ("SHMDT"); Exit (Exit_failure); } if (Shmctl (Shmid,ipc_rmid,null) < 0) {perror ("Shmctl"); Exit (Exit_failure); } exit (exit_success);}