header for public argument:shmdata.h

  1. #define TEXT_SZ 2048
  2.  
  3. struct shared_use_st
  4.  
  5. {
  6.  
  7. int written;
  8.  
  9. char text[TEXT_SZ];
  10.  
  11. };
  12.  
  13. #endif

shmread.c

  1. #include <unistd.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <stdio.h>
  6.  
  7. #include <sys/shm.h>
  8.  
  9. #include "shmdata.h"
  10.  
  11. int main()
  12.  
  13. {
  14.  
  15. int running = ;
  16.  
  17. void *shm = NULL;
  18.  
  19. struct shared_use_st *shared;
  20.  
  21. int shmid;
  22.  
  23. shmid = shmget((key_t), sizeof(struct shared_use_st), |IPC_CREAT);
  24.  
  25. if(shmid == -)
  26.  
  27. {
  28.  
  29. fprintf(stderr, "shmget failed\n");
  30.  
  31. exit(EXIT_FAILURE);
  32.  
  33. }
  34.  
  35. shm = shmat(shmid, , );
  36.  
  37. if(shm == (void*)-)
  38.  
  39. {
  40.  
  41. fprintf(stderr, "shmat failed\n");
  42.  
  43. exit(EXIT_FAILURE);
  44.  
  45. }
  46.  
  47. shared = (struct shared_use_st*)shm;
  48.  
  49. shared->written = ;
  50.  
  51. while(running)
  52.  
  53. {
  54.  
  55. if(shared->written != )
  56.  
  57. {
  58.  
  59. printf("You wrote: %s", shared->text);
  60.  
  61. sleep(rand() % );
  62.  
  63. shared->written = ;
  64.  
  65. if(strncmp(shared->text, "end", ) == )
  66.  
  67. running = ;
  68.  
  69. }
  70.  
  71. else
  72.  
  73. sleep();
  74.  
  75. }
  76.  
  77. if(shmdt(shm) == -)
  78.  
  79. {
  80.  
  81. fprintf(stderr, "shmdt failed\n");
  82.  
  83. exit(EXIT_FAILURE);
  84.  
  85. }
  86.  
  87. if(shmctl(shmid, IPC_RMID, ) == -)
  88.  
  89. {
  90. fprintf(stderr, "shmctl(IPC_RMID) failed\n");
  91. exit(EXIT_FAILURE);
  92. }
  93. exit(EXIT_SUCCESS);
  94. }

shmwrite.c

  1. #include <unistd.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <stdio.h>
  6.  
  7. #include <string.h>
  8.  
  9. #include <sys/shm.h>
  10.  
  11. #include "shmdata.h"
  12.  
  13. int main()
  14.  
  15. {
  16.  
  17. int running = ;
  18.  
  19. void *shm = NULL;
  20.  
  21. struct shared_use_st *shared = NULL;
  22.  
  23. char buffer[BUFSIZ + ];
  24.  
  25. int shmid;
  26.  
  27. shmid = shmget((key_t), sizeof(struct shared_use_st), |IPC_CREAT);
  28.  
  29. if(shmid == -)
  30.  
  31. {
  32.  
  33. fprintf(stderr, "shmget failed\n");
  34.  
  35. exit(EXIT_FAILURE);
  36.  
  37. }
  38.  
  39. shm = shmat(shmid, (void*), );
  40.  
  41. if(shm == (void*)-)
  42.  
  43. {
  44.  
  45. fprintf(stderr, "shmat failed\n");
  46.  
  47. exit(EXIT_FAILURE);
  48.  
  49. }
  50.  
  51. shared = (struct shared_use_st*)shm;
  52.  
  53. while(running)
  54.  
  55.   {
  56.  
  57. while(shared->written == )
  58.  
  59. {
  60.  
  61. sleep();
  62.  
  63. printf("Waiting...\n");
  64.  
  65. }
  66.  
  67. printf("Enter some text: ");
  68.  
  69. fgets(buffer, BUFSIZ, stdin);
  70.  
  71. strncpy(shared->text, buffer, TEXT_SZ);
  72.  
  73. shared->written = ;
  74.  
  75. if(strncmp(buffer, "end", ) == )
  76.  
  77. running = ;
  78.  
  79. }
  80.  
  81. if(shmdt(shm) == -)
  82.  
  83. {
  84.  
  85. fprintf(stderr, "shmdt failed\n");
  86.  
  87. exit(EXIT_FAILURE);
  88.  
  89. }
  90.  
  91. sleep();
  92. exit(EXIT_SUCCESS);
  93. }

