Chinaunix首页 | 论坛 | 博客
  • 博客访问: 844537
  • 博文数量: 489
  • 博客积分: 475
  • 博客等级: 下士
  • 技术积分: 3087
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 16:28
文章分类

全部博文(489)

文章存档

2013年(7)

2012年(301)

2011年(181)

分类:

2011-12-22 20:51:23

原文地址:共享内存 作者:luozhiyong131

shm_com.h

 

#define TEXT_SZ 2048

 

struct shared_use_st

{

    int written_by_you;

      char some_text[TEXT_SZ];

};

 

 

#include

#include

#include

#include

#include

#include

#include

#include "shm_com.h"

 

int main(void)

{

    int running=1;

      void *shared_memory=(void *)0;

      struct shared_use_st *shared_stuff;

      char buffer[BUFSIZ];

      int shmid;

 

      /*创建共享内存*/

      shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);

      if(shmid==-1)

      {

          fprintf(stderr,"shmget failed\n");

           exit(EXIT_FAILURE);

      }

 

      /*映射共享内存*/

      shared_memory=shmat(shmid,(void *)0,0);

      if(shared_memory==(void *)-1)

      {

          fprintf(stderr,"shmat failed\n");

           exit(EXIT_FAILURE);

      }

      printf("Memory attached at %X\n",(int)shared_memory);

 

      /*让结构体指针指向这块共享内存*/

      shared_stuff=(struct shared_use_st *)shared_memory;

 

      /*循环的向共享内存中写数据,直到写入的为“end”为止*/

      while(running)

      {

          while(shared_stuff->written_by_you==1)

           {

               sleep(1);//等到读进程读完之后再写

                 printf("waiting for client...\n");

           }

           printf("Ener some text:");

           fgets(buffer,BUFSIZ,stdin);

           strncpy(shared_stuff->some_text,buffer,TEXT_SZ);

           shared_stuff->written_by_you=1;

           if(strncmp(buffer,"end",3)==0)

           {

               running=0;  //结束循环

           }

      }

 

      /*删除共享内存*/

      if(shmdt(shared_memory)==-1)

      {

          fprintf(stderr,"shmdt failed\n");

           exit(EXIT_FAILURE);

      }

      exit(EXIT_SUCCESS);

}

 

 

#include

#include

#include

#include

#include

#include

#include

#include "shm_com.h"

 

int main(void)

{

    int running=1;

      void *shared_memory=(void *)0;

      struct shared_use_st *shared_stuff;

      int shmid;

 

 

      /*创建共享内存*/

      shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);

      if(shmid==-1)

      {

          fprintf(stderr,"shmget failed\n");

           exit(EXIT_FAILURE);

      }

 

      /*映射共享内存*/

      shared_memory=shmat(shmid,(void *)0,0);

      if(shared_memory==(void *)-1)

      {

          fprintf(stderr,"shmat failed\n");

           exit(EXIT_FAILURE);

      }

      printf("Memory attached at %X\n",(int)shared_memory);

 

      /*让结构体指针指向这块共享内存*/

      shared_stuff=(struct shared_use_st *)shared_memory;

 

      /*控制读写顺序*/

      shared_stuff->written_by_you=0;

 

      /*循环的从共享内存中读数据,直到读到“end”为止*/

      while(running)

      {

         if(shared_stuff->written_by_you)

         {

             printf("You wrote:%s",shared_stuff->some_text);

              sleep(1);  //读进程睡一秒,同时会导致写进程睡一秒,这样做到读了之后再写

              shared_stuff->written_by_you=0;

              if(strncmp(shared_stuff->some_text,"end",3)==0)

              {

                  running=0; //结束循环

              }

         }

      }

 

      /*删除共享内存*/

      if(shmdt(shared_memory)==-1)

      {

          fprintf(stderr,"shmdt failed\n");

          exit(EXIT_FAILURE);

      }

       exit(EXIT_SUCCESS);

}

阅读(572) | 评论(0) | 转发(0) |
0

上一篇:消息列队

下一篇:信号处理

给主人留下些什么吧!~~