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). ...
随机推荐
- Centos6.6部署Redis集群
Centos6.6部署Redis集群 1环境准备 1环境安装redis 1安装ruby 2配置redis主从环境 3部署redis sentinel服务器 5集群使用 13当前集群环境说明 13测试功 ...
- OpenGL 学习总结
最终呈现画出三角形的一个方式: public void draw(float[] mvpMatrix) { // Add program to OpenGL ES environment GLES20 ...
- [STemWin教程入门篇] 第一期:emWin介绍
转自:http://bbs.armfly.com/read.php?tid=1544 SEGGER公司介绍 了解emWin之前,先了解一下SEGGER这家公司,了解生产商才能对emWin有更加全面的认 ...
- (59)C# IIS Express 设置
支持非localhost访问 本地测试时 修改 localhost改成127.0.0.1
- thymeleaf时间戳转换
<span th:text="${#dates.format(curDate, 'yyyy-MM-dd HH:mm:ss')}"></span> <t ...
- (转)Jupyter默认目录和默认浏览器修改
目录 1.总结:修改Anaconda中的Jupyter Notebook默认工作路径的三种方式 # 2.Jupyter默认目录和默认浏览器修改 1.总结:修改Anaconda中的Jupyter Not ...
- 【lua学习笔记】——在sublime中配置Lua运行环境
一.让Sublime可以运行lua脚本 打开sublime 选择tools-->Build System-->New Build System 在新出现的文件中输入如下内容: { &q ...
- 动态规划及LCS
LCS的python实现: #!/usr/bin/env python #-*- coding: utf-8 -*- import sys reload(sys) sys.setdefaultenco ...
- layer通过父页面调用子页面的方法及属性
引言 在使用layer.js的过程中,需要通过layer.open()以iframe的形式打开特定的页面,同时需要用layer的按钮对打开的页面进行提交及重置操作,但是苦于不知如何在父页面调用子页面的 ...
- 9-vim-移动命令-04-利用标记返回之前小编辑的代码位置
标记 在开发时,某一块代码可能需要处理,例如编辑或重看. 此时使用命令模式(普通模式)下使用m增加一个标记,这样可以在需要时快速地跳回来或者执行其他编辑操作. 标记名称可以是a~z或者A~Z之间的 ...