[comp.unix.wizards] Pointers in Shared Memory

flibedin@taurus.BITNET (04/14/91)

I have some doubts concerning the use of pointers inside shared memory
obtained with SysV IPC functions.

My intention is to have a linked list in shared memory. For that
I run a test program that showed me that is not possible to store
a pointer obtained from malloc in shared memory, also I saw that
is not possible to store pointers to the user address space.

However, it is possible to store pointers INSIDE the shared memory,
and also to another shared memory segment.

Someone there has experience or comments on a problem like this one?

Fernando Libedinsky
Tel-Aviv University
flibedin@math.tau.ac.il

cliffs@gaffa.East.Sun.COM (Clifford C. Skolnick) (04/18/91)

In article <2417@taurus.BITNET> <flibedin%math.tau.ac.il@TAUNIVM.TAU.AC.IL> writes:
>I have some doubts concerning the use of pointers inside shared memory
>obtained with SysV IPC functions.
>
>However, it is possible to store pointers INSIDE the shared memory,
>and also to another shared memory segment.

Careful here too.  Store the pointers as offsetts from the address where
teh segment is attached.  This will allow a different process who has not
attached the segment at the same address (by choice, or just can't do it) to
still use the data.  You will experience a slight performance hit, but a slow
working program is better than a fast one that does not work.
--
Cliff Skolnick | "When routine life's hard, and inhibitions are low, and
cliffs@sun.com | resentment lies hide, but emotions run through, and we're
(716) 385-5049 | changing our ways, taking different roads.  Love, love
I think. I am. | will tear us apart, again." -- Joy Division

andre@targon.UUCP (andre) (04/24/91)

In article <2417@taurus.BITNET> <flibedin%math.tau.ac.il@TAUNIVM.TAU.AC.IL> writes:
>I have some doubts concerning the use of pointers inside shared memory
>obtained with SysV IPC functions.

>My intention is to have a linked list in shared memory. For that
>I run a test program that showed me that is not possible to store
>a pointer obtained from malloc in shared memory, also I saw that
>is not possible to store pointers to the user address space.

Well... it is posible, put meaningless the pointer then points to
a part of somebody else's data (even if your program has that
address in its memory map. and as you know, you need shared memory
for this kind of thing.

>However, it is possible to store pointers INSIDE the shared memory,
>and also to another shared memory segment.

Again... this is possible, but BEWARE ! if you let the kernel
(together with the kernel varaible SHMBRK) choose the address to start
the segment, it will be likely in different places for different
non-trivial programs. Remember, malloc() changes the sbrk() value
relative to where the kernel starts your shared memory segment.

If you want to keep linked lists in shared memory, you must keep
the list elements also in shared memory and in stead of pointers
you must use offsets to the beginning of the shared memory.
If you only have to point to the list elements you can build
your shared memory like:

struct info { int num_elsms; int first_elem };
struct elem { char data[MAX]; int next};

struct info *list_info = (address of shared mem);
struct elem *list_data = (address of shared mem) + sizeof struct info;

now if you want to follow the list you use:

{
	struct elem *ep;

	for (ep = list_data + list_info->first_elem; ep != list_data ;
		ep = list_data + ep->next)
	{
		printf("Data is %s\n",ep->data);
	}
}

-- 
The mail|    AAA         DDDD  It's not the kill, but the thrill of the chase.
demon...|   AA AAvv   vvDD  DD        Ketchup is a vegetable.
hits!.@&|  AAAAAAAvv vvDD  DD                    {nixbur|nixtor}!adalen.via
--more--| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre