https://blog.csdn.net/y396397735/article/details/50651633

使用mmap内存映射实现一端写,另一端读的进程间通信


写端代码write.c

/*write.c*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> /*映射内存大小*/ #define MAPLEN 0x100 /*定义一个学生信息结构体*/ struct STU { int id; char name[20]; char sex; }; /*出错信息统一处理函数*/ void sys_err(char *str, int exitno) { perror(str); exit(exitno); } int main(int argc, char*argv[]) { struct STU *pm;//STU结构体指针 int fd, i = 0; if(argc < 2){ printf("args error\n"); exit(1); } fd = open(argv[1], O_RDWR | O_CREAT, 0777); //打开一文件 if(fd < 0){ sys_err("open", 1); } if(lseek(fd, MAPLEN - 1, SEEK_SET) < 0){//文件偏移至分配的内存地址末端 sys_err("lseek", 3); } if(write(fd, "\0", 1) < 0){ //末端赋值为'\0' sys_err("write", 4); } /*将文件映射至进程的地址空间*/ pm = mmap(NULL, MAPLEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(pm == MAP_FAILED){ sys_err("mmap", 2); } /*关闭文件描述符*/ close(fd); /*对文件进行写入操作*/ while(1){ pm->id = i; sprintf(pm->name, "yu-%d", i); if(i % 2 == 0){ pm->sex = 'm'; }else{ pm->sex = 'w'; } i++; sleep(1); } munmap(pm, MAPLEN); return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

读端代码read.c

/*read.c*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> #define MANLEN 0x1000 struct STU { int id; char name[20]; char sex; }; void sys_err(char *str, int exitno) { perror(str); exit(exitno); } int main(int argc, char *argv[]) { struct STU *pm; int fd, i = 0; if (argc < 2) { printf("args error\n"); exit(1); } fd = open(argv[1], O_RDWR); if (fd < 0){ sys_err("open", 1); } pm = mmap(NULL, MAPLEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(pm == MAP_FAILED){ sys_err("mmap", 2); } /*关闭文件*/ close(fd); /*删除文件*/ unlink(argv[1]); /*在内存中读数据*/ while(1){ printf("%d\n", pm->id); printf("%s\n", pm->name); printf("%c\n", pm->sex); sleep(1); } munmap(pm, MAPLEN); return 0; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

执行过程:

yu@ubuntu:~/Linux/211/tongxin$ ls
read.c  write.c
yu@ubuntu:~/Linux/211/tongxin$ gcc -o write write.c yu@ubuntu:~/Linux/211/tongxin$ gcc -o read read.c yu@ubuntu:~/Linux/211/tongxin$ ls read read.c write write.c
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

此时执行写操作

yu@ubuntu:~/Linux/211/tongxin$ ./write myfile
//在向myfile文件中写数据
  • 1
  • 2

另开一终端到当前目录,执行如下读操作:

yu@ubuntu:~/Linux/211/tongxin$ ls
read  read.c  write  write.c  myfile
yu@ubuntu:~/Linux/211/tongxin$ ./read myfile 6 yu-6 m 7 yu-7 w ^C//读取写入的内容Ctrl+C退出
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

退出后,执行ls,可发现myfile文件已删除

yu@ubuntu:~/Linux/211/tongxin$ ls
read  read.c  write  write.c
  • 1
  • 2
版权声明:个人学习之路,若有误,欢迎指正。其中一些博文被证明有错误的地方,最近比较忙,没时间更正,谨慎参考!! https://blog.csdn.net/y396397735/article/details/50651633

转载于:https://www.cnblogs.com/diegodu/p/9262314.html

Linux进程共享通信 -- mmap实现相关推荐

  1. linux进程的通信文件映射,Linux进程间通信 -- mmap函数的使用

    mmap/munmap函数 函数原型 #include void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t ...

  2. linux进程管道通信缺点,Linux进程通信(IPC)的方式详解

    前言:Linux进程通信的方式 什么是进程通信?进程通信是指进程之间交换信息 进程通信方式共有6种: 管道(pipe),包括流管道(s_pipe)和有名管道(named pipe) 信号(signal ...

  3. linux进程管道通信缺点,Linux 进程间通信(1) -- 管道

    进程间通信(IPC - InterProcess Communication) 通信的方式有很多: 文件, 管道, 信号, 共享内存, 消息队列, 套接字, 命名管道等等; 但是由于设计缺陷现在常用的 ...

  4. linux进程socket通信,linux进程间通信--socket套接字 实例代码

    可以实现通信的代码实现,拿去用! 原文来自:http://blog.chinaunix.net/uid-26790551-id-3171897.html 服务器端: #include #include ...

  5. Linux进程之间通信 消息队列

    使用命令 ipcs -q  查看对应的消息队列 代码 文件接收者 #include <sys/types.h> #include <stdio.h> #include < ...

  6. Linux进程之间通信 信号

    2) SIGINT 程序终止(interrupt)信号, 在用户键入INTR字符(通常是Ctrl-C)时发出,用于通知前台进程组终止进程. 3) SIGQUIT 和SIGINT类似, 但由QUIT字符 ...

  7. c++ fork 进程时 共享内存_linux共享内存mmap

    Linux进程间通信-共享内存mmap 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的 ...

  8. linux共享存储通信实验,Linux进程通信——共享存储

    共享内存是进程间通信最有用的方式,也是最快的IPC形式.共享内存是说:同一块内存被映射到多个进程的地址空间.但是共享内存并不提供同步机制,因此需要互斥锁或者信号量.使用共享内存唯一需要注意的是:当前如 ...

  9. 【Linux 应用编程】进程管理 - 进程间通信IPC之共享内存 mmap

    IPC(InterProcess Communication,进程间通信)是进程中的重要概念.Linux 进程之间常用的通信方式有: 文件:简单,低效,需要代码控制同步 管道:使用简单,默认阻塞 匿名 ...

最新文章

  1. MyBatisPlus中删除方法deletetById、deleteBatchIds、deleteByMap的使用
  2. C语言stdio相关的几个函数
  3. 服务器维护,日志分析常用命令
  4. 【分享】关于对象关系映射的理解
  5. Oracle/mysql联合查询union、union all
  6. js 小数自动补0_JavaScript 时分秒时间代码(自动补零)
  7. 教育为什么重要 大数据_为什么开放数据在教育中很重要
  8. 计算机安全的加密技术,计算机安全加密技术研究(4篇)(共14695字).doc
  9. 大型网络之---公司内部局域网
  10. 理解并行和并发的区别?
  11. windows系统清理磁盘临时文件,及缓冲文件,及离线文件和空闲文件
  12. centos安装输入法
  13. 斐讯k3c V1.7D frp升级
  14. 小米平板2刷android,小米平板2怎么刷回MIUI 小米平板2刷回MIUI教程
  15. 2-1:编写一个Java应用程序,输出俄文字母表。
  16. 磊科nw332 linux驱动下载,磊科Netcore NW332驱动
  17. 美柚社区精选:贴心宝妈的八大育儿经验
  18. 献给父亲,《手风琴》;《夺命手术》,母爱无疆。
  19. 回环检测回环校正(二):回环校正原理
  20. sql注入及用PrepareStatement就不用担心sql注入了吗?

热门文章

  1. 1.5 编程基础之循环控制 33 计算分数加减表达式的值 python
  2. 第61课 查分程序 《小学生C++趣味编程》
  3. 博图os更新_博途TIA安装与更新
  4. 【ES11(2020)】全局属性 globalThis
  5. Qt工作笔记-QVector与QMap查找效率实战
  6. C++ STL 乱序算法
  7. linux cpu不足处理运维,Linux运维知识之Linux服务器CPU占用率较高问题排查思路
  8. linux运行powershell,linux – 是否可以编写一个在bash / shell和PowerShell中运行的脚本?...
  9. 好玩gan_效果超赞服务器挤爆!用GAN生成人像油画火了,带你一秒回到文艺复兴...
  10. java中截取部分字符串_JAVA中截取字符串substring用法详解