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). ...
随机推荐
- 对于页面上下载pdf或者excel按钮的实现
这个主要是通过 window.open(url + params) url后台给存放的路径,params是参数
- Least Common Ancestors
/* Least Common Ancestors * Au: Small_Ash */ #include <bits/stdc++.h> using namespace std; con ...
- flutter Could not find the built application bundle
报错信息Could not find the built application bundle at build/ios/iphoneos/Runner.app. Error launching ap ...
- jwt token校验获取用户数据
/** * 用户登录成功之后,获取用户信息 * 1.获取用户id * 2.根据id查询用户 * 3.构建返回值对象 * 4.返回数据 * @return */@RequestMapping(value ...
- dir(dict)|字典的创建-添加-删除-修改-判断存在-取值等相关操作
dir(dict) ####字典操作:创建-添加-删除-修改-判断存在-取值 #(一)创建字典: {} .等号. zip(). [(),()] #1.创建空字典 dict0 = {} #2.等号创建 ...
- Jackson教程
Jackson是一个简单基于Java应用库,Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象.Jackson所依赖的jar包较少,简 ...
- 使用font-size:0px 来制作跨浏览器的inline-block css属性
如果排列的元素是等高的话,相信很多人都会使用浮动来布局,不过如果元素不等高的话,不添加多余的标签,浮动布局是很难实现下图中的效果.如果遇到这种情况,可以使用inline-block来布局. 现代浏览器 ...
- Spring Cloud服务安全连接
Spring Cloud可以增加HTTP Basic认证来增加服务连接的安全性. 1.加入security启动器 在maven配置文件中加入Spring Boot的security启动器. <d ...
- 更换nginx默认端口以及配置文件位置
前言 近段时间在准备毕业设计的前期准备,基本确定了前后端分离的架构,于是就需要用到了nginx. 在之前nginx是放在docker上,所以没有端口更改跟配置文件配置的烦恼.但是现在是直接放在服务器上 ...
- KMP算法及实现
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #inclu ...