[comp.unix.wizards] Question about Sys V shared memory

abs@nbc1.UUCP (03/24/87)

The situation:

I have a large shared memory segment.  I would like to be able to
have a dynamic data structure within that segment, i.e. set aside
some memory within the shm segment for allocation, and have one
or more pointers within the shm segment pointing into this memory
pool.

The question:

If process A attaches the shm segment and assigns an address within
the segment's memory pool to one of these pointers, will this
address have any meaning to other processes that attach the
segment?

My guess:

Theoretically, no.  Practically, I would guess it depends on how
segmentation is done within the host machine.  (We have a VAX
running ULTRIX.)

Any help would be appreciated.  Thanks.
-- 
Andrew Siegel, N2CN		NBC Computer Imaging, New York, NY
philabs!nbc1!andy		(212)664-5776

dave@micropen.UUCP (03/25/87)

In article <314@nbc1.UUCP>, abs@nbc1.UUCP (Andrew Siegel) writes:
> The question:
> 
> If process A attaches the shm segment and assigns an address within
> the segment's memory pool to one of these pointers, will this
> address have any meaning to other processes that attach the
> segment?
> 
At least in Sys V, I would have each process call the allocator with
a pointer to the beginning of the shm ( the pointer returned to the 
process at the attach).  Then, the allocator routine would allocate
the memory and pass back a pointer with the appropriate displacement
into the segment.  Thus, addressing within the shm region is relative
to the virtual address that each process holds on the segment.  And
K&R say that pointer/integer arithmetic is cool (although some machines
like Intel or pdp's will place a limit on the size of memory in any
one segment but your nice VAX won't care a bit.)
-- 
David F. Carlson, Micropen, Inc.
...!{seismo}!rochester!ur-valhalla!micropen!dave

"The faster I go, the behinder I get." --Lewis Carroll

aeusesef@csun.UUCP (03/25/87)

In message <314@nbc1.UUCP>, Andrew Siegel writes:
>The situation:
>
>I have a large shared memory segment.  I would like to be able to
>have a dynamic data structure within that segment, i.e. set aside
>some memory within the shm segment for allocation, and have one
>or more pointers within the shm segment pointing into this memory
>pool.
>
>The question:
>
>If process A attaches the shm segment and assigns an address within
>the segment's memory pool to one of these pointers, will this
>address have any meaning to other processes that attach the
>segment?
>
>My guess:
>
>Theoretically, no.  Practically, I would guess it depends on how
>segmentation is done within the host machine.  (We have a VAX
>running ULTRIX.)
>
>Any help would be appreciated.  Thanks.
>-- 
>Andrew Siegel, N2CN		NBC Computer Imaging, New York, NY
>philabs!nbc1!andy		(212)664-5776

No, but what you can do is use offsets (if you want to just use one shared
memory segment), or, even closer (but this may not be what you want) to an
equivalent to calloc, get a new segment, and put the segment ID into some
reserved location within the original segment (which, I suppose you could
re-design just for that purpose).  Unfortuneatly, what I think you want to
do is something like this:
	char a[SIZE];
	shmat(id,a,0);
	ptr=&a[some_value_greater_than_0_and_less_than_SIZE]; /*COBOL!*/
and then expect ptr to be the same in all processes.  Well, for that, as
long as you do not store ptr in some memory location, it will work.
However, if you do need to store ptr, you may be able to use
	a[x]=ptr-a; /*In C, should work*/
I do hope that this helps.

 -----

 Sean Eric Fagan		    ------\
 Computer Center 		    litvax \
 Cal State University, Northridge   rdlvax  \
 18111 Nordhoff St.  		    psivax   --> !csun!aeusesef
 Northridge, CA  91330 		    hplabs  /
 AGTLSEF@CALSTATE.BITNET	    ihnp4  /
				    ------/
   "I drank what?!" -- Socrates | My opinions *are* facts.

dss@fatkid.UUCP (03/25/87)

In article <314@nbc1.UUCP> abs@nbc1.UUCP (Andrew Siegel) writes:
>
>If process A attaches the shm segment and assigns an address within
>the segment's memory pool to one of these pointers, will this
>address have any meaning to other processes that attach the
>segment?
>

The SystemV Interface Definition (SVID) [and AT&T's SystemV implementation]
specify the shmat() interface with an 'shmaddr' argument that may be either
an address at which to attach the shared-memory segment or zero (specifying
that the kernel should pick an address at which to attach the segment).

As such, if all processes attached the shared memory segment at the same
address, then absolute pointers into the segment would work.  However,
this is not a portable construct, for several reasons:

1) The SVID does not specify how you can determine the range of valid
   addresses for a shmat() operation.  It does not even say whether the
   address range may be mapped already (e.g., a block of malloc'ed bytes),
   although AT&T SystemV only allows you to attach shared memory at an
   address that is inbetween your current brk()-point and your stack.
   Other implementations might only allow shared memory to be attached
   at an address range that is already mapped in the data space of the
   process.

   By the way, the AT&T SystemV Programmer's Guide says, on page 9-104,
   "If [shmaddr] is user supplied, the address must be a valid address
    that the UNIX operating system would pick....It would be wise to
    let the operating system pick addresses so as to improve portability."

   [A portable way to use the 'supplied address' feature might be to
    attach a segment with a zero address, detach the segment, then
    reattach the segment at the same address that was returned before.
    But that isn't a very interesting use of this feature.]

2) There are some UNIX implementations that provide shared memory,
   but cannot support the 'attach at given address' feature, either
   because of machine architecture limitations or the virtual-memory
   implementation.

If you can structure your application such that all cooperating processes
share common source, then you could have a process attach the shared
memory segment and fork (shared memory mappings are preserved across fork(),
but not exec()).  Or, if you were only concerned about running your
application on specific implementations of AT&T SystemV, you could depend
on the 'attach at given address' feature and to hell with portability.
In general, however, it is a better practice to use offsets into shared
segments, rather than absolute pointers, to do what you're trying to do.

	Daniel Steinberg
	Sun Microsystems, Inc.
	ihnp4!sun!dss
	(415) 691-7531