Linux进程共享通信 -- mmap实现
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
Linux进程共享通信 -- mmap实现的更多相关文章
- linux下共享内存mmap和DMA(直接访问内存)的使用 【转】
转自:http://blog.chinaunix.net/uid-7374279-id-4413316.html 介绍Linux内存管理和内存映射的奥秘.同时讲述设备驱动程序是如何使用“直接内存访问” ...
- linux 进程间通信 共享内存 mmap
共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进程A可以即时看到进程B对共享内存中数据的更新,反 ...
- 【Linux 应用编程】进程管理 - 进程间通信IPC之共享内存 mmap
IPC(InterProcess Communication,进程间通信)是进程中的重要概念.Linux 进程之间常用的通信方式有: 文件:简单,低效,需要代码控制同步 管道:使用简单,默认阻塞 匿名 ...
- linux进程通信之共享内存
共享内存同意两个或多个进程共享一给定的存储区,由于数据不须要来回复制,所以是最快的一种进程间通信机制.共享内存能够通过mmap()映射普通文件(特殊情况下还能够採用匿名映射)机制实现,也能够通过系统V ...
- 撸代码--linux进程通信(基于共享内存)
1.实现亲缘关系进程的通信,父写子读 思路分析:1)首先我们须要创建一个共享内存. 2)父子进程的创建要用到fork函数.fork函数创建后,两个进程分别独立的执行. 3)父进程完毕写的内容.同一时候 ...
- linux 进程通信之 共享内存
共享内存是被多个进程共享的一部分物理内存.共享内存是进程间共享数据的一种最快的方法.一个进程向共享内存区域写入了数据,共享这个内存区域的全部进程就能够立马看到当中的内容. 关于共享内存使用的API k ...
- Linux进程通信学习总结
http://blog.csdn.net/xiaoweibeibei/article/details/6552498 SYSV子系统的相关概念 引用标识符:引用标识符是一个整数,表示每一个SYSV ...
- Linux进程间的通信
一.管道 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: A. 管道是半双工的,数据只能向一个方向流动: B. 需要双工通信时,需要建立起两个管道: C. 只能用于父子进程或者兄弟 ...
- linux进程的管道通信
linux进程的管道通信 要求 编程实现进程的管道通信,掌握管道通信的同步和互斥机制. 相关函数 pipe管道 指用于连接一个读进程和一个写进程以实现他们之间通信的一个共享文件,又名pipe文件.向管 ...
随机推荐
- 【周年庆】china-pub 14周年庆感恩回馈四波狂热来袭
活动主题:china-pub 14周年庆感恩回馈四波狂热来袭活动说明:[第1波]电子书免费抢!10万好书 65折封顶! 活动规则: 1.活动期间内凡 ...
- Datagridview 在基于文本的单元格中启用换行,自动调整行高列宽
将 DataGridViewCellStyle的 WrapMode 属性设置为 DataGridViewTriState 枚举值之一.下面的代码示例使用 System.Windows.Forms.Da ...
- yolo源码解析(一)
原文:https://www.cnblogs.com/zyly/p/9534063.html yolo源码来源于网址:https://github.com/hizhangp/yolo_tensorfl ...
- HTTP和HTTPS的区别(转)
原文链接:HTTP和HTTPS的区别 HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议 它是一个安全通信通道,它基于HTTP开发,用于在客户计算机和 ...
- maven单测生成覆盖率报告---Jacoco的使用
JaCoCo介绍 一.JaCoCo简述 JaCoCo是一个开源的覆盖率工具,它针对的开发语言是java,其使用方法很灵活,可以嵌入到Ant.Maven中:可以作为Eclipse插件,可以使用其Java ...
- spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
转自:https://hbiao68.iteye.com/blog/2031006 1.Spring的框架中,org.springframework.beans.factory.config.Prop ...
- iOS开发文件夹--Copy items if needed
蓝色文件夹 蓝色文件夹(folder)一般作为资源文件夹使用,与黄色文件夹的主要区别是不参与编译,所以说如果你在这些文件夹下编写的逻辑代码是不参与编译的,其他文件也不能直接引用它们,若引用其中文件需要 ...
- windows取证
工具网站 : http://www.ntsecurity.nu/toolbox/ 命令行历史 :命令行模式 CMD 中使 doskey /history 命令可以显示前面输入的命令情况(例如使用 cl ...
- iOS开发-UITextField手机号和邮箱验证
不管是网页是手机,用户注册登录的时候绝大数时候都需要手机号码和邮箱地址,而且有些App会限制只能使用手机号注册,iOS方面邮箱正则比较简单,不过手机号码验证找了一下网上的,发现三大运营商的号码段有所变 ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...