Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mmap(), MAP_SHARED, MAP_ANONYMOUS and sharing memory

2,230 views
Skip to first unread message

Bahadir

unread,
Dec 23, 2007, 8:43:14 PM12/23/07
to
Hi,

I have checked various unix specs and I am puzzled about how to share
memory between two processes using mmap().

AFAIK MAP_ANONYMOUS + MAP_PRIVATE would map /dev/zero (in linux man
pages don't mention /dev/zero) so the process can get some private
zero-initialised free memory, correct?

Then what would MAP_ANONYMOUS + MAP_SHARED do? If Linux specifies no
particular file for MAP_ANONYMOUS where would the shared mapping be
found by another process? Perhaps these two flags don't get used
together?

Thanks,
Bahadir

Rainer Weikusat

unread,
Dec 24, 2007, 4:44:44 AM12/24/07
to
Bahadir <Bilgeha...@gmail.com> writes:
> I have checked various unix specs and I am puzzled about how to share
> memory between two processes using mmap().
>
> AFAIK MAP_ANONYMOUS + MAP_PRIVATE would map /dev/zero (in linux man
> pages don't mention /dev/zero) so the process can get some private
> zero-initialised free memory, correct?

Before MAP_ANONYMOUS existed, the same effect could be accomplished on
'some operating system' (notably, Solaris) by mmap'ing /dev/zero.

> Then what would MAP_ANONYMOUS + MAP_SHARED do?

Create a memory region shared among all processes created by the
process who mmap'ed it and all processes created by these processes
and ...

> If Linux specifies no particular file for MAP_ANONYMOUS where would
> the shared mapping be found by another process?

Not at all. To share memory among unrelated processing using mmap, one
would either use shm_open or mmap real files.

Bahadir

unread,
Dec 24, 2007, 10:51:31 AM12/24/07
to
On Dec 24, 9:44 am, Rainer Weikusat <rweiku...@mssgmbh.com> wrote:
>
> Create a memory region shared among all processes created by the
> process who mmap'ed it and all processes created by these processes
> and ...
>

So in summary you mean an area mapped with MAP_ANON + MAP_SHARED would
be shared among the descendants of the process. That makes sense since
only the descendants would know how to access the area.

> Not at all. To share memory among unrelated processing using mmap, one
> would either use shm_open or mmap real files.

I see that now.

Thanks,
Bahadir

0 new messages