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