?
Solved

Mapping a struct instance to shared memory

Posted on 2003-03-12
2
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

743 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