[LCP] mapping memory

Paul Gearon gearon at ieee.org
Tue Nov 4 16:05:07 EST 2008


On Mon, Nov 3, 2008 at 8:50 PM, Mark Farnell <mark.farnell at gmail.com> wrote:
<snip/>
> void *b = map(a, size);
>
> so that b is the base address of a different block (address looks
> different to a), yet if a thread changes the content pointed to b, it
> actually maps to the block pointed to by a.

What for? Why not just use the same pointer again?

> The application is that I have a process which spawns multiple POSIX
> threads.  The process malloc's a piece of memory.  Then what I intend
> to do is that for each thread, map that piece of memory to a different
> address space, each mapping can have individual mprotect operations
> that does not affect other mappings owned by other threads.
>
> However if a mapping is written to, it actually writes to the piece of
> memory malloc'ed by the master.

How can you say "not affect other mappings owned by other threads",
and then also say that writes will all go the same place? Also, I
don't think that mprotect is doing what you think it does here.

It sounds to me like you just need to allocate the memory, and then
use that same pointer in every thread. Of course, you will need to
synchronize access to that memory, so that a writer gains exclusive
access, while you allow multiple concurrent readers (which cannot
overlap with the writer). But that kind of synchronization is a common
issue with this kind of thing.

> So memcpy etc will *not* do the job.  I am not asking how to copy contents.
>
> Can this be done?

Even if it could, you'd still need to synchronize. Having different
addresses is not going to help you.

Paul



More information about the linuxCprogramming mailing list