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



"Solomon_Man" <cmgray74@xxxxxxxxx> wrote in
news: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.
.



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: raw byte representation of unsigned long long?
    ... I encountered a problem involving storage of unsigned long long. ... I am wondering whether there is other way to store big int ... or perhaps a function returns a pointer to a long long ...
    (comp.lang.c)