[置顶] 共享内存 shm

往共享内存中写:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "shm_com.h"
#include <sys/shm.h>

#include <sys/types.h>
#include <sys/ipc.h>


int main()
{
  int running=1;
  void *shared_memory=(void *)0;//??
  struct shared_use_st *shared_stuff;
  int shmid;
  char buffer[BUFSIZ];
 /*创建共享内存*/
 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==1)
    {
       
       // sleep(1);//??读进程读了之后再写
        printf("waiting for client...\n");
       
     }
  printf("enter some text:");
  fgets(buffer,BUFSIZ,stdin);
  strncpy(shared_stuff->some_text,buffer,TEXT_SZ);
  shared_stuff->written_by_you=1;
        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);
}

读共享内存:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "shm_com.h"
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>

int main()
{
  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);
}

/*
fprintf是C/C++中的一个格式化写—库函数;其作用是格式化输出到一个流/文件中;
  函数完整形式: int fprintf(FILE *stream,char *format,[argument])
*/

/*
函数名: strncmp
  功 能: 串比较
  用 法: int strncmp(char *str1, char *str2, int maxlen);
  说明:此函数功能即比较字符串str1和str2的前maxlen个字符。如果前maxlen字节完全相等,返回值就=0;在前maxlen字节比较过程中,如果出现str1[n]与str2[n]不等,则返回(str1[n]-str2[n])。
*/


你可能感兴趣的:(struct,Stream,File,buffer)