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文件.向管 ...
随机推荐
- Android工具类 DateUtil,可以用它方便的进行日期的操作
本文转载自:http://blog.csdn.net/xuduzhoud/article/details/27526177 全部代码如下: DateUtil.java package com.exam ...
- RFC 868 -- TIME Protocol
INTERNET STANDARD Errata Exist Network Working Group J. Postel - ISI Request for Comments: 868 K. Ha ...
- #incldue<cctype>函数系列
#include <cctype>的函数 c++中应该是#include <cctype> c中应该是#include <ctype.h> 以下为字符函数库中常用的 ...
- nodejs中thiskeyword的问题
再分析详细内容之前,必需要好好阅读下面下面两篇blog 学习Javascript闭包(closure) Javascript的this使用方法 这两篇文章是阮一峰老师对Javascript的闭包和th ...
- maven与jdk版本不一致报:Unsupported major.minor version 51.0
I recently uninstalled Java 8, to use Java 6 as I want my code/creations to be usable by more people ...
- Guava之ImmutableMap使用示例
ImmutableMap 的作用就是:可以让java代码也能够创建一个对象常量映射,来保存一些常量映射的键值对. 分析以下情景,来具体讨论这个的好处. 假设现在有需求如下:根据数据库存的某个key字段 ...
- 数学图形(1.47)贝塞尔(Bézier)曲线
贝塞尔曲线又称贝兹曲线或贝济埃曲线,是由法国数学家Pierre Bézier所发现,由此为计算机矢量图形学奠定了基础.它的主要意义在于无论是直线或曲线都能在数学上予以描述. 上一节讲的是高次方程曲线, ...
- C#高级编程六十六天----表达式树总结【转】
https://blog.csdn.net/shanyongxu/article/details/47257139 表达式树总结 基础 表达式树提供了一个将可执行代码转换成数据的方法.如果你要在执行代 ...
- python3 map,filter和列表推导式
num_list = [11,2,-33,10,7,3,5,43] 1.filter 函数 获取num_list大于5的元素,并返回列表 用lambda表达式实现: # 在python2 中 fil ...
- Laravel 5 中使用 JWT(Json Web Token) 实现基于API的用户认证
在JavaScript前端技术大行其道的今天,我们通常只需在后台构建API提供给前端调用,并且后端仅仅设计为给前端移动App调用.用户认证是Web应用的重要组成部分,基于API的用户认证有两个最佳解决 ...