Discussion:
[Linux] Shared memory and pointers
(too old to reply)
Willy
2012-05-14 15:17:36 UTC
Permalink
Hi,
I've a problem with shared memory and pointers.

I've a struct like this in shm:

typedef struct my_struct
{
char mat[3][10];
char * p_mat;
}my_struct;

and a process A that sets:

shm->p_mat = &shm->mat[2];

Another process B that try to print it:

printf("%s",shm->p_mat);

I obtain a segmentation fault.

I think that is not the right way to use pointers in shared memory, but
I can't find more info.
--
*Willy* - *Contatti*: http://snipurl.com/contatti
* *FAQ* di /it-alt.comp.software.mozilla/: http://www.FAQmozilla.org
* *Gratuiki* - Software Free: http://gratuiki.willygroup.org
Victor Bazarov
2012-05-14 15:30:39 UTC
Permalink
Post by Willy
I've a problem with shared memory and pointers.
Shared memory is not defined by the language, nor is interprocess
communication (how, for instance, memory addresses are mapped between
two different processes, whether the memory is shared or not). You'll
be better off asking about platform-specific elements in that platform's
newsgroup. Try 'comp.os.linux.development.*' hierarchy.

Good luck!
Post by Willy
typedef struct my_struct
{
char mat[3][10];
char * p_mat;
}my_struct;
shm->p_mat = &shm->mat[2];
printf("%s",shm->p_mat);
I obtain a segmentation fault.
I think that is not the right way to use pointers in shared memory, but
I can't find more info.
V
--
I do not respond to top-posted replies, please don't ask
BGB
2012-05-14 16:16:08 UTC
Permalink
Post by Willy
Hi,
I've a problem with shared memory and pointers.
typedef struct my_struct
{
char mat[3][10];
char * p_mat;
}my_struct;
shm->p_mat = &shm->mat[2];
printf("%s",shm->p_mat);
I obtain a segmentation fault.
I think that is not the right way to use pointers in shared memory, but
I can't find more info.
technically OT here, but I will answer:
part of the problem here is that, unless otherwise specified, the
shared-memory will not necessarily be mapped to the same address in
every process.

as a result, pointers in one process will not be correct in another
(since all the memory will be in a different location, ...).

instead, other options are needed, such as:
storing pointers as offsets relative to the mapping base address;
storing pointers as relative offsets from the address of the pointer.

also note that pointers to any memory outside the shared-memory can't be
safely used, as they may not exist in the other process.

or such...
Paavo Helde
2012-05-14 16:38:19 UTC
Permalink
Post by Willy
Hi,
I've a problem with shared memory and pointers.
typedef struct my_struct
{
char mat[3][10];
char * p_mat;
}my_struct;
shm->p_mat = &shm->mat[2];
printf("%s",shm->p_mat);
I obtain a segmentation fault.
I think that is not the right way to use pointers in shared memory, but
I can't find more info.
This is related to the concept of virtual memory. Pointers from one
process are not guaranteed to make any sense in another. You will need to
force the shared memory block to be loaded at the same address (this is
all very platform-specific and seems error-prone) or replace the pointer
by some more invariant value (an offset from the beginning of the shared
memory block, for example).

hth
Paavo
Scott Lurndal
2012-05-14 17:01:42 UTC
Permalink
Post by Willy
Hi,
I've a problem with shared memory and pointers.
typedef struct my_struct
{
char mat[3][10];
char * p_mat;
}my_struct;
shm->p_mat = &shm->mat[2];
printf("%s",shm->p_mat);
I obtain a segmentation fault.
How did you map the shared memory region? You basically have three choices -
shmat(2), open(2) with mmap(2) or the posix interface shm_open(2) with mmap(2).

There is no guarantee that the shared memory segment will be mapped into the
process virtual address space at the same address in different processes. You
may not use pointers in the shared memory segment, but rather must use offsets
from the base address of the region (returned by shmat(2) or mmap(2)).

It is not recommended to provide a 'shmaddr' parameter to shmat(2) or an 'addr'
parameter to mmap(2) since the address may not be available in the process.

However, if you were unable to dereference 'shm' in your example above without
generating a segmentation violation, then you've probably not successfully mapped
the shared memory segment in process B.

scott
Willy
2012-05-15 09:18:54 UTC
Permalink
Hi,[cut...]
Thanks and sorry for OT.

I think I will use indexes instead pointers.
The biggest problem is that my code is already written (I'm doing a
DOS->Linux porting) so I've a lot of code to modify and test :-(
--
*Willy* - *Contatti*: http://snipurl.com/contatti
* *FAQ* di /it-alt.comp.software.mozilla/: http://www.FAQmozilla.org
* *Gratuiki* - Software Free: http://gratuiki.willygroup.org
Nobody
2012-05-16 05:56:46 UTC
Permalink
Post by Willy
Thanks and sorry for OT.
I think I will use indexes instead pointers.
The biggest problem is that my code is already written (I'm doing a
DOS->Linux porting) so I've a lot of code to modify and test :-(
If the code is C++, you can make a class which contains an index but which
behaves like a pointer by implementing operator* and operator->.

You can force mapping at a specific address by passing the MAP_FIXED flag
to mmap(), but that will fail if any part of the address space is already
in use.

Juha Nieminen
2012-05-15 09:38:47 UTC
Permalink
Post by Willy
Hi,
I've a problem with shared memory and pointers.
typedef struct my_struct
{
char mat[3][10];
char * p_mat;
}my_struct;
shm->p_mat = &shm->mat[2];
printf("%s",shm->p_mat);
I obtain a segmentation fault.
Is 'mat' a null-terminated char array? It doesn't look like one.
Continue reading on narkive:
Loading...