linux共享内存简析
共享内存是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共享内存简析的更多相关文章
- Linux VFS机制简析(一)
Linux VFS机制简析(一) 本文主要基于Linux内核文档,简单分析Linux VFS机制,以期对编写新的内核文件系统(通常是给分布式文件系统编写内核客户端)的场景有所帮助. 个人渊源 切入正文 ...
- Linux目录结构简析
Linux目录结构简析 Linux继承了unix操作系统结构清晰的特点.在linux下的文件结构非常有条理.但是,上述的优点只有在对linux相当熟悉时,才能体会到.现在,虫虫就把linux下的目录结 ...
- Linux VFS机制简析(二)
Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...
- Linux 程序设计1:深入浅出 Linux 共享内存
笔者最近在阅读Aerospike 论文时,发现了Aerospike是利用了Linux 共享内存机制来实现的存储索引快速重建的.这种方式比传统利用索引文件进行快速重启的方式大大提高了效率.(减少了磁盘 ...
- linux 共享内存shm_open实现进程间大数据交互
linux 共享内存shm_open实现进程间大数据交互 read.c #include <sys/types.h> #include <sys/stat.h> #includ ...
- linux 共享内存
共享内存是最高效的IPC机制,因为它不涉及进程之间的任何数据传输.这种高效带来的问题是,我们必须用其他手段来同步进程对共享内存的访问,否则会产生竞态条件.所以,共享内存通常和其他进程间通信方式一起使用 ...
- Linux共享内存(二)
Linux共享内存编程实例 原文链接:http://blog.csdn.net/pcliuguangtao/article/details/6526119 /*共享内存允许两个或多个进程进程共享同一块 ...
- 关于linux 共享内存查看已经完整释放
完整删除共享内存脚本 #!/bin/sh function rmshm() { zero_status=`ipcs -m|awk '{print $6}'|grep -w 0|wc -l` if [ ...
- linux 共享内存实现
说起共享内存,一般来说会让人想起下面一些方法:1.多线程.线程之间的内存都是共享的.更确切的说,属于同一进程的线程使用的是同一个地址空间,而不是在不同地址空间之间进行内存共享:2.父子进程间的内存共享 ...
随机推荐
- Python IDE的选择和安装
安装好Python后我们需要选择合适自己的IDE进行学习,虽然利用python默认的编辑器,或者直接文档编辑也可以进行基础的学习,但总归不是太方便,能够开发python项目的IDE很多,如sublim ...
- html5 laboratory - drawing in the canvas
html5 laboratory - drawing in the canvas Creating a bar chart with canvas 21st February 2010 The exp ...
- FP-Growth算法之频繁项集的挖掘(python)
前言: 关于 FP-Growth 算法介绍请见:FP-Growth算法的介绍. 本文主要介绍从 FP-tree 中提取频繁项集的算法.关于伪代码请查看上面的文章. FP-tree 的构造请见:FP-G ...
- 多点触控插件Hammer.js
插件描述:Hammer.js是一个开源的,轻量级的javascript库,它可以在不需要依赖其他东西的情况下识别触摸,鼠标事件. 使用方法: <script src=<span class ...
- index < m_IntCount错误
在inspector中,重新添加动画的过度条件即可. 参考
- App Store不能下载一直等待中的两种解决办法
1,重启手机,之后确认是否得到改善 2,重启不行,更改WiFi的dns为114.114.114.114或者223.5.5.5 或 223.6.6.6,再重启手机 ps:我是第二种方法
- Android图片编译报错
一. AAPT err(1118615418): ERROR: 9-patch image icon_item_bottom_line.9.png malformed No marked region ...
- UILabel + 导入字体
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)]; 1.设置文字颜色 label.textC ...
- canvas入门
<html> <head> <script> window.onload=function(){ var canvas=document.getElementByI ...
- HDU 1070 - Milk
给每种牛奶价格和量 要求买最便宜的牛奶 #include <iostream> using namespace std; int t,n; ][]; ],v[]; int main() { ...