Re: Shared Memory...Some Questions.....



On Mar 21, 7:53 am, Martin Vuille <jpm...@xxxxxxxxx> wrote:
"Solomon_Man" <cmgra...@xxxxxxxxx> wrote innews:1174442771.089368.296510@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:

I'm by no means a shared memory expert...

if (pid == 0) /* child process */
{
int indexIdChild = 0;
if((indexIdChild =
shmget(key,sizeof(int),SHM_R|SHM_W))<0)
{
cout<<" CHILD ERROR: indexIdChild "<<endl;
}

A few things here...

Where does "key" come from? I did not see it declared prior to this
point. Same with

Attached memory segments are inherited across a fork, so you
shouldn't have to repeat the shmget/shmat in the children.

You specified IPC_PRIVATE as the key when you first called shmget
before the fork. If you want to be able to shmget the same segment
in another process, you neet to use the same key in all shmgets and
that key cannot be IPC_PRIVATE.

int ctheSharedIndex;
ctheSharedIndex = (int)shmat(indexIdChild,0,0);
*/
/* int ctheSharedIndex;
ctheSharedIndex = (int)shmat(indexId,0,0);*/
cout<<getpid()<<" "<<ptheSharedIndex<<endl;
ptheSharedIndex = 1099;

As already pointed out, this is incorrect. shmat returns a pointer
to the shared segment. If you are not comfortable with pointers, I
suggest that you do some experimentation in a simple program
without shared memory first. Any decent C tutorial should explain
pointers.

shmat returns a "generic" pointer (void*) because it has no way of
knowing what you plan to use the segment for. In your case, since
you are using it to store an int, you need to store the return
value of shmat into an "int*", i.e., a pointer to an integer, so:

int* pTheSharedIndex = shmat( ... )

Then, to store a value there you would dereference the pointer:

*pTheSharedIndex = 1099;

which means "store 1099 in the integer that pTheSharedIndex point
to".

MV

--
I do not want replies; please follow-up to the group.

All thanks so much for the help. I definetly was flawed on my way of
thinking for this example. :)

I will recode the small test app and try and get it to work from all
your suggestions.

After posting this originally, I found a few examples checking the
return value of shmat ( == (void *)-1) and figured that I definetlly
had some issue with my original idea. Then I also read on a man page
at home where the child processes inherits the shared memory after a
fork. The key issue was a idea I tried after seeing a few examples
online, I gave up on that and went to IPC_PRIVATE instead of the key.

Again thank for the help and I will repost with what I come up with
later tonight.

Thanks,
Chris


.



Relevant Pages

  • Re: Shared Memory...Some Questions.....
    ... I'm by no means a shared memory expert... ... /* int ctheSharedIndex; ... shmat returns a pointer ... you are using it to store an int, you need to store the return ...
    (comp.os.linux.development.system)
  • Looking for a more elegant way to do memory offsets
    ... I am currently using an implementation of sysV shared memory. ... pointer to the head, everything should be done as offsets from this. ... int fur_flag; ... Basically I know that my first element is sizeofaway from ...
    (comp.lang.c)
  • Re: sorting the input
    ... Ideally, given a value of some type T, the best place to store ... is not really good enough -- an int and a float are often the ... This will print "4 4" on many machines (I have one here that does ... one "kind" of pointer, having the bits moved around (byte vs word ...
    (comp.lang.c)
  • Re: Can array[]=malloc()ed?
    ... >>You cannot store a pointer value in an `int' variable. ... CLC FAQ CLC readme: ...
    (comp.lang.c)
  • Re: How much extra window memory really gets allocated with cbWndClass?
    ... I need to store a pointer and an int. ... > extra window memory, minus the size of an integer." ... Thus the last valid int address is "the number of bytes of extra window ...
    (microsoft.public.win32.programmer.ui)