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

Linux进程共享通信 -- mmap实现的更多相关文章

  1. linux下共享内存mmap和DMA(直接访问内存)的使用 【转】

    转自:http://blog.chinaunix.net/uid-7374279-id-4413316.html 介绍Linux内存管理和内存映射的奥秘.同时讲述设备驱动程序是如何使用“直接内存访问” ...

  2. linux 进程间通信 共享内存 mmap

    共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进程A可以即时看到进程B对共享内存中数据的更新,反 ...

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

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

  4. linux进程通信之共享内存

    共享内存同意两个或多个进程共享一给定的存储区,由于数据不须要来回复制,所以是最快的一种进程间通信机制.共享内存能够通过mmap()映射普通文件(特殊情况下还能够採用匿名映射)机制实现,也能够通过系统V ...

  5. 撸代码--linux进程通信(基于共享内存)

    1.实现亲缘关系进程的通信,父写子读 思路分析:1)首先我们须要创建一个共享内存. 2)父子进程的创建要用到fork函数.fork函数创建后,两个进程分别独立的执行. 3)父进程完毕写的内容.同一时候 ...

  6. linux 进程通信之 共享内存

    共享内存是被多个进程共享的一部分物理内存.共享内存是进程间共享数据的一种最快的方法.一个进程向共享内存区域写入了数据,共享这个内存区域的全部进程就能够立马看到当中的内容. 关于共享内存使用的API k ...

  7. Linux进程通信学习总结

    http://blog.csdn.net/xiaoweibeibei/article/details/6552498 SYSV子系统的相关概念   引用标识符:引用标识符是一个整数,表示每一个SYSV ...

  8. Linux进程间的通信

    一.管道 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: A. 管道是半双工的,数据只能向一个方向流动: B. 需要双工通信时,需要建立起两个管道: C. 只能用于父子进程或者兄弟 ...

  9. linux进程的管道通信

    linux进程的管道通信 要求 编程实现进程的管道通信,掌握管道通信的同步和互斥机制. 相关函数 pipe管道 指用于连接一个读进程和一个写进程以实现他们之间通信的一个共享文件,又名pipe文件.向管 ...

随机推荐

  1. 制作高仿QQ的聊天系统(下)—— Adapter & Activity

    一.适配器 1.1 分页显示数据 因为聊天信息数目很多,所以adpter需要做分页处理,这里的分页处理是我自己实现的,如果有更好的办法欢迎在评论中告知.我们从友盟的反馈SDK中能得到聊天的list,我 ...

  2. GridView和SimpleAdapter实现网格布局

    android:horizontalSpacing 元素之间的水平间距 android:verticalSpacing     元素之间的垂直间距 android:numColumns         ...

  3. [Web 前端] 使用yarn代替npm作为node.js的模块管理器

    cp from : https://www.jianshu.com/p/bfe96f89da0e     Fast, reliable, and secure dependency managemen ...

  4. Pandas快速入门(一)

    快速使用 bogon:Documents rousseau$ ipython --pylab Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23 ...

  5. [转]RSYNC 参数中文详解

    FROM : http://www.qiansw.com/rsync-cn.html rsync是一款好用的*nux文件同步工具.下面是其参数的中文解释. Rsync 参数选项说明 -v, --ver ...

  6. smb与samba

    为了使Windows 主机间的资源能够共享,微软于1980年开发了SMB(Server Message Block)通信协议,并通过SMB通信协议,使网络上各台主机之间能够共享文件.打印机等资源.目前 ...

  7. idea自动生成serialVersionUID , serialVersionUID的作用

    Java的序列化的机制通过判断serialVersionUID来验证版本的一致性.在反序列化的时候与本地的类的serialVersionUID进行比较,一致则可以进行反序列化,不一致则会抛出异常Inv ...

  8. DevExpress ChartControl控件实现图表【转】

    1.饼状图图 1.1添加ChartControl控件 在工具箱中找到ChartControl控件,拖到窗口中,创建Pie: 1.2准备数据 private DataTable CreateChartD ...

  9. Android Eclipseproject开发中的常见调试问题(二)android.os.NetworkOnMainThreadException 异常的解决的方法

    android.os.NetworkOnMainThreadException 异常的解决的方法. 刚开是把HttpURLConnectionnection 打开连接这种方法放在UI线程里了,可能不是 ...

  10. IDEA初使用:解决搜狗输入法不跟随BUG

    IDEA初使用:解决搜狗输入法不跟随BUG https://blog.csdn.net/qq_27905183/article/details/79119237 嗯,学习了: 然而,效果不明显: