简体   繁体   English

将队列设计为共享内存

[英]Designing a Queue to be a shared memory

I'm attempting to design/implement a (circular) queue (in C) as a shared memory so that it can be shared between multiple threads/processes.我正在尝试将(循环)队列(在 C 中)设计/实现为共享内存,以便它可以在多个线程/进程之间共享。

The queue structure is as follows:队列结构如下:

typedef struct _q {
    int q_size;
    int q_front;
    int q_rear;
    int *q_data;
}queue;

Which supports the following functions:其中支持以下功能:

int empty_q(queue *q);
int display_q(queue *q);
int create_q(queue **q, int size);
int delete_q(queue **q);
int enqueue(queue *q, int data);
int dequeue(queue *q, int *data);

As per the queue size mentioned by the user, the memory for q_data will be allocated in create_q().根据用户提到的队列大小,q_data 的内存将在 create_q() 中分配。

Question: How to create a shared memory for this queue using system functions provided in "sys/shm.h"?问题:如何使用“sys/shm.h”中提供的系统函数为该队列创建共享内存? Any code snippet/example for creating/attaching/retrieving/deleting shared memory for the queue data-structure using shmget(), shmat(), shmctl(), etc would be a great help.任何使用 shmget()、shmat()、shmctl() 等为队列数据结构创建/附加/检索/删除共享内存的代码片段/示例都会有很大帮助。

Here is a simple example that creates shared memory the size of a structure, writes some data to it and prints it out.这是一个简单的示例,它创建结构大小的共享内存,向其中写入一些数据并将其打印出来。 Run one instance and it will create the shared memory and put some "data" in it, and then wait for a key press.运行一个实例,它将创建共享内存并将一些“数据”放入其中,然后等待按键。 Run a second instance in a different command prompt, and the second instance will print the contents of the memory.在不同的命令提示符下运行第二个实例,第二个实例将打印内存的内容。

typedef struct
   {
   char a[24];
   int i;
   int j;
   } somestruct;


void fillshm(int shmid) {
   somestruct *p;

   if ( (p = shmat (shmid, NULL, 0)) < 0 )
      {
      perror("shmat");
      exit(1);
      }

   printf("writing to shared memory\n");
   strcpy(p->a, "my shared memory");
   p->i = 123;
   p->j = 456;
}


void printshm(int shmid)
{
   somestruct *p;
   if ( (p = shmat (shmid, NULL, 0)) < 0 )
      {
      perror("shmat");
      exit(1);
      }

   printf( "%s, %d, %d\n", p->a, p->i, p->j );
}

int main( int argc, char *argv[] ) {

   int shmid;

   // see if the memory exists and print it if so
   if ( (shmid = shmget (1234, 0, 0)) >= 0 )
      printshm( shmid );
   else
      {
      // didn't exist, so create it
      if ( (shmid = shmget (1234, sizeof( somestruct ), IPC_CREAT | 0600)) < 0 )
         {
         perror("shmget");
         exit(1);
         }

      printf( "shmid = %d\n", shmid );

      fillshm(shmid);
      printf( "Run another instance of this app to read the memory... (press a key): " );
      getchar();

      // delete it
      if ( shmctl (shmid, IPC_RMID, NULL) < 0 )
         {
         perror("semctl");
         exit(1);
         }
      }

   return 0;
}

When I messed with Unix IPC, I followed Beej's guide to Unix IPC .当我搞砸 Unix IPC 时,我遵循了 Beej 的 Unix IPC 指南 It even has some jokes!它甚至有一些笑话! You can go directly to the shared memory section .您可以直接进入共享内存部分 It has snippets explaining each step, and a full example at the end.它有解释每个步骤的片段,最后有一个完整的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM