Sender: applies for the shared memory area and writes data to the shared memory area.
Sender. c
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/shm. h> # define BUFSIZ 102400 int main () {int shmid =-1; char * viraddr; char buffer [BUFSIZ]; key_t shmkey = <SPAN style = "COLOR: # ff0000 "> ftok ("/home/cip/test/", 0); </SPAN> shmid = shmget (shmkey, BUFSIZ, IPC_CREAT | 0666 ); // apply for the sharing region viraddr = (char *) shmat (shmid,); // The attachment share address to the process while (1) {puts ("enter some text: "); fgets (buffer, BUFSIZ, stdin); strcat (viraddr, buffer); add data to shared memory if (strncmp (buffer," end ", 3) = 0) {break ;}} shmdt (viraddr); // undo address ing return 0 ;#include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/shm. h> # define BUFSIZ 102400int main () {int shmid =-1; char * viraddr; char buffer [BUFSIZ]; key_t shmkey = ftok ("/home/cip/test/", 0); shmid = shmget (shmkey, BUFSIZ, IPC_CREAT | 0666 ); // apply for the sharing region viraddr = (char *) shmat (shmid,); // The attachment share address to the process while (1) {puts ("enter some text: "); fgets (buffer, BUFSIZ, stdin); strcat (viraddr, buffer); add data to shared memory if (strncmp (buffer," end ", 3) = 0) {break ;}} shmdt (viraddr); // undo address ing return 0;} gcc-o send sender. c
Acceptor:
Read the content of the shared area
# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/shm. h> # define BUFSIZ 102400 int main () {int shmid =-1; char * viraddr; char buffer [BUFSIZ]; key_t shmkey = ftok <SPAN style = "COLOR: # ff0000 "> ("/home/cip/test/", 0); </SPAN> shmid = shmget (shmkey, BUFSIZ, IPC_CREAT | 0666 ); // obtain the shared region viraddr = (char *) shmat (shmid,); printf ("Your message is: \ n % s", viraddr); shmdt (viraddr ); shmctl (shmid, IPC_RMID, 0); return 0 ;}# include <stdio. h> # include <stdlib. h> # include <string. h> # include <unistd. h> # include <sys/types. h> # include <sys/shm. h> # define BUFSIZ 102400int main () {int shmid =-1; char * viraddr; char buffer [BUFSIZ]; key_t shmkey = ftok ("/home/cip/test/", 0); shmid = shmget (shmkey, BUFSIZ, IPC_CREAT | 0666 ); // obtain the shared region viraddr = (char *) shmat (shmid,); printf ("Your message is: \ n % s", viraddr); shmdt (viraddr ); shmctl (shmid, IPC_RMID, 0); return 0 ;}