使用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可以方便地添加共享内存的更多相关文章

  1. mmap映射区和shm共享内存的区别总结

    [转载]原文链接:https://blog.csdn.net/hj605635529/article/details/73163513 linux中的两种共享内存.一种是我们的IPC通信System ...

  2. linux下共享内存mmap和DMA(直接访问内存)的使用 【转】

    转自:http://blog.chinaunix.net/uid-7374279-id-4413316.html 介绍Linux内存管理和内存映射的奥秘.同时讲述设备驱动程序是如何使用“直接内存访问” ...

  3. 进程间通信之信号量、消息队列、共享内存(system v的shm和mmap)+信号signal

    进程间通信方式有:System v unix提供3种进程间通信IPC:信号量.消息队列.共享内存.此外,传统方法:信号.管道.socket套接字. [注意上述6种方式只能用户层进程间通信.内核内部有类 ...

  4. mmap映射文件至内存( 实现 共享内存 与 文件的另类访问 )

    Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数声明: 头文件: < ...

  5. Linux环境进程间通信(五): 共享内存(下)

    linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...

  6. Linux环境进程间通信(五): 共享内存(上)

    linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...

  7. UNIX网络编程 12 15共享内存区

    管道,FIFO,消息队列,在两个进程交换信息时,都要经过内核传递 共享内存可以绕过,默认fork生成的子进程 并不与父进程共享内存区 mmap munmap msync 父子进程共享内存区的方法之一是 ...

  8. 【转】Linux环境进程间通信(五) 共享内存(上)

    转自:https://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index1.html 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以 ...

  9. <转>Linux环境进程间通信(五): 共享内存(下)

    http://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index2.html 系统调用mmap()通过映射一个普通文件实现共享内存.系统V则是通 ...

随机推荐

  1. HashMap分析 + 哈希表

    http://www.cnblogs.com/hzmark/archive/2012/12/24/HashMap.html http://www.cnblogs.com/xqzt/archive/20 ...

  2. android studio 查看大纲

    就是 structure 面板 快捷键 Alt+7 === android studio 查看方法说明 点击菜单“View”-“Quick Documentation" 建议直接查看源代码文 ...

  3. android 横竖屏 切换

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 横竖屏切换 会重新凋用 activity的 生命周期,再次oncreat() 如果清单文 ...

  4. git 撤销本地修改

    git checkout file 例如:git checkout app/views/carts/_index_m.html.erb 可以先用 git status 查看差异 然后 git chec ...

  5. NOI 2002 荒岛野人

    人生第一次做NOI的题祭!!! 大概是NOI最简单的一道题 克里特岛以野人群居而著称.岛上有排列成环行的M个山洞.这些山洞顺时针编号为1,2,…,M.岛上住着N个野人,一开始依次住在山洞C1,C2,… ...

  6. [Java]进程与线程的区别(转)

    线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间; (2)资源拥有: ...

  7. Maven学习——修改Maven的本地仓库路径

    安装Maven后我们会在用户目录下发现.m2 文件夹.默认情况下,该文件夹下放置了Maven本地仓库.m2/repository.所有的Maven构件(artifact)都被存储到该仓库中,以方便重用 ...

  8. java之ibatis数据缓存

    使用IBatis作数据缓存 1.SqlMapConfig.xml中<settingscacheModelsEnabled="true" //设置为trueenhancemen ...

  9. js识别用户设备是移动端手机时跳转到手机网站

    一.识别到用户的设备是手机等移动端设备时跳转到移动端网站 var mobileAgent = new Array("iphone", "ipod", " ...

  10. [原创]WebScarab工具介绍

    [原创]WebScarab工具介绍 一 WebScarab介绍 WebScarab是一个用来分析使用HTTP和HTTPS协议的应用程序框架.其原理很简单,WebScarab可以记录它检测到的会话内容( ...