Shared memory of Linux interprocess communication

Source: Internet
Author: User

One, shared memory
The kernel manages a piece of physical memory, allowing different processes to map at the same time, multiple processes can map the same piece of memory, the physical memory that is mapped at the same time by multiple processes , that is, shared memory.
The mapped physical memory is called hooking up, and the map is called off after it is exhausted.

1. Features of shared memory:

  Advantage : is the fastest IPC.
  disadvantage : To implement mutually exclusive access to shared memory by the programmer. How is it implemented?

2, programming Model: the use of specific functions can be viewed with the man manual (strongly recommended)

Process A:WRITESHM.C
1) Get key, Ftok ()
2) Use key to create a shared memory Shmget ()
3) Map shared memory (get virtual address), Shmat ()
4) Use shared memory to write data to shared memory
5) de-map SHMDT ()
6) If shared memory is no longer used, you can use Shmctl () to destroy the shared memory

Process B:READSHM.C

1) Get key, Ftok ()

2) Use key to obtain a shared memory Shmget ()

3) Map shared memory (get virtual address), Shmat ()

4) Use shared memory to read data in shared memory

5) de-map SHMDT ()

3, example

Process A:

//WRITESHM.C#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/shm.h>intMain () {//generate a keykey_t key = Ftok ("./", the); //Create shared memory, return an ID    intShmid = Shmget (Key,8, ipc_creat|0666|ipc_excl); if(-1==Shmid) {Perror ("Shmget failed"); Exit (1); }    //map shared memory, get virtual address    void*p = Shmat (Shmid,0,0); if((void*)-1==p) {perror ("Shmat failed"); Exit (2); }    //Write shared Memory    int*PP =p; *PP =0x12345678; * (pp +1) =0xFFFFFFFF; //de-mapping    if(-1==SHMDT (P)) {Perror ("Shmdt failed"); Exit (3); } printf ("Unlock the map successfully, click Enter to destroy the shared memory \ n");    GetChar (); //Destroying shared memory    if(-1==Shmctl (Shmid, Ipc_rmid, NULL)) {Perror ("Shmctl failed"); Exit (4); }    return 0;}


Process B:

//READSHM.C#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/shm.h>intMain () {//generate a keykey_t key = Ftok ("./", the); //get shared memory, return an ID    intShmid = Shmget (Key,0,0); if(-1==Shmid) {Perror ("Shmget failed"); Exit (1); }    //map shared memory, get virtual address    void*p = Shmat (Shmid,0,0); if((void*)-1==p) {perror ("Shmat failed"); Exit (2); }    //Read Shared memory    intx = * (int*) p; inty = * (int*) p +1); printf ("taken from shared memory: 0x%x and 0x%x \ n", x, y); //de-mapping    if(-1==SHMDT (P)) {Perror ("Shmdt failed"); Exit (3); }    return 0;}

Operation Result:

Writeshma:

Readshma:

Shared memory of Linux interprocess 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.