Shared memory of interprocess communication

Source: Internet
Author: User

I. Understanding of Shared memory

(1) Shared memory is one of the most efficient interprocess communication processes that can read and write directly to memory without requiring any copy of the data.

(2) In order to exchange information between multiple processes, the kernel specifically exits a chunk of memory that can be mapped to its own private space by the process that needs to be accessed. The process can read and write directly to this piece of memory without having to copy the data, thereby greatly improving efficiency.

(3) Because multiple processes share a piece of memory, they also need to rely on some kind of synchronization mechanism.

Two. Operating flow of shared memory

(1) Create/open shared memory.

(2) Mapping shared memory, that is, the specified shared memory is mapped to the address space of the process for access.

(3) Revoke the shared memory mapping.

(4) Delete the shared memory object.

Three. Implementing the Code

Shm.h

1 #include <stdio.h>

2 #include <sys/shm.h>

3 #include <sys/ipc.h>

4 #include <unistd.h>

5 #include <sys/types.h>

6 #include <sys/wait.h>

7 #define _PATH_ "."

8 #define _PROJ_ID_ 0x6666

9 #define _SHM_SIZE_ 1024*4

Ten int get_shm ();

One char *at_shm (int shm_id);

DT_SHM int (char* addr);

int destroy_shm (int shm_id);

Shm.c

1 #include "shm.h"

2 int Get_shm ()

3 {

4 key_t _key=ftok (_PATH_,_PROJ_ID_);

5 int flag=ipc_creat| ipc_excl|0666;

6 int shm_id=shmget (_KEY,_SHM_SIZE_,FLAG);

7 if (shm_id<0)

8 {

9 perror ("Shmget");

Ten return-1;

11}

return shm_id;

13}

*AT_SHM char (int shm_id)

15 {

Return Shmat (shm_id,null,0);

17}

DT_SHM int (char* addr)

19 {

return Shmdt (addr);

21}

int destroy_shm (int shm_id)

23 {

Return Shmctl (Shm_id,ipc_rmid,null);

25}

Test function test.c

1 #include "shm.h"

2

3 int Main ()

4 {

5 int shm_id=get_shm ();

6 pid_t id=fork ();

7 if (id<0)

8 {

9 perror ("fork");

Ten return-1;

11}

else if (id==0)

13 {

*buf=at_shm char (shm_id);

i=0 int;

(I<10)

17 {

buf[i]= ' X ';

i++;

20}

buf[10]= ' + ';

DT_SHM (BUF);

23}

-Else

25 {

*buf=at_shm char (shm_id);

Sleep (2);

printf ("%s\n", buf);

DT_SHM (BUF);

//waitpid (id,null,0);

DESTROY_SHM (shm_id);

32}

return 0;

34}

Operation Result:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/3C/wKiom1cXQsfxvdsxAAAWGRT8Sqg906.png "title=" Shm.png "alt=" Wkiom1cxqsfxvdsxaaawgrt8sqg906.png "/>


This article is from the "Zwy" blog, make sure to keep this source http://10548195.blog.51cto.com/10538195/1765874

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