Re: Big mallocs, mmap sorrows and double buffering.

Matti Aarnio (matti.aarnio@tele.fi)
Wed, 12 Feb 1997 12:49:34 +0200 (EET)


> Matti Aarnio <matti.aarnio@tele.fi> writes:
>
> |>> icp = (char *)mmap( 0, st.st_size,
> |>> PROT_READ, MAP_FILE | MAP_PRIVATE, ifd, 0);
>
> |> Ouch! You need: MAP_FILE|MAP_SHARED
> |> You don't want a private copy of it!
>
> Since PROT_WRITE is not set this is in effect a shared mapping (and Linux
> internally handles it like a shared mapping).

Hmm.. Ok, perhaps it is so. It just isn't obvious.

> |>> ocp = (char *)mmap( 0, st.st_size,
> |>> PROT_WRITE, MAP_FILE | MAP_PRIVATE, ofd, 0);
>
> |> No, you can't mmap() for writing the file.
>
> Sure you can (if you use MAP_SHARED).

It depends -- if the file page is pre-allocated, you can
write to it via MAP_SHARED, but you can't extend a file
just by overmapping a memory region, and then storeing
into those non-backed pages. That is where you will
always need write().

I recall the `ofd' was opened with O_TRUNC flag, which means
it had zero size...

In many systems attempt to mmap() [PROT_WRITE, MAP_SHARED] on
a file with holes in it (or beyond its end) produces an error.
Here we are treading on extremely varying area of system
implementations.

> --
> Andreas Schwab "And now for something
> schwab@issan.informatik.uni-dortmund.de completely different"

/Matti Aarnio <matti.aarnio@tele.fi>