内存共享允许两个或多个不相关的进程,访问同一个逻辑内存,共享内存的具体实现,由不同进程之间共享的内存安排为同一物理内存。

过个进程就像通过malloc获取的内存一样去使用,但是需要额外的小消息来同队内存的访问。

可以通过信号量,传递消息(消息队列),生成信号来同步对内存的访问。

1 shmget

#include

#include

int shmget(key_t key, size_t size, int shmflg);

shmget() returns the identifier of the System V shared memory segment associated with the value of the argument key. A new shared memory segment, with size equal to the value of size rounded up to a multiple of PAGE_SIZE, is created if key has the value IPC_PRI‐VATE or key isn’t IPC_PRIVATE, no shared memory segment corresponding to key exists, and IPC_CREAT is specified in shmflg.

2 shmat

把创建的内存地址空间连接到进程的地址空间。

#include

#include

void *shmat(int shmid, const void *shmaddr, int shmflg);

int shmdt(const void *shmaddr);

3 shmdt

#include

#include

void *shmat(int shmid, const void *shmaddr, int shmflg);

int shmdt(const void *shmaddr);

把共享的内存地址从进程中分离。

4 shmctl

控制共享内存

#include

#include

int shmctl(int shmid, int cmd, struct shmid_ds *buf);

控制共享内存;

5 使用共享内存

/* Our first program is a consumer. After the headers the shared memory segment

(the size of our shared memory structure) is created with a call to shmget,

with the IPC_CREAT bit specified. */

#include

#include

#include

#include

#include

#include "shm_com.h"

int main()

{

int running = 1;

void *shared_memory = (void *)0;

struct shared_use_st *shared_stuff;

int shmid;

srand((unsigned int)getpid());

shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);

if (shmid == -1) {

fprintf(stderr, "shmget failed\n");

exit(EXIT_FAILURE);

}

/* We now make the shared memory accessible to the program. */

shared_memory = shmat(shmid, (void *)0, 0);

if (shared_memory == (void *)-1) {

fprintf(stderr, "shmat failed\n");

exit(EXIT_FAILURE);

}

printf("Memory attached at %X\n", (int)shared_memory);

/* The next portion of the program assigns the shared_memory segment to shared_stuff,

which then prints out any text in written_by_you. The loop continues until end is found

in written_by_you. The call to sleep forces the consumer to sit in its critical section,

which makes the producer wait. */

shared_stuff = (struct shared_use_st *)shared_memory;

shared_stuff->written_by_you = 0;

while(running) {

if (shared_stuff->written_by_you) {

printf("You wrote: %s", shared_stuff->some_text);

sleep( rand() % 4 ); /* make the other process wait for us ! */

shared_stuff->written_by_you = 0;

if (strncmp(shared_stuff->some_text, "end", 3) == 0) {

running = 0;

}

}

}

/* Lastly, the shared memory is detached and then deleted. */

if (shmdt(shared_memory) == -1) {

fprintf(stderr, "shmdt failed\n");

exit(EXIT_FAILURE);

}

if (shmctl(shmid, IPC_RMID, 0) == -1) {

fprintf(stderr, "shmctl(IPC_RMID) failed\n");

exit(EXIT_FAILURE);

}

exit(EXIT_SUCCESS);

}

/* The second program is the producer and allows us to enter data for consumers. It's very similar to shm1.c and looks like this. */

#include

#include

#include

#include

#include

#include "shm_com.h"

int main()

{

int running = 1;

void *shared_memory = (void *)0;

struct shared_use_st *shared_stuff;

char buffer[BUFSIZ];

int shmid;

shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);

if (shmid == -1) {

fprintf(stderr, "shmget failed\n");

exit(EXIT_FAILURE);

}

shared_memory = shmat(shmid, (void *)0, 0);

if (shared_memory == (void *)-1) {

fprintf(stderr, "shmat failed\n");

exit(EXIT_FAILURE);

}

printf("Memory attached at %X\n", (int)shared_memory);

shared_stuff = (struct shared_use_st *)shared_memory;

while(running) {

while(shared_stuff->written_by_you == 1) {

sleep(1);

printf("waiting for client...\n");

}

printf("Enter some text: ");

fgets(buffer, BUFSIZ, stdin);

strncpy(shared_stuff->some_text, buffer, TEXT_SZ);

shared_stuff->written_by_you = 1;

if (strncmp(buffer, "end", 3) == 0) {

running = 0;

}

}

if (shmdt(shared_memory) == -1) {

fprintf(stderr, "shmdt failed\n");

exit(EXIT_FAILURE);

}

exit(EXIT_SUCCESS);

}

