Basic demo of Memory Sharing

Source: Internet
Author: User

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 ;}

 

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.