Pointers in Shared Memory

andre andre at targon.UUCP
Wed Apr 24 06:57:53 AEST 1991


In article <2417 at taurus.BITNET> <flibedin%math.tau.ac.il at 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



More information about the Comp.unix.wizards mailing list