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. ExpandoObject对象的JSON序列化

    如果: dynamic expando = new ExpandoObject(); d.SomeProp=SomeValueOrClass; 然后,我们在控制器中: return new JsonR ...

  2. 关于 as 播放器的记录

    一:文件结构 1:代码 2:编译后   二:IDE展示区 1处还有6个层,2处为代码和设计文件,3处是主类. 资源文件的位置如下:   三:数据交互 AS中代码: JS中代码: 更多需要注意的地方在这 ...

  3. 深入分析Java的编译原理

    在<Java代码的编译与反编译>中,有过关于Java语言的编译和反编译的介绍.我们可以通过javac命令将Java程序的源代码编译成Java字节码,即我们常说的class文件.这是我们通常 ...

  4. ORM(Object-Relational Mapping 对象关系映射)如何实现(转)

    原文链接:http://blog.163.com/hzd_love/blog/static/13199988120107891854473/ 1.什么是ORM ORM的全称是Object Relati ...

  5. c++流缓冲学习---rdbuf()

    我们使用STL编程的时候有时候会想到把一个流对象指向的内容用另一个流对象来输出,比如想把一个文件的内容输出到显示器上,我们可以用简单的两行代码就可以完成: ifstream infile(" ...

  6. Maximum Submatrix & Largest Rectangle

    相关题型 问题一(最大和子矩阵) : 有一个 m x n 的矩阵,矩阵的元素可正可负.请找出该矩阵的一个子矩阵(方块),使得其所有元素之和在所有子矩阵中最大.(问题来源:http://acm.pku. ...

  7. LeetCode295-Find Median from Data Stream && 480. 滑动窗口中位数

    中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个支持以下两种操 ...

  8. Binary Tree ZigZag Level Order Traversal leetcode java

    题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...

  9. 用jstl标签判断一个字符串是否包含了另一个字符串

    <c:if test="${fn:contains(str1,str2)}">

  10. 使用JDBC向Kudu表插入中文数据乱码(转载)

    参考:https://cloud.tencent.com/developer/article/1077763 问题描述 使用Impala JDBC向Kudu表中插入中文字符,插入的中文字符串乱码,中文 ...