Linux进程共享通信 -- mmap实现
来源:互联网 发布:中文域名交易中心 编辑:程序博客网 时间:2024/06/05 10:28
使用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;}
读端代码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 0x1000struct 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;}
执行过程:
yu@ubuntu:~/Linux/211/tongxin$ lsread.c write.cyu@ubuntu:~/Linux/211/tongxin$ gcc -o write write.cyu@ubuntu:~/Linux/211/tongxin$ gcc -o read read.cyu@ubuntu:~/Linux/211/tongxin$ lsread read.c write write.c
此时执行写操作
yu@ubuntu:~/Linux/211/tongxin$ ./write myfile//在向myfile文件中写数据
另开一终端到当前目录,执行如下读操作:
yu@ubuntu:~/Linux/211/tongxin$ lsread read.c write write.c myfileyu@ubuntu:~/Linux/211/tongxin$ ./read myfile6yu-6m7yu-7w^C//读取写入的内容Ctrl+C退出
退出后,执行ls
,可发现myfile
文件已删除
yu@ubuntu:~/Linux/211/tongxin$ lsread read.c write write.c
0 0
- Linux进程共享通信 -- mmap实现
- 进程通信----共享内存以及mmap函数实现共享内存
- Linux--mmap实现进程间通信
- 进程通信--mmap内存共享
- 进程通信-共享内存-mmap()
- linux多线程【8】mmap实现父子进程的共享内存通信,用信号量同步
- Linux进程间通信源码剖析,共享内存(mmap)
- Linux进程间通信源码剖析,共享内存(mmap)
- Linux进程间通信--mmap共享内存(一)
- Linux进程间通信--mmap()共享内存(二)
- linux进程通信(一)--共享内存--mmap()
- linux 中mmap的用法(进程间通信-共享内存)
- [转] linux下使用mmap实现进程间共享内存
- 进程通信-共享内存-mmap()-code1
- 进程通信-共享内存-mmap()-code2
- 进程通信-共享内存-mmap()-code3
- 进程间通信之mmap共享内存
- Linux\Unix IPC进程通信实例分析(一):共享内存通信---文件映射mmap方式
- 架构设计三部曲之如何评审架构设计说明书
- hdu1140 war on weather
- 学业众筹,为着闪亮的日子
- Java的多线程之同步篇二:synchronized关键字
- docker 学习笔记 -----如何批量删除的已经处于停止状态的容器
- Linux进程共享通信 -- mmap实现
- BZOJ1117 POI2009 救火站
- <LeetCode OJ> 290. Word Pattern
- Linux服务管理命令
- C语言连接mysql数据库查询中文的数据表乱码问题
- Volley get请求上传中文参数,后台接收到的是乱码的解决办法
- 1. 判断用户输入是否符合预期的循环
- Java学习笔记5-泛型
- Qt多线程事件管理的正确打开方式