共享内存是IPC的一种机制,允许两个不相关的进程共享同一块内存

//共享内存可以双向通信,但其本身没有相应机制,需要程序编写者设计,本例为单向通信(分为读端和写端)。

共享内存读端:

#include <stdio.h>

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>

//自定义数据结构,flag为同步机制标志:flag 为1表示读端可读, flag为1表示写端可写;str为数据存储的字符数组
struct my_shared
{
int flag;
char str[1024];
};

int main(void)
{
void *shared_memory = NULL;
struct my_shared *shared_buff = NULL;
int running = 1;
int shmid;

//创建共享内存标识
shmid = shmget((key_t)1234, sizeof(struct my_shared), 0666|IPC_CREAT);
if(shmid < 0)
{
perror("fail to shmget");
exit(1);
}

//连接共享内存到进程地址空间
shared_memory = shmat(shmid, NULL, 0);
if(shared_memory == NULL)
{
perror("fail to shmat");
exit(1);
}

shared_buff = (struct my_shared *)shared_memory;
shared_buff->flag = 0;

while(running)

{

//flag为1,读数据

if(shared_buff->flag)
{
printf("shared_memory message is: %s\n", shared_buff->str);
shared_buff->flag = 0;

if(strncmp(shared_buff->str, "end", 3) == 0)
{
running = 0;
}

}

}

//把共享从本进程空间分离
if(shmdt(shared_memory) == -1)
{
perror("fail to shmdt");
exit(1);
}

//删除共享内存
if(shmctl(shmid, IPC_RMID, 0) == -1)
{
perror("fail to shmctl");
exit(1);
}

return 0;

}

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>

struct my_shared
{
int flag;
char str[1024];
};

int main(void)
{
void *shared_memory = NULL;
struct my_shared *shared_buff = NULL;
int running = 1;
int shmid;
char buffer[512];

shmid = shmget((key_t)1234, sizeof(struct my_shared), 0666|IPC_CREAT);
if(shmid < 0)
{
perror("fail to shmget");
exit(1);
}

shared_memory = shmat(shmid, NULL, 0);
if(shared_memory == NULL)
{
perror("fail to shmat");
exit(1);
}

shared_buff = (struct my_shared *)shared_memory;
shared_buff->flag = 0;

while(running)
{

//flag为1,等等读端读数据
while(shared_buff->flag)
{
printf("wait for other process's reading\n");
sleep(2);
}

printf("Please input some data\n");
fgets(buffer, 512, stdin);

shared_buff->flag = 1;
strncpy(shared_buff->str, buffer, 1024);

if(strncmp(shared_buff->str, "end", 3) == 0)
{
running = 0;
}

}

if(shmdt(shared_memory) == -1)
{
perror("fail to shmdt");
exit(1);
}

return 0;

}

linux共享内存简析的更多相关文章

  1. Linux VFS机制简析(一)

    Linux VFS机制简析(一) 本文主要基于Linux内核文档,简单分析Linux VFS机制,以期对编写新的内核文件系统(通常是给分布式文件系统编写内核客户端)的场景有所帮助. 个人渊源 切入正文 ...

  2. Linux目录结构简析

    Linux目录结构简析 Linux继承了unix操作系统结构清晰的特点.在linux下的文件结构非常有条理.但是,上述的优点只有在对linux相当熟悉时,才能体会到.现在,虫虫就把linux下的目录结 ...

  3. Linux VFS机制简析(二)

    Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...

  4. Linux 程序设计1:深入浅出 Linux 共享内存

    笔者最近在阅读Aerospike 论文时,发现了Aerospike是利用了Linux 共享内存机制来实现的存储索引快速重建的.这种方式比传统利用索引文件进行快速重启的方式大大提高了效率.(减少了磁盘 ...

  5. linux 共享内存shm_open实现进程间大数据交互

    linux 共享内存shm_open实现进程间大数据交互 read.c #include <sys/types.h> #include <sys/stat.h> #includ ...

  6. linux 共享内存

    共享内存是最高效的IPC机制,因为它不涉及进程之间的任何数据传输.这种高效带来的问题是,我们必须用其他手段来同步进程对共享内存的访问,否则会产生竞态条件.所以,共享内存通常和其他进程间通信方式一起使用 ...

  7. Linux共享内存(二)

    Linux共享内存编程实例 原文链接:http://blog.csdn.net/pcliuguangtao/article/details/6526119 /*共享内存允许两个或多个进程进程共享同一块 ...

  8. 关于linux 共享内存查看已经完整释放

    完整删除共享内存脚本 #!/bin/sh function rmshm() { zero_status=`ipcs -m|awk '{print $6}'|grep -w 0|wc -l` if [ ...

  9. linux 共享内存实现

    说起共享内存,一般来说会让人想起下面一些方法:1.多线程.线程之间的内存都是共享的.更确切的说,属于同一进程的线程使用的是同一个地址空间,而不是在不同地址空间之间进行内存共享:2.父子进程间的内存共享 ...

随机推荐

  1. MyCat 介绍、分片规则、调优的内容收集

    一.MyCat的简介 MyCat高可用.负载均衡架构图: 详细知识点:  MySQL分布式集群之MyCAT(一)简介(修正) 二.MyCat的schema.xml讲解 详细知识点:MySQL分布式集群 ...

  2. 【转】android 电容屏(一):电容屏基本原理篇

    关键词:android  电容屏 tp  ITO 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV310(samsung exynos 42 ...

  3. 【转】C++实现RTMP协议发送H.264编码及AAC编码的音视频

    RTMP(Real Time Messaging Protocol)是专门用来传输音视频数据的流媒体协议,最初由Macromedia 公司创建,后来归Adobe公司所有,是一种私有协议,主要用来联系F ...

  4. Python 单词字母顺序不变且所有倒排

    翻出google測试project师的一道题目: 设计一个函数,不论什么语言都能够,实现下面功能: 一个句子,将句子中的单词所有倒排过来,但单词的字母顺序不变.eg.  this is a real ...

  5. start_kernel——local_irq_disable

    在启动初期须要关闭CPU的IRQ,原因: 因为尚未对中断代码.向量表,中断处理器进行初始化,所以必须关闭中断. 我的源码里面定义了 CONFIG_TRACE_IRQFLAGS_SUPPORT,所以调用 ...

  6. 在spring+hibernaet+mysql事务处理中遇到的一些坑

    spring的事务处理本来就是依赖于底层的实现,比如hibernate及数据库本身. 所以,当使用mysql数据库时,首先要确定的是,所操作的对象表是innodb格式的. 1. read-only方法 ...

  7. js执行环境深入研究

    js 声明函数是创建函数对象的过程,当创建函数对象时,函数对象的[[scope]] =连当前执行环境对象的作用域(栈顶执行环境--当执行函数时,js会将该函数的执行环境对象入栈) 当为全局函数时,如: ...

  8. 高效的SQL分页存储过程

    CREATE PROCEDURE SP_CommonPageList @Fields VARCHAR(500), @From VARCHAR(1000), @Condition VARCHAR(100 ...

  9. ThreadPoolExecutor(转)

    让ThreadPoolExecutor的workQueue占满时自动阻塞submit()方法 By learnhard | 2015 年 09 月 04 日 0 Comment 转载请注明出处:htt ...

  10. 理解Java的GC日志

    分析如下GC日志:[GC [PSYoungGen: 9216K->1024K(9216K)] 1246196K->1246220K(1287040K), 0.2398360 secs] [ ...