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). ...
随机推荐
- 关于Burp Suite不能抓包的解决方法
一.Burp Suite有时能抓到包,有时不能抓到包 解决方法: 出现这种问题的原因就是代理没有设置成全局的,只是设置成了局部的. 打开IE浏览器,依次打开工具->Internet 属性-> ...
- redis shell命令
APPEND key value追加一个值到key上 AUTH password验证服务器 BGREWRITEAOF异步重写追加文件 BGSAVE异步保存数据集到磁盘上 BLPOP key [key ...
- java.lang.IllegalAccessException: Class XXXcan not access xxx with modifiers "private"
field 或者 method 是 provate的 field.setAccessible(true); method.setAccessible(true); 有时候是因为 newinstance ...
- OAccflow集成sql
SELECT * FROM PORT_EMP WHERE NO='18336309966'SELECT * FROM PORT_DEPT WHERE no='42DBAF50712C4046B09BC ...
- 力扣算法——138CopyListWithRandomPointer【M】
A linked list is given such that each node contains an additional random pointer which could point t ...
- 力扣算法题—148sort-list
Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2-> ...
- windows配置环境变量
windows配置环境变量 1.第一步 2.第二步 3.第三步
- leetcode.矩阵.766托普里茨矩阵-Java
1. 具体题目 如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵.给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True. 示例 1: 输入: ma ...
- C# 网络编程 TcpListener
1.服务断代码 public partial class Server : Form { private bool lk = true; public Server() { InitializeCom ...
- matlab中struct创建方法
MATLAB中struct创建方法可分为:直接创建法和struct()函数创建法 (1)直接创建: 直接定义字段,像使用一般matlab变量一样,不需要事先声明,支持动态扩充.下面创建一个Studen ...