Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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().

Question: How to create a shared memory for this queue using system functions provided in "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.

like image 862
Sangeeth Saravanaraj Avatar asked Dec 12 '11 18:12

Sangeeth Saravanaraj


People also ask

Is Message Queue a shared memory?

The message queue is a buffer that is used in non-shared memory environments, where tasks communicate by passing messages to each other rather than by accessing shared variables.

What is a memory queue?

The Memory queue buffer is a FIFO type of data structure, where adding and reading can occur simultaneously. It works as a pipe where additions happen at one end and reading happens at the other end and reading removes the data from queue.

Which is faster message queue or shared memory?

The IPC shared semaphore facility provides process synchronization. Shared memory is the fastest form of interprocess communication. The main advantage of shared memory is that the copying of message data is eliminated.


2 Answers

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;
}
like image 91
Mark Wilkins Avatar answered Sep 30 '22 16:09

Mark Wilkins


When I messed with Unix IPC, I followed Beej's guide to 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.

like image 23
dario_ramos Avatar answered Sep 30 '22 15:09

dario_ramos