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. 关于MySQL的行转列的简单应用(二)---group函数

    MySQL的行转列.列转行.连接字符串  concat.concat_ws.group_concat函数用法使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一 ...

  2. Kafka深度解析(如何在producer中指定partition)(转)

    原文链接:Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅的消息系统.主要设计目标如下: 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能 ...

  3. BMap:JavaScript API

    ylbtech-Map-Baidu:JavaScript API JavaScript API百度地图JavaScript API是一套由JavaScript语言编写的应用程序接口,可帮助您在网站中构 ...

  4. 计算机中的概念: 视图 VS 镜像

    这两个概念还是不太一样的.下面来说说个人的理解,记录一下. 1. 镜像 镜像可以理解为一份完全一样的拷贝.也就是"深度拷贝",一个复制品. 比如 iso映像文件,ubuntu-12 ...

  5. asp.net 读取word 文档的方法

    资料一:适合读取并显示(简单而明了) 第一种方法:    Response.ClearContent(); Response.ClearHeaders();   Response.ContentTyp ...

  6. [leetcode]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

  7. javascript定义对象写法

    javascript定义对象的几种简单方法 1.构造函数方式,全部属性及对象的方法都放在构造方法里面定义 优点:动态的传递参数 缺点:每创建一个对象就会创建相同的方法函数对象,占用大量内存 funct ...

  8. NumPy与ndarray简介(转)

    http://blog.csdn.net/u014374284/article/details/45420645 一.NumPy简介 NumPy的全名为Numeric Python,是一个开源的Pyt ...

  9. php manager

    https://phpmanager.codeplex.com/releases/view/69115

  10. CareerCup Facebook Total number of substring palindrome

    Write a function for retrieving the total number of substring palindromes.  For example the input is ...