使用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则是通 ...
随机推荐
- hdu 4407 容斥原理
题意: 1 //一组数据 3 3 //数字为1-3,3次运算 2 2 3 //将2号位变成3 1 1 3 4 //计算1-3号位上与4互质的数的和 1 2 3 6 好题,需要重复练习 #include ...
- Java多线程runnable
主要为大家分享Java多线程怎么实现Runnable方式 一 :主要步骤 1.定义实现Runnable接口 2.覆盖Runnable接口中run方法,将线程要运行的代码存在run方法里 3.用Thre ...
- 第一次使用autohotkey的记录
第一次使用autohotkey的记录 原来想着直接用python来做模拟输入的,后面查了一下发现,目前的封装的库不一定能支持输入到游戏里,是的,我是打算用来做游戏辅助的,嘿嘿嘿 暂时来讲,我只是看完了 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划
C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...
- oracle 树型结构数据的查询
Oracle中start by prior子句用法 connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start with 条件1 con ...
- delphi 消息的使用
//分析结果 WM_AnalysisResult = WM_USER + 1009; SendMessage(G_MainHandle, WM_AnalysisResult, 0, 0); proce ...
- On premise TFS Git OAuth clone failed on agent
利用TFS2015的跨平台生成代理,在mac osx 上生成xamarin.ios项目,agent费劲九牛二虎之力搞定了(参见http://www.cnblogs.com/zjoch/p/581101 ...
- [经使用有效]Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法
sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法 最近几天从网上找了几个asp.net的登录案例想要研究研究代码,结果在用 Sql Server2005附 ...
- What is the largest TCP/IP network port number allowable for IPv4
69 down vote The largest port number is an unsigned short 2^16-1: 65535 A registered port is one ass ...
- 在ASP.NET MVC中实现一种不同于平常的三级联动、级联方式, 可用于城市、车型选择等多层级联场景
三级或多级联动的场景经常会碰到,比如省.市.区,比如品牌.车系.车型,比如类别的多级联动......我们首先想到的是用三个select来展示,这是最通常的做法.但在另外一些场景中,比如确定搜索条件的时 ...