网站首页 > 资源文章 正文
共享内存概念
共享内存是通信效率最高的IPC方式,因为进程可以直接读写内存,而无需进行数据的拷备。但是它没有自带同步机制,需要配合信号量等方式来进行同步。
共享内存被创建以后,同一块物理内存被映射到了多个进程地址空间,当有一个进程修改了共享内存的数据,其余的进程均可看见所修改的内容,反之亦然。
mmap函数
函数原型:
void mmap(void adrr, size_t length, int prot, int flags, int fd, off_t offset);
返回值:
成功:返回创建的映射区首地址;
失败:返回MAP_FAILED
具体参数含义:
addr:指向映射区的首地址,这是由系统内核所决定的,一般设为NULL;
length:欲创建的映射区大小;
prot:映射区的权限,一般有如下几种:
PROT_EXEC 映射区域可被执行
PROT_READ 映射区域可被读取
PROT_WRITE 映射区域可被写入
PROT_NONE 映射区域不能存取
flags:指映射区的标志位,MAP_FIXED与MAP_PRIVATE必须选择一个:
MAP_FIXED:对映射区所作的修改会反映到物理设备,但需要调用msync()或者munmap();
MAP_PRIVATE:对映射区所作的修改不会反映到物理设备。
fd:创建的映射区的文件描述符;
offset:被映射文件的偏移量,一般设为0,表示从头开始映射。
mumap函数
函数原型:
int munmap(void *addr, size_t length);
函数作用:
如同malloc之后需要free一样,mmap调用创建的映射区使用完毕之后,需要调用munmap去释放。
例程
写进程:
1#include <stdio.h>
2#include <sys/mman.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <string.h>
8
9typedef struct
10{
11 int id;
12 char name[20];
13 char gender;
14}stu;
15
16int main(int argc, char *argv[])
17{
18 stu *p = NULL;
19 int fd = 0;
20 stu student = {10, "harry", 'm'};
21
22 if (argc < 2) {
23 printf("useage: ./a.out file\n");
24 return -1;
25 }
26
27 fd = open(argv[1], O_RDWR | O_CREAT, 0664);
28 if (fd == -1) {
29 printf("ERROR: open failed!\n");
30 return -1;
31 }
32 ftruncate(fd, sizeof(stu));
33
34 p = mmap(NULL, sizeof(stu), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
35 if (p == MAP_FAILED) {
36 printf("ERROR: mmap failed!\n");
37 return -1;
38 }
39
40 close(fd);
41
42 while (1) {
43 memcpy(p, &student, sizeof(stu));
44 student.id++;
45 sleep(2);
46 }
47 munmap(p, sizeof(stu));
48
49 return 0;
50}
读进程:
1#include <stdio.h>
2#include <sys/mman.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <unistd.h>
7
8typedef struct
9{
10 int id;
11 char name[20];
12 char gender;
13}stu;
14
15int main(int argc, char *argv[])
16{
17 stu *p = NULL;
18 int fd = 0;
19
20 if (argc < 2) {
21 printf("useage: ./a.out file\n");
22 return -1;
23 }
24
25 fd = open(argv[1], O_RDONLY);
26 if (fd == -1) {
27 printf("ERROR: open failed!\n");
28 return -1;
29 }
30
31 p = mmap(NULL, sizeof(stu), PROT_READ, MAP_SHARED, fd, 0);
32 if (p == MAP_FAILED) {
33 printf("ERROR: mmap failed!\n");
34 return -1;
35 }
36
37 close(fd);
38
39 while (1) {
40 printf("id = %d, name = %s, gender = %c\n", p->id, p->name, p->gender);
41 sleep(2);
42 }
43
44 munmap(p, sizeof(stu));
45
46 return 0;
47}
> 2020 精选 阿里/腾讯等一线大厂 面试、简历、进阶、电子书 「**良许Linux**」后台回复「**资料**」免费获取
#### 看完的都是真爱,点个赞再走呗?您的「三连」就是良许持续创作的最大动力!
1. 关注**原创**「**良许Linux**」,第一时间获取最新Linux干货!
2. 后台回复【资料】【面试】【简历】获取精选一线大厂面试、自我提升、简历等资料。
3. 关注我的博客:[lxlinux.net](http://www.lxlinux.net)
- 上一篇: C++深拷贝和浅拷贝应用实例
- 下一篇: LSM Oops 内存错误根因分析与解决
猜你喜欢
- 2025-04-27 JIT原理简单介绍
- 2025-04-27 LSM Oops 内存错误根因分析与解决
- 2025-04-27 C++深拷贝和浅拷贝应用实例
- 2025-04-27 消息队列概念及其实现细节
- 2025-04-27 基于FIMC接口的CMOS摄像头驱动分析与设计
- 2025-04-27 高性能异步io机制:io_uring
- 2025-04-27 《C与指针》读书笔记五
- 2025-04-27 linux内核分析 SLAB原理及实现
- 2025-04-27 RapidJSON完全指南:高性能JSON解析与生成的最佳实践
- 2025-04-27 常用网络协议整理笔记(一)
你 发表评论:
欢迎- 04-27JIT原理简单介绍
- 04-27LSM Oops 内存错误根因分析与解决
- 04-27Linux系统编程—共享内存之mmap
- 04-27C++深拷贝和浅拷贝应用实例
- 04-27消息队列概念及其实现细节
- 04-27基于FIMC接口的CMOS摄像头驱动分析与设计
- 04-27高性能异步io机制:io_uring
- 04-27《C与指针》读书笔记五
- 最近发表
- 标签列表
-
- 电脑显示器花屏 (79)
- 403 forbidden (65)
- linux怎么查看系统版本 (54)
- 补码运算 (63)
- 缓存服务器 (61)
- 定时重启 (59)
- plsql developer (73)
- 对话框打开时命令无法执行 (61)
- excel数据透视表 (72)
- oracle认证 (56)
- 网页不能复制 (84)
- photoshop外挂滤镜 (58)
- 网页无法复制粘贴 (55)
- vmware workstation 7 1 3 (78)
- jdk 64位下载 (65)
- phpstudy 2013 (66)
- 卡通形象生成 (55)
- psd模板免费下载 (67)
- shift (58)
- localhost打不开 (58)
- 检测代理服务器设置 (55)
- frequency (66)
- indesign教程 (55)
- 运行命令大全 (61)
- ping exe (64)
本文暂时没有评论,来添加一个吧(●'◡'●)