使用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. 社会地位即服务, Status as a Service (一): 社交网络是一种 ICO 行为?

    上周,看到 Eugene Wei 又发了一篇长文,Status as a Service (StaaS).状态即服务?服务器的状态吗?不知所言.抱着好奇,我打开了这篇文章,一看就是 3 个小时!

  2. http://blog.mn886.net/jqGrid/

    http://blog.mn886.net/jqGrid/  中文版 http://www.trirand.com/blog/jqgrid/jqgrid.html  英文版

  3. BZOJ1712 : [Usaco2007 China]Summing Sums 加密

    设$s[i]$为进行$i$次加密后所有奶牛数字的和,有$s[i]=(n-1)s[i-1]$. 设$c[i]$为某头固定的奶牛进行$i$次加密后的数字, 若$i$为奇数,有: \[c[i]=((1-n) ...

  4. ibatis的缓存机制

    Cache        在特定硬件基础上(同时假设系统不存在设计上的缺漏和糟糕低效的SQL 语句)Cache往往是提升系统性能的最关键因素).        相对Hibernate 等封装较为严密的 ...

  5. 通过webbrowser控件获取验证码

    1.首先介绍下基本控件(拖控件大家都会,我就不一一介绍了),看下图: 2.添加MSHTML引用,步骤如下: 解决方案—右键“引用”—​添加引用—在.NET下找到Microsoft.mshtml组件—点 ...

  6. 汽车之家店铺数据抓取 DotnetSpider实战

    一.背景 春节也不能闲着,一直想学一下爬虫怎么玩,网上搜了一大堆,大多都是Python的,大家也比较活跃,文章也比较多,找了一圈,发现园子里面有个大神开发了一个DotNetSpider的开源库,很值得 ...

  7. Windows Phone本地数据库(SQLCE):4、[Column]attribute(翻译) (转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第四篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  8. eclipse使用profile完成不同环境的maven打包功能

    原文:https://blog.csdn.net/duan9421/article/details/79086335 我们在日常开发工作中通常会根据不同的项目运行环境,添加不同的配置文件,例如 开发环 ...

  9. Maven编译时跳过Test

    在使用Maven编译项目时发现,可能在Test中写了一些有问题的代码,但是,由于写的代码比较多,所以不愿意去找具体的错误,反正Test中的代码不会影响项目的正常运行.于是想在编译时跳过对Test部分的 ...

  10. C#编程(二十九)----------泛型接口

    泛型接口 定义 先来看一个简单的例子: public class Sharp {} public class Rectangle:Sharp {} 上面定义了两个简单的类,一个是图形类,一个是矩形类; ...