写端
1 #include <string.h>
2 #include<stdio.h>
3 #include <sys/shm.h>
4 #include <unistd.h>
5
6 // int shmget(key_t key, size_t size, int shmflg);
7 // void *shmat(int shmid, const void *shmaddr, int shmflg);
8 // int shmdt(const void *shmaddr);
9
10 int main()
11 {
12 key_t key;
13 key = ftok(".",5);
14 int shmid = shmget(key,1024*5,IPC_CREAT|0600);
15 if(shmid == -1){
16 perror("why");
17 }
18 char *buf;
19 buf = shmat(shmid,0,0);
20 strcpy(buf,"this is GM and you!");
21 sleep(3);
22
23 shmdt(buf);
24 // int shmctl(int shmid, int cmd, struct shmid_ds *buf);
25 shmctl(shmid,IPC_RMID,0);
26
27 printf("write done\n");
28 return 0;
29 }
~
写端
1 #include <string.h>
2 #include<stdio.h>
3 #include <sys/shm.h>
4 #include <unistd.h>
5
6 // int shmget(key_t key, size_t size, int shmflg);
7 // void *shmat(int shmid, const void *shmaddr, int shmflg);
8 // int shmdt(const void *shmaddr);
9
10 int main()
11 {
12 key_t key;
13 key = ftok(".",5);
14 int shmid = shmget(key,1024*5,0);
15 if(shmid == -1){
16 perror("why");
17 }
18 char *buf;
19 buf = shmat(shmid,0,0);
20
21 printf("get contxt:%s\n",buf);
22
23 shmdt(buf);
24 // int shmctl(int shmid, int cmd, struct shmid_ds *buf);
25
26 printf("read done\n");
27 return 0;
28 }
~
~
~