linux进程实现进程通信内存共享,Linux进程间通信 -3内存共享相关推荐

  1. linux 环境下的进程间的通信——消息队列传输结构体

    linux 环境下的进程间的通信方式主要有:管道,有名和无名管道, 这种方式适用于具有亲缘关系的进程之间的通信: 信号: 消息队列: 共享内存: 信号量: 套接字: 这次主要涉及消息队列: 1. 需要 ...

  2. linux 实验2 进程创建,实验2Linux进程控制与通信

    实验2Linux进程控制与通信 实验 2 Linux 进程控制与通信1. 实验目的(1 ) 进一步认识并发执行的概念,认识父子进程及进程创建原理:(2 ) 了解 Linux 系统中进程通信的基本原理. ...

  3. linux unix域socket_Socket通信原理

    对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问:1.         什么是TCP/IP.UDP?2.         Socke ...

  4. 操作系统——MFC实现进程创建和通信4

    引入 我接着上篇博客讲,如果没有构建项目的童鞋请移步到操作系统--MFC实现进程创建和通信1 用PostMessage实现通信请移步到操作系统--MFC实现进程创建和通信2 用CopyDATA消息实现 ...

  5. Linux进程间的通信----->共享内存

    共享内存:         顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式.不同进程之间共享的内存通常安排为同一段物 ...

  6. linux进程间的通信(C): 共享内存

    一.共享内存介绍 共享内存是三个IPC(Inter-Process Communication)机制中的一个. 它允许两个不相关的进程访问同一个逻辑内存. 共享内存是在两个正在进行的进程之间传递数据的 ...

  7. Linux访问其他进程空间,Linux环境进程间通信系列(五):共享内存

    共享内存可以说是最有用的进程间通信方式,也是最快的 IPC 形式.两个不同进程 A . B 共享内存的意思是,同一块物理内存被映射到进程 A . B 各自的进程地址空间.进程 A 可以即时看到进程 B ...

  8. linux共享内存示例,linux 进程间共享内存示例

    写入端: #include #include #include #include #include using namespace std; struct MappingDataType { int ...

  9. 【Linux 应用编程】进程管理 - 进程间通信IPC之共享内存 mmap

    IPC(InterProcess Communication,进程间通信)是进程中的重要概念.Linux 进程之间常用的通信方式有: 文件:简单,低效,需要代码控制同步 管道:使用简单,默认阻塞 匿名 ...

  10. 【Linux 内核】进程管理 task_struct 结构体 ⑤ ( files 字段 | nsproxy 字段 | 信号处理相关字段 | 信号量和共享内存相关字段 )

    文章目录 一.task_struct 结构体字段分析 1.files 字段 2.nsproxy 字段 3.信号处理相关字段 4.信号量和共享内存相关字段 在 Linux 内核 中 , " 进 ...

最新文章

  1. ASP.NET控件Repeater遍历
  2. CISCO ACL的匹配数问题
  3. ArcIMS体系结构
  4. 第一节:别出心裁的HTML5简介
  5. struts2中页面访问action的url问题,或许很简单
  6. 使用7zip把jre集成到绿色运行程序内
  7. java 485通讯_CAKJ-963U3-KT带485通讯上下限报警智能型仪表
  8. MSDN中文帮助文档
  9. 邻接矩阵实现(有向邻接矩阵)、(无向邻接矩阵) 基于C++
  10. android sdk环境变量配置
  11. PMP复习整理考点篇【3】--- 风险应对策略
  12. unity制作小地图
  13. 聊聊前端框架——尤雨溪
  14. 信息安全中常见的网络知识(一)网络基本概念
  15. 计算机专业的,颜值很高是一种怎样的赶脚?
  16. 【综述】3D智能数字化与3D打印:中国制造向中国智造转变的机遇
  17. MultiSim电路仿真之受控源的使用
  18. 【C语言】厘米换算英尺英寸
  19. 微信小程序云开发入门教程
  20. md5 php 加密后乱码_PHP中的密码加密的解决方案总结

热门文章

  1. kali linux 里vim如何使用_Linux vim基本的使用方法
  2. Spring Data说明
  3. mysql 转成树_Mysql树型结构2种方式及相互转换
  4. oracle 序列_Oracle好记性不如烂笔头序列及日期时间的插入
  5. python颜色识别原理_用opencv-python实现颜色检测
  6. 正则匹配不包含某字符串_如何替换JS字符串中匹配到多处中某一指定节点?
  7. Unity3d开发跳一跳-郑洪智-专题视频课程
  8. MySQL: InnoDB 还是 MyISAM?
  9. 使用哪种关机方式后再启动计算机时间最长,有没有哪一种软件可以使电脑在设置好时间后自动开关机...
  10. html 生成image java makenoise,[图形学] 柏林噪声 (perlin noise)