Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Mapping a struct instance to shared memory

Posted on 2003-03-12
2
Medium Priority
?
289 Views
Last Modified: 2013-12-26
Hi!

I'm rather weak in C and my knowledge of IPC is lacking as well so I'd appreciate any and all advice.

The problem:

I'm writing a program in C that allocates shared memory for interprocess communication. So I shmget it and shmat it and end up with a pointer to this shared memory block (please feel free to correct me if I'm wrong.)

Now I have a structure defined by

typedef struct something{
int a;
int b;
char c[100];
int d;
} something2;

and I want an instance of this struct to be placed in the shared memory so that I could use something2_instance.a from any process that has access to this shared memory block...

Maybe something along the lines of creating a pointer to the struct instance and .. then doing something to it?

As you see I'm really puzzled, any hints?

THank you in advance
0
Comment
Question by:NeverAgain
2 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 400 total points
ID: 8124528
first of all have a look on this link.. its a very good tutorial..
http://www.cs.cf.ac.uk/Dave/C/node27.html

and about mapping the struct to shared memory..well here is the way ( using the definition of ur struct)

void *shm;
something2 *s1;

now lets say u get pointer to the memory with
shm=shmat(..usual stuff);

now there are at least two basic ways to fill the value of struct at shm
1)  make a something2 variable fill up and copy to memory at shm
s1=(something2*)malloc(sizeof(something2));
fill up s1  ..
s1->a=12;... and so on..

memcpy(shm,(void*)s1,sizeof(something2));

2) typecast the shm to something2 * and fill up the values;
s1=(something2*)shm;

s1->a=12; s1->b=24;  and so on



this will write only one something2 type data on shm .. u can extend it to put array of structs and more stuff..
also when writer is writing .. reader shud wait .. and viceversa .. the usual synchronization stuff
0
 

Author Comment

by:NeverAgain
ID: 8124608
THanks!

I'll give it a try
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question