share memory的更多相关文章

  1. Share Memory By Communicating

    Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...

  2. Share Memory By Communicating 一等公民

    Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...

  3. share memory between guest and nic

    通过硬件的IOMMU,内核提供的共享内存.VFIO可以实现. REF: 1. offical DPDK API Doc, 简书有翻译版 DPDK编程指南(翻译)(一)  (二十七) 2. dpdk v ...

  4. share memory cache across multi web application

    Single instance of a MemoryCache across multiple application pools on the same server [duplicate] Yo ...

  5. How to share memory between services and user processes?

    除了必要的InitializeSecurityDescriptor和SetSecurityDescriptorDacl, 内存映射文件名必须GLOBAL开头.

  6. zabbix登陆问题:cannot allocate shared memory for collector

    问题说明:在一台zabbix被监控服务器上(64位centos6.8系统,64G内容)启动zabbix_agent,发现进程无法启动,10050端口没有起来! 启动zabbix_agent进程没有报错 ...

  7. System Services -> Memory Management -> About Memory Management

    Virtual Address Space Memory Pools Memory Performance Information Virtual Memory Functions Heap Func ...

  8. docker cgroup 技术之memory(首篇)

    测试环境centos7 ,内核版本4.20 内核使用cgroup对进程进行分组,并限制进程资源和对进程进行跟踪.内核通过名为cgroupfs类型的虚拟文件系统来提供cgroup功能接口.cgroup有 ...

  9. C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 VC中进程与进程之间共享内存 .net环境下跨进程、高频率读写数据 使用C#开发Android应用之WebApp 分布式事务之消息补偿解决方案

    C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing). ...

随机推荐

  1. thinkphp 根据文件后缀的不同返回不同的结果

    ** * 根据文件后缀的不同返回不同的结果 * @param string $str 需要判断的文件名或者文件的id * @return integer 1:图片 2:视频 3:压缩文件 4:文档 5 ...

  2. BZOJ 3569: DZY Loves Chinese II(线性基)

    传送门 解题思路 首先构造出一个生成树,考虑不连接的情况.假设连通两点的非树边和树边都断掉后不连通,那么可以给所有的非树边随机一个互不相同的值,然后树边的权值为过他两端点的非树边权值的异或和,这个可以 ...

  3. (转)Maven中的库(repository)详解 ---repository配置查找构件(如.jar)的远程库

    转:https://blog.csdn.net/taiyangdao/article/details/52287856 Maven中的库(repository)是构件(artifact)的集合.构件以 ...

  4. python主要探索函数

    在数据分析中,Python的主要探索函数 Python中主要用于书探索的是pandas(数据分析)和matplotlib(数据可视化).其中pandas提供了大量的数据探索的工具与数据相关的函数,这些 ...

  5. ML&MLDS笔记:偏差 vs 方差

    原文地址:https://www.jianshu.com/p/a02c6bd5d5e9 error来自哪?来自于偏差Bias和方差Variance. 就如打靶时瞄准一个点\(\overline{f}\ ...

  6. yum常见问题

    --> Finished Dependency Resolution Error: Multilib version problems found. This often means that ...

  7. JFreeChart教程

    图表是信息的图形表示.有可用的各种工具,它可用于创建不同类型的图表. 本教程学习什么是JFreeChart?为什么需要它,并在各种方式列出一个基于Java的应用程序或独立创建不同类型的图表. JFre ...

  8. Android Studio3.3中Cannot resolve symbol ActivityTestRule

    最近在看<Android编程权威指南>,在Windows10下安装了Android Studio3.3,边看编练习书中的例子程序,看到第21章""音频播放与单元测试&q ...

  9. Python面试题之下面代码会输出什么

    def f(x,l=[]): for i in range(x): l.append(i*i) print l f(2) f(3,[3,2,1]) f(3) 答案: [0, 1] [3, 2, 1, ...

  10. Arcpy里莫名其妙的字段类型(Field type)

    对比6个常用的字段数据类型在Arcpy字段创建与字段属性输出时奇怪的事情: 添加字段使用arcpy.AddField_management: # addfield 的 type参数 # 浮点型,Flo ...