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的更多相关文章

  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. 微信公众号ios10.1 版本白屏问题

    真机调试IOS 10.1.x的版本不支持fetch 所以一直loading显示白屏. 其他设备都没问题. 所以用主要属性window.fetch用来判断是否支持fetch 属性 import { ba ...

  2. Cocos2d-x之Node

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 节点是cocosd-x游戏引擎中的重要元素,是其他重要游戏元素的基类,例如,场景Scene,图层Layer,菜单Menu和精灵sprite ...

  3. Java转型大数据开发全套教程,都在这儿!

    众所周知,很多语言技术已经在长久的历史发展中掩埋,这期间不同的程序员也走出的自己的发展道路. 有的去了解新的发展趋势的语言,了解新的技术,利用自己原先的思维顺利改变自己的title. 比如我自己,也都 ...

  4. hduoj-1301 Jungle Roads(最小生成树-克鲁斯卡尔和普里姆求解)

    普里姆求解: #include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...

  5. zabbix添加对web页面url的状态监控

    zabbix3.0.4添加对web页面url的状态监控 1.应用集配置 在配置—>主机中打开主机列表,选择需要添加监控主机的web,创建应用集 2.web监测配置 选择web场景,再单击右上角的 ...

  6. .Net Core身份认证:IdentityServer4实现OAuth 2.0 客户端模式

    一.客户端模式介绍 客户端模式(Client Credentials Grant)是指客户端直接向认证服务(Authorization Server)发送认证请求,获取token,进行认证,一般适用于 ...

  7. Spring Cloud服务保护

    微服务虽然解决了传统单体式应用各个模块之间强耦合的缺点,但同时也引出了新问题,由于微服务各个服务之间是独立部署的,并且一般情况下一个服务往往会依赖多个其他服务,并且服务之间的调用更多的是依赖不稳定的网 ...

  8. (好题)2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest F Pizza Delivery

    题意:给n个点m条边的有向图.每次使一条边反向,问你1到2的最短路变短,变长,还是不变. 解法:遇到这种题容易想到正向求一遍最短路d1,反向再求一遍最短路d2.纪录原图上的最短路为ans,然后分开考虑 ...

  9. redis配置篇

    配置 Redis的配置信息在/etc/redis/redis.conf下. 查看 sudo vi /etc/redis/redis.conf 核心配置选项 绑定ip:如果需要远程访问,可将此⾏注释,或 ...

  10. 小白的第一篇python博客

    学习python课程三天,先了解了计算机的历史及各种发展史,python的历史及部分语法,python主要学习内容有"hello world",int常量.str字符串,print ...