Shared Memory for inter-process communication and inter-process communication
Shared Memory is a physical memory shared by multiple time periods. Shared memory is the fastest way to share data between processes.
Header file: # include <sys/types. h> # include <sys/shm. h>
A: Create shared memory.
Int shmget (key_t key, int size, int shmflg)
1. key: key value, obtained by the ftok function.
2. size: the size of the shared memory.
3. shmflg: flag.
The shared memory identifier is returned successfully. If the shared memory identifier fails,-1 is returned.
B: ing shared memory.
Int shmat (int shmid, char * shmaddr, int flag)
1. shmid: the shared memory identifier returned by the shmget function.
2. flag: determines the method to determine the mapped address (usually NULL ).
If the shared memory is successfully mapped to the address in the process,-1 is returned.
C: delete a ing.
Int shmdt (char * shmddr)
D: Clear the shared memory.
Int shmctl (int shmid, int cmd, struct shmid_ds * buf)
Example: two processes use the shared memory to communicate with each other
Server. c
1 /************************************** * *********************************** 2> File Name: server1.c 3> Author: xu 4> Mail: eeexu123@163.com 5> Created Time: sunday, October 09, 2016, 6 ******************************* **************************************** */7 8 # include <stdio. h> 9 # include <stdlib. h> 10 # include <signal. h> 11 # include <sys/stat. h> 12 # include <sys/types. h> 13 # include <fcntl. h> 14 # include <unistd. h> 15 # include <sys/shm. h> 16 # include <sys/ipc. h> 17 # define BUFFER_SIZE 12818 19 void myfun () 20 {21 return; 22} 23 24 int main () 25 {26 struct mybuf27 {28 int pid; 29 char buf [BUFFER_SIZE]; 30}; 31 32 int key, shmid; 33 int pid; 34 struct mybuf * p; 35 // register SIGUSR1 to 36 signal (SIGUSR2, myfun); 37 key = ftok (". /e. c ", 'A'); 38 if (key <0) 39 {40 printf (" creat two process error \ n "); 41 exit (1 ); 42} 43 // create shared memory 44 shmid = shmget (key, BUFFER_SIZE, IPC_CREAT | S_IRWXU); 45 if (shmid <0) 46 {47 perror ("shmget "); 48 exit (1); 49} 50 printf ("create share memory success \ n"); 51 // ing shared memory 52 p = (struct mybuf *) shmat (shmid, NULL, 0); 53 if (p = NULL) 54 {55 perror ("shmat"); 56 exit (1 ); 57} 58 printf ("map the share memory success \ n"); 59 60 p-> pid = getpid (); // write the server PID into the shared memory 61 62 pause (); // wait for the client 63 64 pid = p-> pid; // read the client PID65 66 while (1) 67 {68 printf ("parent process start write share memory \ n"); 69 fgets (p-> buf, 124, stdin); 70 kill (pid, SIGUSR1 ); 71 pause (); 72} 73 shmdt (p); 74 shmctl (shmid, IPC_RMID, NULL); 75 76 return 0; 77}
Client. c
/*************************************** * *********************************> File Name: client1.c> Author: xu> Mail: eeexu123@163.com> Created Time: ******************************** **************************************** /# include <stdio. h> # include <stdlib. h> # include <signal. h> # include <sys/stat. h> # include <sys/types. h> # include <fcntl. h> # include <unistd. h> # include <sys/shm. h> # include <sys/ipc. h ># define BUFFER_SIZE 128 void my_fun () {return;} int main () {struct my_buf {int pid; char buf [BUFFER_SIZE] ;}; int key, shmid; int pid; struct my_buf * p; // register SIGUSR2 into the function signal (SIGUSR1, my_fun); key = ftok (". /e. c ", 'A'); if (key <0) {perror (" fotk "); exit (1) ;}// create shared memory shmid = shmget (key, BUFFER_SIZE, IPC_CREAT | S_IRWXU); if (shmid <0) {perror ("shmget"); exit (1) ;}// ing shared memory p = (struct my_buf *) shmat (shmid, NULL, 0); if (p = NULL) {perror ("shmat"); exit (1 );} printf ("map the share memory success \ n"); pid = p-> pid; // read server PID p-> pid = getpid (); // write the client PID to kill (pid, SIGUSR2) in the shared memory; // send a signal to the server while (1) {pause (); printf ("client process recave data form share memory: % s \ n", p-> buf); kill (pid, SIGUSR2);} shmdt (p); shmctl (shmid, IPC_RMID, NULL); return 0 ;}