使用mmap可以方便地添加共享内存
使用mmap添加的共享内存。
局限:
只能在有亲属关系的进程之间使用。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <wait.h>
//#include <sys/types.h>
//#include <sys/stat.h>
//#include <fcntl.h> #define MEMSIZE 1024 int main() {
char *str;
pid_t pid; str = (char *)mmap(NULL, MEMSIZE,
PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS,
-, ); if (str == MAP_FAILED) {
perror("mmap");
exit();
} pid = fork();
if (pid < ) {
perror("fork");
exit();
} if (pid == ) {
strncpy(str, "Hello!", );
munmap(str, MEMSIZE);
printf("child exit\n");
exit();
}
else {
wait(NULL);
puts(str);
munmap(str, MEMSIZE);
printf("father exit\n");
exit();
} }
运行结果:
[work@ mmap1]$g++ -o mmap_sharedm mmap_sharedm.cpp
[work@ mmap1]$./mmap_sharedm
child exit
Hello!
father exit
注意以上各个头文件的作用:
//#include <stdio.h>
mmap_sharedm.cpp:: error: `perror' was not declared in this scope
mmap_sharedm.cpp:: error: `perror' was not declared in this scope
mmap_sharedm.cpp:: error: `printf' was not declared in this scope
mmap_sharedm.cpp:: error: `puts' was not declared in this scope
mmap_sharedm.cpp:: error: `printf' was not declared in this scope //#include <stdlib.h>
mmap_sharedm.cpp:: error: `exit' was not declared in this scope
mmap_sharedm.cpp:: error: `exit' was not declared in this scope
mmap_sharedm.cpp:: error: `exit' was not declared in this scope
mmap_sharedm.cpp:: error: `exit' was not declared in this scope //#include <unistd.h>
mmap_sharedm.cpp:: error: `fork' was not declared in this scope //#include <sys/mman.h>
mmap_sharedm.cpp:: error: `PROT_READ' was not declared in this scope
mmap_sharedm.cpp:: error: `PROT_WRITE' was not declared in this scope
mmap_sharedm.cpp:: error: `MAP_SHARED' was not declared in this scope
mmap_sharedm.cpp:: error: `MAP_ANONYMOUS' was not declared in this scope
mmap_sharedm.cpp:: error: `mmap' was not declared in this scope
mmap_sharedm.cpp:: error: `MAP_FAILED' was not declared in this scope
mmap_sharedm.cpp:: error: `munmap' was not declared in this scope
mmap_sharedm.cpp:: error: `munmap' was not declared in this scope //#include <string.h>
mmap_sharedm.cpp:: error: `strncpy' was not declared in this scope //#include <wait.h>
mmap_sharedm.cpp:: error: no matching function for call to `wait::wait(NULL)'
/usr/include/bits/waitstatus.h:: note: candidates are: wait::wait()
/usr/include/bits/waitstatus.h:: note: wait::wait(const wait&)
使用mmap可以方便地添加共享内存的更多相关文章
- mmap映射区和shm共享内存的区别总结
[转载]原文链接:https://blog.csdn.net/hj605635529/article/details/73163513 linux中的两种共享内存.一种是我们的IPC通信System ...
- linux下共享内存mmap和DMA(直接访问内存)的使用 【转】
转自:http://blog.chinaunix.net/uid-7374279-id-4413316.html 介绍Linux内存管理和内存映射的奥秘.同时讲述设备驱动程序是如何使用“直接内存访问” ...
- 进程间通信之信号量、消息队列、共享内存(system v的shm和mmap)+信号signal
进程间通信方式有:System v unix提供3种进程间通信IPC:信号量.消息队列.共享内存.此外,传统方法:信号.管道.socket套接字. [注意上述6种方式只能用户层进程间通信.内核内部有类 ...
- mmap映射文件至内存( 实现 共享内存 与 文件的另类访问 )
Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数声明: 头文件: < ...
- Linux环境进程间通信(五): 共享内存(下)
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- Linux环境进程间通信(五): 共享内存(上)
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- UNIX网络编程 12 15共享内存区
管道,FIFO,消息队列,在两个进程交换信息时,都要经过内核传递 共享内存可以绕过,默认fork生成的子进程 并不与父进程共享内存区 mmap munmap msync 父子进程共享内存区的方法之一是 ...
- 【转】Linux环境进程间通信(五) 共享内存(上)
转自:https://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index1.html 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以 ...
- <转>Linux环境进程间通信(五): 共享内存(下)
http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index2.html 系统调用mmap()通过映射一个普通文件实现共享内存.系统V则是通 ...
随机推荐
- eclipse中的maven build、maven clean、maven install和maven test的区别
eclipse中的maven build.maven clean.maven install和maven test的区别 https://www.cnblogs.com/Marydon20170307 ...
- flask 中的request
request.args 从URL地址中的参数request.form POST请求时 从FormData中获取参数reque ...
- Android中,利用Intent传递对象值
在很多情况下,调用startActivity(Intent) 方法,跳转到另外一个Activity或其他component,需要传递一个对象给它. 可以让这个要传递的对象所属类实现Serializab ...
- 【转】Asp.net实现URL重写
[概述] URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程.重写URL是非常有用的一个功能,因为它可以让你提高搜索引擎阅读和索引你的网站的能力:而且在你改变了 ...
- BZOJ4042 : [Cerc2014] parades
设f[x]为x子树里能选的最多的路径数,h[x]为x子树里往上走的点的集合,且不与x子树内的最优解冲突 首先f[x]=sum(f[son]) 若h[son]与x可以直接匹配,则匹配上,f[x]++ 然 ...
- 基于Servlet+JSP+JavaBean开发模式的用户登录注册
http://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBea ...
- 解决 MyEclipse build workspace 慢,validation javascript 更慢的问题
自从升级了MyEclipse到7.0,项目Build的时候总是很慢,显示Validating 那些js,html文件.不管我怎么调整 Windows > Preference > MyEc ...
- LPC-LINK 2 LPC4370 简化线路图
- C#轻量级高性能日志组件EasyLogger
一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的第六部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理 ...
- DirectX全屏游戏中弹出窗口(转)
一直有人问如何在DirectX全屏游戏中弹出窗口就象金山游侠一样.我答应过要给出原码,只是一直没有时间整理,不过现在总算是弄玩了.代码不长,大致作了些注释,但愿你能看懂:)按照我的说明一步步作应该就能 ...