From: Casper H.S. Dik on
Ian Collins <ian-news(a)hotmail.com> writes:

>queengambit wrote:
>> Good morning Ian, Bin,
>> I don't think mapping the shared memory to processes at exactly the
>> same address a good idea as in my wrapper (which is what i have to
>> write) users are allowed to 'exec' after 'fork'. The method sounds
>> good is using offset.

>The segment could be mapped with MAP_FIXED.

Dangerous, because you might accidentily map on top of something else.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
From: Bin Chen on
On 2ÔÂ26ÈÕ, ÏÂÎç4ʱ36·Ö, "queengambit" <cbp...(a)cs.york.ac.uk> wrote:
> Good morning Ian, Bin,
> I don't think mapping the shared memory to processes at exactly the
> same address a good idea as in my wrapper (which is what i have to
> write) users are allowed to 'exec' after 'fork'. The method sounds
> good is using offset.
> ok, let say i already do the following:
>
> struct my_ringnode_st {
> my_ringnode_t *next;
> my_ringnode_t *prev;
> int my_offset[PTHREAD_THREADS_MAX]; /* for offset to nex and prev
> elements */
> };
>
> shm_unlink(shm_name);
> fd = shm_open(mm_name, O_RDWR | O_CREAT | O_EXCL, S_IRWXU);
> if (fd ==-1)
> perror("error opening file for read and write");
>
> /*set size of the my_shared memory object to sizeof(struct
> my_ringnode_st) */
> ftruncate(fd, PTHREAD_THREADS_MAX*sizeof(struct my_ringnode_st));
>
> /*in parent */
> ptr1 = mmap(NULL, PTHREAD_THREADS_MAX*sizeof(struct my_ringnode_st),
> PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> if (ptr == MAP_FAILED)
> {
> perror("error mapping the file");
> exit(EXIT_FAILURE);
> }
>
> /*in child */
> ptr2 = ... /* same as parent */
>
> With child, ptr2 = child_base_addr. Now if i want to add a new element
> to the linked list in child, which is initially NULL , what do i have
> to do? What i'm really confused is how to allocate new block of space
> from the shared memory for the new element of the linked list and how
> offset works
>
> thank you

You need to write a simple allocator that ensures what memory you
allocated for the list are within the range of the mmap'd range.
Otherwise, you can't share the pointer between the parent and
child(multi-process).

From: queengambit on
I'm sorry but i can't think of even a simple way to implement my own
allocator & deallocator working on shared memory with items in the
linked list are of the same size sizeof(struct my_ringnode_st).

I thought of defining a new struct with two arrays, one for holding
offset in shared memory of each item of the linked list and the other
for the actual items. The problem with this approach is how to keep
track of free slots in the second array so that new item can be
allocated as any item can be removed from the linked list.

struct my_shm{
long ringnode_off[PTHREAD_THREADS_MAX];
my_ringnode_t ringnodes_array[PTHREAD_THREADS_MAX*sizeof(struct
my_ringnode_st)]
}
Please advise me on how to implement the allocator/deallocator for my
shared memory of linked list.

Thank you

From: Casper H.S. Dik on
"queengambit" <cbp500(a)cs.york.ac.uk> writes:

>I thought of defining a new struct with two arrays, one for holding
>offset in shared memory of each item of the linked list and the other
>for the actual items. The problem with this approach is how to keep
>track of free slots in the second array so that new item can be
>allocated as any item can be removed from the linked list.

I'd suggest doing it simpler and just allocating an array of your
structures in the shared memory.


struct mystruct *myarray = <address of start of shmem>

Then, rather than using pointers to entries, use integer indices 1 .. N.

The initial allocating would put all those entries on a single
free list, e.g., of the "next" field of the initial node which
would be reserved as the free list pointer (and this allows you
to use index 0 as NULL as index 0 would be invalid)
(You'd need N+1 structures allocated)

Casper
From: Ian Collins on
Casper H.S. Dik wrote:
> Ian Collins <ian-news(a)hotmail.com> writes:
>
>
>>queengambit wrote:
>>
>>>Good morning Ian, Bin,
>>>I don't think mapping the shared memory to processes at exactly the
>>>same address a good idea as in my wrapper (which is what i have to
>>>write) users are allowed to 'exec' after 'fork'. The method sounds
>>>good is using offset.
>
>
>>The segment could be mapped with MAP_FIXED.
>
>
> Dangerous, because you might accidentily map on top of something else.
>
I agree, use with great care but still worth investigating.

--
Ian Collins.