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. 生成函数小结——[ EGF ][ ln 的一个套路 ][ 概率生成函数 ]

    看了jcvb的WC2015交流课件.虽然没懂后面的复合逆部分,但生成函数感觉受益良多. 指数生成函数 集合中大小为 i 的对象的权值是 \( a_i \) ,该集合的生成函数是 \( \sum\lim ...

  2. centos安装virtualbox

    参考:http://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-r ...

  3. Java + selenium 启动谷歌浏览器

    在之前创建的test项目下建一个test包,然后在包下建一个Chrome.java类.写入如下代码 package test; import java.util.concurrent.TimeUnit ...

  4. CentOS 7 64位虚拟机安装过程

    第一步:新建一个虚拟机,选择典型安装,点击下一步.

  5. 第一节:mybatis入门

    1.新建数据表 本次测试使用mysql数据,数据库名称为mybatis,新建一张表person,建表语句如下: CREATE TABLE `person` ( `id` ) PRIMARY KEY a ...

  6. fdisk分区实例

    查看磁盘分区详情 直接使用fdisk -l 或者使用fdisk /dev/sda,再使用p命令来查看 Command (m for help): p Disk /dev/sda: 32.2 GB, 3 ...

  7. 解决Redhat yum出现This system is not registered with RHN的方案

    最近博主在学习Linux,菜鸟级别的的选手连装个Chrome都觉得难,悲了个催的……百度了很多教程,大多是类似的.博主的配置是在VM8下搭建的RHEL5.3 (Tikanga)版本,不知道什么原因,每 ...

  8. Pytest -断言、跳过及运行

    基本断言方法: Pytest框架assert断言使用 • 断言:支持显示最常见的子表达式的值,包括调用,属性,比较以及二元和一元运算 符. • 包含,相等,不等,大于 小于运算,assertnot 假 ...

  9. JS提示信息来检测相应id的标签

    2015-07~2015-08 (其中$为document.getElementById()) 使用span提示信息来检测相应id的标签,没有返回值 infoTips("LRYH" ...

  10. php常见的验证方法

    php常见的验证方法 干货文章 ·2018-03-16 23:50:36 <?php /** * @param $id * @return false|int * 检测id */ functio ...