11 }
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705111325371.png)
### 获取共享内存
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705113215411.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
1 #include
2 #include<sys/ipc.h>
3 #include<sys/types.h>
4 #include<sys/shm.h>
5 #include"mp.h"
6 using namespace std;
7 int main()
8 {
9 key_t key=ftok(PATH,proj_id);
10 int shmid=shmget(key,4096,IPC_CREAT|IPC_EXCL);
11 if(shmid<0)
12 {
13 cerr<<“shmget()”<<endl;
14 return 1;
15 }
16 hex(cout);
17 cout<<"my key is "<<key<<endl;
18 dec(cout);
19 cout<<"my shmid is "<<shmid<<endl;
20 return 0;
21 }
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021070511442385.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
介绍两个命令: ipcs -m 查看共享内存
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705114819178.png)
删除共享内存的命令 ipcrm -m + 共享内存的shmid号
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705114903531.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/202107151520502.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705115138460.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
上图的perms的值为0,表明我没有任何权限,那要这个共享内存干嘛?所以我们在创建时必须给其加上权限。以八进制方式加
1 #include
2 #include<sys/ipc.h>
3 #include<sys/types.h>
4 #include<sys/shm.h>
5 #include"mp.h"
6 using namespace std;
7 int main()
8 {
9 key_t key=ftok(PATH,proj_id);
10 int shmid=shmget(key,4096,IPC_CREAT|IPC_EXCL|0664);
11 if(shmid<0)
12 {
13 cerr<<“shmget()”<<endl;
14 return 1;
15 }
16 hex(cout);
17 cout<<"my key is "<<key<<endl;
18 dec(cout);
19 cout<<"my shmid is "<<shmid<<endl;
20 return 0;
21 }
~
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705115333604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
### 删除共享内存
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210709120226307.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
我们可以使用ipcrm -m +shmid 命令删除,但共享内存为什么不能用完不用我们自己手动去释放,而是调用一个函数去释放它,这里介绍。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705120014273.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705120301505.png)
1 #include
2 #include<sys/ipc.h>
3 #include<sys/types.h>
4 #include<unistd.h>
5 #include<sys/shm.h>
6 #include"mp.h"
7 using namespace std;
8 int main()
9 {
10 key_t key=ftok(PATH,proj_id);
11 int shmid=shmget(key,4096,IPC_CREAT|IPC_EXCL|0664);
12 if(shmid<0)
13 {
14 cerr<<“shmget()”<<endl;
15 return 1;
16 }
17 hex(cout);
18 cout<<"my key is "<<key<<endl;
19 dec(cout);
20 cout<<"my shmid is "<<shmid<<endl;
21 sleep(10);
22 shmctl(shmid,IPC_RMID,NULL);
23 return 0;
24 }
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705124345727.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
### 共享内存的挂载
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210709120137869.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705125116351.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
1 #include<iostream>
2 #include<sys/ipc.h>
3 #include<sys/types.h>
4 #include<unistd.h>
5 #include<sys/shm.h>
6 #include"mp.h"
7 using namespace std;
8 int main()
9 {
10 key_t key=ftok(PATH,proj_id);
11 int shmid=shmget(key,4096,IPC_CREAT|IPC_EXCL|0664);
12 if(shmid<0)
13 {
14 cerr<<“shmget()”<<endl;
15 return 1;
16 }
17 hex(cout);
18 cout<<"my key is "<<key<<endl;
19 dec(cout);
20 cout<<"my shmid is "<<shmid<<endl;
W> 21 char* s=(char*)shmat(shmid,NULL,0);
22 sleep(10);
23 shmctl(shmid,IPC_RMID,NULL);
24 return 0;
25 }
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705130431904.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
### 去除挂载
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210709120201433.png)
\**int shmdt(const void *shmaddr);** 只需要将shmat返回值传入即可
1 #include
2 #include<sys/ipc.h>
3 #include<sys/types.h>
4 #include<unistd.h>
5 #include<sys/shm.h>
6 #include"mp.h"
7 using namespace std;
8 int main()
9 {
10 key_t key=ftok(PATH,proj_id);
11 int shmid=shmget(key,4096,IPC_CREAT|IPC_EXCL|0664);
12 if(shmid<0)
13 {
14 cerr<<“shmget()”<<endl;
15 return 1;
16 }
17 hex(cout);
18 cout<<"my key is "<<key<<endl;
19 dec(cout);
20 cout<<"my shmid is "<<shmid<<endl;
21 char* s=(char*)shmat(shmid,NULL,0);
22 sleep(10);
23 shmdt(s);
24 sleep(4);
25 shmctl(shmid,IPC_RMID,NULL);
26 return 0;
27 }
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705130752568.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
## 利用共享内存实现简单通信
mp.h
#pragma once
2 #define PATH “/tmp”
3 #define proj_id 7756
server
1 #include
2 #include<sys/ipc.h>
3 #include<sys/types.h>
4 #include<unistd.h>
5 #include<sys/shm.h>
6 #include"mp.h"
7 using namespace std;
8 int main()
9 {
10 key_t key=ftok(PATH,proj_id);
11 int shmid=shmget(key,4096,IPC_CREAT|IPC_EXCL|0664);
12 if(shmid<0)
13 {
14 cerr<<“shmget()”<<endl;
15 return 1;
16 }
17 hex(cout);
18 cout<<"my key is "<<key<<endl;
19 dec(cout);
20 cout<<"my shmid is "<<shmid<<endl;
21 char* s=(char*)shmat(shmid,NULL,0);
22 while(1)
23 {
24 sleep(1);
25 cout<<s<<endl;
26 }
27 shmdt(s);
28 shmctl(shmid,IPC_RMID,NULL);
29 return 0;
30 }
client
1 #include
2 #include<sys/ipc.h>
3 #include<sys/types.h>
4 #include<unistd.h>
5 #include<sys/shm.h>
6 #include"mp.h"
7 using namespace std;
8 int main()
9 {
10 key_t key=ftok(PATH,proj_id);
11 int shmid=shmget(key,4096,0);
12 if(shmid<0)
13 {
14 cerr<<“shmget()”<<endl;
15 return 1;
16 }
17 char* s=(char*)shmat(shmid,NULL,0);
18 char c=‘a’;
19 for(;c<=‘z’;c++)
20 {
21 s[c-‘a’]=c;
22 sleep(3);
23 }
24 shmdt(s);
25 return 0;
26 }
~
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210705131954895.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1MzEzNzE0,size_16,color_FFFFFF,t_70)
**共享内存底层不存在同步与互斥机制,因为在写的时候也可以读。**
## IPC命令