From: David Given on
On 22/03/10 17:53, Vandana wrote:
[...]
> shm = (struct shared_mem*) shmat (shmid, NULL, 0);
[...]
> shm->emp = (struct emp*) shmat (shmid, NULL, 0);
[...]
> By doing the above, am I allocating memory for pointer to emp &
> pointer to dob within the shared memory?

As far as I can tell (and it's late and so I could very well be wrong),
this still won't work. You have no control over where your two blocks
will be mapped, therefore your pointers won't be valid in a different
process where they're mapped somewhere else.

I think you're not going to have any luck with this approach. Your best
bet is probably to use indices into an array of objects stored in the
shared memory segment. By avoiding pointers completely, the problem goes
away. As the shared memory segment is of fixed size, a heap isn't that
useful anyway, so it's not much more inconvenient (although I appreciate
that it is still a little inconvenient).

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
│ --- Conway's Game Of Life, in one line of APL
From: Vladimir Jovic on
David Given wrote:
> On 22/03/10 17:53, Vandana wrote:
> [...]
>> shm = (struct shared_mem*) shmat (shmid, NULL, 0);
> [...]
>> shm->emp = (struct emp*) shmat (shmid, NULL, 0);
> [...]
>> By doing the above, am I allocating memory for pointer to emp &
>> pointer to dob within the shared memory?
>
> As far as I can tell (and it's late and so I could very well be wrong),
> this still won't work. You have no control over where your two blocks
> will be mapped, therefore your pointers won't be valid in a different
> process where they're mapped somewhere else.
>
> I think you're not going to have any luck with this approach. Your best
> bet is probably to use indices into an array of objects stored in the
> shared memory segment. By avoiding pointers completely, the problem goes
> away. As the shared memory segment is of fixed size, a heap isn't that
> useful anyway, so it's not much more inconvenient (although I appreciate
> that it is still a little inconvenient).
>

Vandana can use mmap to get a pointer to the specific memory address.
From: Rainer Weikusat on
Vladimir Jovic <vladaspams(a)gmail.com> writes:
> David Given wrote:
>> On 22/03/10 17:53, Vandana wrote:
>> [...]
>>> shm = (struct shared_mem*) shmat (shmid, NULL, 0);
>> [...]
>>> shm->emp = (struct emp*) shmat (shmid, NULL, 0);
>> [...]
>>> By doing the above, am I allocating memory for pointer to emp &
>>> pointer to dob within the shared memory?
>>
>> As far as I can tell (and it's late and so I could very well be wrong),
>> this still won't work. You have no control over where your two blocks
>> will be mapped, therefore your pointers won't be valid in a different
>> process where they're mapped somewhere else.
>>
>> I think you're not going to have any luck with this approach. Your best
>> bet is probably to use indices into an array of objects stored in the
>> shared memory segment. By avoiding pointers completely, the problem goes
>> away. As the shared memory segment is of fixed size, a heap isn't that
>> useful anyway, so it's not much more inconvenient (although I appreciate
>> that it is still a little inconvenient).
>>
>
> Vandana can use mmap to get a pointer to the specific memory address.

In theory, yes. In practice, programs are not supposed to be aware of
their own address space layout since this collides with the political
necessity to provide OpenBSD invented "they will never figure out how
to exploit THAT!" pseudo security hacks[*] (like 'address space
randomization').

[*] The problem is the buggy code, not that someone may be
capable to exploit those bugs.
From: Tim Bowler on
On 03/22/2010 07:22 PM, David Given wrote:
> On 22/03/10 17:53, Vandana wrote:
> [...]
>> shm = (struct shared_mem*) shmat (shmid, NULL, 0);
> [...]
>> shm->emp = (struct emp*) shmat (shmid, NULL, 0);
> [...]
>> By doing the above, am I allocating memory for pointer to emp&
>> pointer to dob within the shared memory?
>
> As far as I can tell (and it's late and so I could very well be wrong),
> this still won't work. You have no control over where your two blocks
> will be mapped, therefore your pointers won't be valid in a different
> process where they're mapped somewhere else.
>
> I think you're not going to have any luck with this approach. Your best
> bet is probably to use indices into an array of objects stored in the
> shared memory segment. By avoiding pointers completely, the problem goes
> away. As the shared memory segment is of fixed size, a heap isn't that
> useful anyway, so it's not much more inconvenient (although I appreciate
> that it is still a little inconvenient).
>

To save addresses in shared memory, we always subtracted off
the base address of the shared memory and stored the offsets.

Whenever they're to be used as addresses, each process would
have to add their base address of the shared memory to the
offset.
From: Vladimir Jovic on
Rainer Weikusat wrote:
> Vladimir Jovic <vladaspams(a)gmail.com> writes:
>> David Given wrote:
>>> On 22/03/10 17:53, Vandana wrote:
>>> [...]
>>>> shm = (struct shared_mem*) shmat (shmid, NULL, 0);
>>> [...]
>>>> shm->emp = (struct emp*) shmat (shmid, NULL, 0);
>>> [...]
>>>> By doing the above, am I allocating memory for pointer to emp &
>>>> pointer to dob within the shared memory?
>>> As far as I can tell (and it's late and so I could very well be wrong),
>>> this still won't work. You have no control over where your two blocks
>>> will be mapped, therefore your pointers won't be valid in a different
>>> process where they're mapped somewhere else.
>>>
>>> I think you're not going to have any luck with this approach. Your best
>>> bet is probably to use indices into an array of objects stored in the
>>> shared memory segment. By avoiding pointers completely, the problem goes
>>> away. As the shared memory segment is of fixed size, a heap isn't that
>>> useful anyway, so it's not much more inconvenient (although I appreciate
>>> that it is still a little inconvenient).
>>>
>> Vandana can use mmap to get a pointer to the specific memory address.
>
> In theory, yes. In practice, programs are not supposed to be aware of
> their own address space layout since this collides with the political
> necessity to provide OpenBSD invented "they will never figure out how
> to exploit THAT!" pseudo security hacks[*] (like 'address space
> randomization').
>
> [*] The problem is the buggy code, not that someone may be
> capable to exploit those bugs.

I agree. She should use the array stored in the shared memory, and just
pass the index of the element from one process to another.