Designing a Queue to be a shared memory
Solution 1
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.
Solution 2
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;
}
Comments
-
Sangeeth Saravanaraj almost 2 years
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.
-
Sangeeth Saravanaraj over 12 yearsThanks for the example. This is correct (but incomplete). Any idea on how to attach/associate a structure to a shared memory?
-
Mark Wilkins over 12 years@SangeethSaravanaraj: Assign the memory to a pointer to a structure. Same as memory from a malloc call. You need to make sure that each process has an identical definition of the structure (with the same packing).
-
Mark Wilkins over 12 years@SangeethSaravanaraj: For fun, I updated the example to use a structure and to be slightly more interesting (allow two instances two run and have one fill the memory and the other to print it).
-
Sangeeth Saravanaraj over 12 yearsBeej's guide is awesome. Thanks for sharing. :)
-
Sangeeth Saravanaraj over 12 yearsThanks for your effort. Your code snippet gave me few good hints to complete my task. +1
-
Sangeeth Saravanaraj over 12 yearsI referred this also <kohala.com/start/unpv22e/unpv22e.chap12.pdf> .. relatively good info.
-
dario_ramos over 12 yearsLooks like a more detailed reference, good for you. A little detail: Remove the "<" and ">" from your link, otherwise it won't work (I could see the doc because I edited the URL)