share memory
header for public argument:shmdata.h
#define TEXT_SZ 2048
struct shared_use_st
{
int written;
char text[TEXT_SZ];
};
#endif
shmread.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include "shmdata.h"
int main()
{
int running = ;
void *shm = NULL;
struct shared_use_st *shared;
int shmid;
shmid = shmget((key_t), sizeof(struct shared_use_st), |IPC_CREAT);
if(shmid == -)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shm = shmat(shmid, , );
if(shm == (void*)-)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
shared = (struct shared_use_st*)shm;
shared->written = ;
while(running)
{
if(shared->written != )
{
printf("You wrote: %s", shared->text);
sleep(rand() % );
shared->written = ;
if(strncmp(shared->text, "end", ) == )
running = ;
}
else
sleep();
}
if(shmdt(shm) == -)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if(shmctl(shmid, IPC_RMID, ) == -)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
shmwrite.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shmdata.h"
int main()
{
int running = ;
void *shm = NULL;
struct shared_use_st *shared = NULL;
char buffer[BUFSIZ + ];
int shmid;
shmid = shmget((key_t), sizeof(struct shared_use_st), |IPC_CREAT);
if(shmid == -)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shm = shmat(shmid, (void*), );
if(shm == (void*)-)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
shared = (struct shared_use_st*)shm;
while(running)
{
while(shared->written == )
{
sleep();
printf("Waiting...\n");
}
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared->text, buffer, TEXT_SZ);
shared->written = ;
if(strncmp(buffer, "end", ) == )
running = ;
}
if(shmdt(shm) == -)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
sleep();
exit(EXIT_SUCCESS);
}
share memory的更多相关文章
- Share Memory By Communicating
Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...
- Share Memory By Communicating 一等公民
Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...
- share memory between guest and nic
通过硬件的IOMMU,内核提供的共享内存.VFIO可以实现. REF: 1. offical DPDK API Doc, 简书有翻译版 DPDK编程指南(翻译)(一) (二十七) 2. dpdk v ...
- share memory cache across multi web application
Single instance of a MemoryCache across multiple application pools on the same server [duplicate] Yo ...
- How to share memory between services and user processes?
除了必要的InitializeSecurityDescriptor和SetSecurityDescriptorDacl, 内存映射文件名必须GLOBAL开头.
- zabbix登陆问题:cannot allocate shared memory for collector
问题说明:在一台zabbix被监控服务器上(64位centos6.8系统,64G内容)启动zabbix_agent,发现进程无法启动,10050端口没有起来! 启动zabbix_agent进程没有报错 ...
- System Services -> Memory Management -> About Memory Management
Virtual Address Space Memory Pools Memory Performance Information Virtual Memory Functions Heap Func ...
- docker cgroup 技术之memory(首篇)
测试环境centos7 ,内核版本4.20 内核使用cgroup对进程进行分组,并限制进程资源和对进程进行跟踪.内核通过名为cgroupfs类型的虚拟文件系统来提供cgroup功能接口.cgroup有 ...
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 VC中进程与进程之间共享内存 .net环境下跨进程、高频率读写数据 使用C#开发Android应用之WebApp 分布式事务之消息补偿解决方案
C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing). ...
随机推荐
- NOIWC2019 懵逼记
弱省蒟蒻,第一次也是最后一次来冬令营.. Day [-inf,-1] \(woc\)咋啥都听不懂,没错在下划水王. Day 0 白天上课继续划水..晚上跑去试机,骗了半天交互的分就滚了..半夜里竟然睡 ...
- js千位符 | js 千位分隔符 | js 金额格式化
js 千位分隔符 千位分隔符,其实就是数字中的逗号.依西方的习惯,人们在数字中加进一个符号,以免因数字位数太多而难以看出它的值.所以人们在数字中,每隔三位数加进一个逗号,也就是千位分隔符,以便更加容易 ...
- Linux命令 uname
1.简介 管理系统而使用的命令,用于显示系统信息(不同linux版本可能有写差异) 2.语法 uname [-amnrsv] (1) -a,--all 显示所有的信息 (2) -s,--kernel- ...
- Tomcat负载均衡、调优核心应用进阶学习笔记(一):tomcat文件目录、页面、架构组件详解、tomcat运行方式、组件介绍、tomcat管理
文章目录 tomcat文件目录 bin conf lib logs temp webapps work 页面 架构组件详解 tomcat运行方式 组件介绍 tomcat管理 tomcat文件目录 ➜ ...
- MHA+atlas(数据库的高可用与读写分离)
学习完了mycat的高可用还是复习一下MHA+atlas吧,个人感觉还是比mycat好用,毕竟MHA有数据补全和切换主从的机制 1 MHA是什么? MHA(Master High Availabili ...
- Java构造函数(构造器)
构造函数是用于在对象创建后立即初始化对象的代码块.构造函数的结构看起来类似于一个方法. 声明构造函数 构造函数声明的一般语法是: 1 2 3 <Modifiers> <Constru ...
- 在webpack4 中利用Babel 7取消严格模式方法
报错信息: Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on stri ...
- LVM-扩容目录
LVM LVM是一种源自Unix环境,在Linux上广泛应用的逻辑虚拟盘存储方案.借助LVM,可以在保证各个Linux目录分区稳定,又可以实现各目录存储资源灵活分配. 本文主要系统介绍Linux环境下 ...
- [JXOI2017]数列
题目 一个不太一样的做法 当\(A_{i-1}=x\),称\(A_1\)到\(A_{i-2}\)中大于等于\(A_{i-1}\)的最小值\(R\)为上界,\(A_1\)到\(A_{i-2}\)中小于等 ...
- 4、服务注册&服务提供者
1.什么是服务提供者 服务提供者(Service Provider):是指服务的被调用方(即:为其它服务提供服务的服务):服务提供者,作为一个Eureka Client,向Eureka Server做 ...