SUMMARY: Are pointers in a shared memory segment valid for a different process?
Todd Kelley
kelley at qucis.queensu.CA
Sun Aug 26 05:52:08 AEST 1990
This is a summary of responses to my question about pointers in
shared memory. I'd like to thank all those who provided a response:
volpe at disney.crd.ge.com (Christopher R Volpe)
Greg Hunt <hunt at DG-RTP.DG.COM>
Jean-Luc Lachance <jll at tnl.CAM.ORG>
Doug Luecke <iex!neptune.iex.com!doug at uunet.UU.NET>
mmcg at bruce.cs.monash.OZ.AU (Mike McGaughey)
Question:
>Suppose that process A and process B both have one particular shared
>memory segment attached to their respective address spaces with shmat().
>
>Now suppose that process A puts an array of strings into the shared memory
>segment, such that each string resides in the shared memory segment, but
>they do not occupy a contiguous piece of memory. That array will be
>meaningless to process B, because the (char *) pointers in the array
>point into A's address space, and not B's address space. If the base of
>the shared memory segment is at AAA0 in A's address space, and at
>BBB0 in B's address space (for example), in order for
>B to access the strings in the array, B would need to add (AAA0-BBB0) to
>each of the pointers in the array to make them point at the strings in
>B's address space.
>
>Is this wrong? If not, is there an accepted way to map addresses back
>and forth between address spaces? Should each string's offset from the
>base of the segment be stored in the array, rather than the actual pointer?
Almost everyone said that pointers to data in a shared memory segment
should be stored as an offset from the base of the segment. (The one
exception involved the assumption that the base of the segement would
be the same in both processes.) It was mentioned that arranging to
have a shared segment attached at the same address in both processes
can be attempted, but this becomes a problem if the application is handling
multiple shared segments of varying sizes.
Also, since offsets are integers, an array of strings would be stored
in a shared segment as an array of integers instead of "char *".
A macro could be used to convert an integer to the corresponding "char *",
for example, SHMOFF_TO_POINTER(segment, offset).
Below are two examples of C code. The first shows how to deal with
an array of strings in shared memory, and the second shows how to deal
with an array of structures in shared memory.
----------------------------------------------------------------------------
From: volpe at disney.crd.ge.com (Christopher R Volpe):
For example, you might do something along the lines of this:
char *string_one;
char *string_two;
int *string_pointer_offsets;
string_one= get_free_shmem_space(25*sizeof(char));
strcpy(string_one,"First string data");
string_two= get_free_shmem_space(25*sizeof(char));
strcpy(string_two,"Second string data");
string_pointer_offsets=get_free_shmem_space(2*sizeof(int));
string_pointer_offsets[0]=string_one-shmem_base;
string_pointer_offsets[1]=string_two-shmem_base;
Now, assuming you communicate to the other process where
"string_pointer_offsets"
is, or always keep it in some hard-coded predefined place in shared memory,
the other process can look at the two elements, add its own shmem_base
to each, and get at the data for string_one and string_two.
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com
----------------------------------------------------------------------
From: Greg Hunt <hunt at DG-RTP.DG.COM>
This coupled with C's (I'm assuming that you're using C) pointer
arithmetic capabilities makes it very simple to move around [in] arrays in
shared memory. You compute the (base + offset to beginning of array)
once for each process and remember it in a global variable or
something. From then on you use C pointer arithmetic to access
elements of the array like this:
ptr_to_base_of_array = my_shared_segment_attach_address +
offset_of_array_into_segment;
To access any element use:
(ptr_to_base_of_array + element_number) -> structure_element ...
The element numbers are the the array subscripts as you would
normally use them. The first element being numbered zero, the last
one being numbered (number_of_elements_in_array - 1).
You need to properly cast the pointers into "pointers to an element
of the array" to get the pointer arithmetic to work properly.
That's my opinion on the matter. Hope this helps. Enjoy!
--
Greg Hunt Internet: hunt at dg-rtp.dg.com
DG/UX Kernel Development UUCP: {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC These opinions are mine, not DG's.
-------------------------------------------------------------------------
--
Todd Kelley (kelley at qucis.queensu.ca)
More information about the Comp.unix.wizards
mailing list