Shared Memory:
Used for data transfer between processes, which is most efficient in the System V version, but it is not synchronous and mutually exclusive, so it is often used in conjunction with semaphores.
Nattch: Indicates how many processes are hooked up to shared memory. To view its values with the IPCS-M command
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/14/wKioL1cTJATCl3OTAAAkrhdXGBc987.png "title=" Nattch . PNG "width=" 588 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:588PX;HEIGHT:97PX; "alt=" Wkiol1ctjatcl3otaaakrhdxgbc987.png "/>
Delete key value with ipcrm-m +key value;
Graphics Understanding Shared Memory:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/17/wKiom1cTMDOjC8YqAAAxNKhZNZk793.png "title=" pp.png "alt=" Wkiom1ctmdojc8yqaaaxnkhznzk793.png "/>
#include <sys/types.h>
#include <sys/shm.h>
1. Create a shared memory function:
int Shget (key_t key,size_t size, int shmflg* buf);
The second parameter is its size, which is generally 4k or 4096 integer multiples;
The third parameter is generally: ipc_creat| IPC_EXCL;
2. function to destroy shared memory:
int shmctl (int shm _id,int cmd,int shmflg* buf);
The second parameter is generally: ipc_rmid;
3. Hook up on shared memory:
void *shmat (int shmid, const void *shmaddr, int shmflg);
4. To associate shared memory
int SHMDT (const void *SHMADDR);
Shared Memory Implementation code:
comm.h 1 #include <stdio.h> 2 #include <sys/ipc.h> 3 #include <sys/shm.h> 4 #include <stdlib.h> 5 #include < unistd.h> 6 #define _PATH_ "." 7 #define _PROJ_ID_ 0X7777 8 #define _SIZE_ 4096 9 int shm_create () 10 { 11 key_t Key=ftok (_path_, _proj_id_); 12 if (key<0) 13 { 14 perror ("Ftok"); 15 return -1; 16 } 17 int shm_id=shmget (key,_SIZE_,IPC_CREAT| ipc_excl|0666); 18 if (shm_id<0) 19 { 20 perror ("Shmget "); 21 return -1; 22 } 23 return shm_id; 24 } 25 26 int shm_get () 27 { 28 29 30 key_t Key=ftok (_path_, _proj_id_); 31 if (key<0) 32 { 33 perror ("Ftok"); 34 return -1; 35 } 36 int shm_id=shmget (Key,_SIZE_,IPC_CREAT);// Get the Created 37 if (shm_id<0) 38 { 39 &nbsP; perror ("Shmget"); 40 return -1; 41 } 42 return shm_id; 43 44 } //server.c 1 #include "comm.h" 2 3 int main () 4 { 5 int shm _id=shm_create (); 6 sleep (&NBSP;&NBSP;7&NBSP;&NBSP;CHAR&NBSP;*START=AT_SHM); SHM _ID); 8 int i; 9 for (i=0;i<20;++i) 10 { 11 12 printf ("%s\n", start); 13 sleep (1); 14 15 } 16 dt_shm (start); 17 shm_destroy (shm_id); 18 return 0; 19 }//client.c 1 #include "comm.h" 2 3 int main () 4 { 5 int shm_id= Shm_get (); 6 sleep (5); 7 char *start=at_shm (shm_ ID); 8 int i; 9 for (i=0;i<20;++i) 10 { 11 start[i]= ' A '; 12 start[i+1]= ' '; 13 } 14 sleep (7); 15 dt_shm (start); 16 sleep (&NBSP;17&NBSP;&NBSP;RETURN&NBSP;0;&NBSP;18&NBSP;&NBSP;19&NBSP;&NBSP;20&NBSP;&NBSP;21); 22 } //makefile 1 . phony:all 2 all:server client 3 server:server.c 4 gcc -o [email protected] $^ 5 client:client.c 6 gcc -o [email protected] $^ 7 . Phony:clean 8 clean: 9 rm -f server client
: 650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/14/wKioL1cTKj-jcXmSAAB0rY9DX6U721.png "title=" SHMP. PNG "alt=" Wkiol1ctkj-jcxmsaab0ry9dx6u721.png "/>
Results Analysis:
When SERVER.C is running, there is nothing on the monitor, and when CLIENT.C is running to print something, the server can read the content and display it on the monitor. This enables communication between different processes, through shared memory.
This article is from the "Output Diamond pattern" blog, so be sure to keep this source http://10541571.blog.51cto.com/10531571/1764736
Shared memory of interprocess communication