Shared memory of Linux process communication

Source: Internet
Author: User

Shared memory agrees that two or more processes share a given store, and because the data does not need to replicate back and forth, it is the fastest inter-process communication mechanism. Shared memory can be implemented through the mmap () mapping of ordinary files (and, in special cases, anonymous mappings), and through the System V shared memory mechanism. The application interface and principle are very easy 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 contains:

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: Assuming success, return shared bucket address, error return-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 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 actively eliminated with the end of the program, either by calling Shmctl Delete, or by tapping the command itself to delete it, or stay in the system forever.


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);}



Shared memory for Linux process communication

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.