[comp.unix.questions] Are pointers in a shared memory segment valid for a different process?

kelley@qucis.queensu.CA (Todd Kelley) (08/23/90)

This question is closely related to my previous question about IPC
using shared memory (comp.unix.questions).

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?  For instance, should each string's
offset from the base of the segment be stored in the array, rather
than the actual pointer?

If this *is* wrong, please let me know.

Respond by email, and if there seems to be enough interest in this
question, I'll post a summary.
-- 

Todd Kelley  (kelley@qucis.queensu.ca)

cpcahil@virtech.uucp (Conor P. Cahill) (08/24/90)

In article <838@qusunc.queensu.CA> kelley@qucis.queensu.CA (Todd Kelley) writes:
>
>	[Question of using pointers to shared memory segments between two
>	 processes deleted]
>

The easiest way to do this is to ensure that both processes attach the
segment at the same address.  In that case, any pointers within the segment
will be the same for both processes.

The following algorithm could be used:

	creat a new segment
	attach at default address
	calculate extra space needed and re-attach the segment far enough
		away from your data segment so that you wont run out of malloc
		space
	save the address that it was attached at in the first portion of the
		shared memory segment.

For other processes:
	get segment id
	attach segment at default address
	get correct attachment address (from the shared memory segment itself)
	detach & re-attach at the correct address.

The only thing you need to ensure is that when you are leaving enough room 
between the shared memory segment and your program's standard data segment
you must ensure that the resultant address is far enough down for any programs
that are going to use this shared memory segment.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

ray@ctbilbo.UUCP (Ray Ward) (08/25/90)

In article <838@qusunc.queensu.CA>, kelley@qucis.queensu.CA (Todd Kelley) writes:
> Suppose that process A and process B both have one particular shared
> memory segment attached to their respective address spaces with shmat().
>
> 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?  

You are correct.  If the shared memory segment maps into different addresses
in the two processes address spaces, pointers are invalid between the two
processes.

The first response most programmers have to this problem is to store
offsets somewhere.  This is cumbersome, at best, and has never been
required in any system I have seen.

Another approach, sometimes feasible, is to store only non-pointer data
in the shared memory area.  This is rather constricting for C programmers.

The most usual approach is to pick an address at which to map in the
shared memory segment in *both* programs.  If you pick an address that
is considerably larger than the larger of the two programs is likely to 
grow, no further maintainance is required. Both programs can place
pointers in the shared memory area because both programs have mapped the
shared memory segment to the same address.  (For portability it is best to
set the SHM_RND flag to allow architectures that require segment boundaries
to do their own thing and tell you where they *really* mapped in the
segment.)


-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Ray Ward                                          Email:  uunet!ctbilbo!ray  
Voice:  (214) 991-8338x226, (800) 331-7032        Fax  :  (214) 991-8968     
=-=-=-=-  There _are_ simple answers, just no _easy_ ones. -- R.R. -=-=-=-=

gt0178a@prism.gatech.EDU (BURNS,JIM) (08/25/90)

in article <1990Aug24.115009.24808@virtech.uucp>, cpcahil@virtech.uucp (Conor P. Cahill) says:
> For other processes:
> 	get segment id
> 	attach segment at default address
> 	get correct attachment address (from the shared memory segment itself)
> 	detach & re-attach at the correct address.

To summarize some points made in a recent similar thread in
comp.unix.wizards, this only works if your OS allows specifying the attach
address, and, if the OS is virtual, the attach address means the same
thing in different process address spaces. Specifying offsets may be more
work, but its more portable.
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

pgd@bbt.se (P.Garbha) (08/25/90)

In article <115@ctbilbo.UUCP> ray@ctbilbo.UUCP (Ray Ward) writes:

>The most usual approach is to pick an address at which to map in the
>shared memory segment in *both* programs.  If you pick an address that

Some systems/architectures might have restrictions to the allowable
addresses chosen. In a application using shared memory, that I have
made, the program that creates the shared memory, let's the operating
system choose the address, and write it as the first word in shared
memory. The next program to link up, then let's the os map just the
first word of the shared memory, reads the starting address, unmaps
the memory, and finally maps it to the correct starting address, and 
length.

I was using Xenix/386 (The intel 386 i a segmented architecture)