共享内存是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. Spring定时任务的几种实现(转自iteye网gong1208)

    原文地址: http://gong1208.iteye.com/blog/1773177 以下为正文: 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整 ...

  2. QUARTZ CRON

    本文来自:http://www.blogjava.net/crazycy/archive/2013/06/06/400287.html 每次使用Quartz Cron的时候都要去查manual doc ...

  3. 理解java中【同步】和【死锁】

    一.理解同步 要想解决资源共享的同步操作问题,可以使用两种方法: 使用同步代码块 之前学习过程中,代码块分为四种: l         普通代码块:是直接定义在方法之中的: l         构造块 ...

  4. stagefright框架(四)-Video Buffer传输流程

    這篇文章將介紹Stagefright中是如何和OMX video decoder传送buffer. (1) OMXCodec會在一開始的時候透過read函式來傳送未解碼的data給decoder,並且 ...

  5. PHP学习笔记五【方法】

    <?php $num1=34; $num2=90; $oper="+"; $res=0; switch($oper) { case "+": $res=$ ...

  6. Sql中的Exists和in

    最近学习数据库的分页算法,提到第一种 SELECT TOP 页大小 *FROM table1WHERE id NOT IN          (          SELECT TOP 页大小*(页数 ...

  7. tostring的用法

    ToString()可空参数单独使用,同时可以加一个格式化参数,具体方式如下: . 取中文日期显示_年月 currentTime.ToString("y"); 格式:2007年1月 ...

  8. 谈谈对web标准的理解

    Web标准不是某一个标准,而是由一系列标准组合而成.网页主要由三部分组成:结构.表现和行为.对应的标准也分三方面:结构化标准语言主要包括XHTML和HTML以及XML,表现标准语言主要包括CSS,行为 ...

  9. struts2 <s:iterator> 遍历方法

    1.MapAction.java import java.util.ArrayList;   import java.util.HashMap;   import java.util.List;    ...

  10. (转)发现两个有用的C函数_alloca()、_msize()

    转自: http://blog.csdn.net/pony12/article/details/8678071 (1)_alloca()alloca也是用来分配存储空间的,它和malloc的区别是它是 ...