Date: Sat, 27 May 2000 11:21:38 -0700 (PDT) From: Matthew Dillon <dillon@apollo.backplane.com> To: Trent Nelson <tpnelson@echidna.stu.cowan.edu.au> Cc: hackers@FreeBSD.ORG Subject: Re: Erroneous mmap() behaviour? Message-ID: <200005271821.LAA96710@apollo.backplane.com> References: <39300E44.558B632F@student.cowan.edu.au>
next in thread | previous in thread | raw e-mail | index | archive | help
: Can someone explain to me why mmap() returns an address map you're
:prohibited from accessing if the fd argument represents a file that has
:just been created?
:
: I have a function that calls the following, where name represents a
:file that, if it exists, is intended to get written over, and if it
:doesn't exist, is intended to get created:
: open(name,
: (O_RDWR | O_CREAT | O_TRUNC),
: (S_IRUSR | S_IWUSR));
It's perfectly acceptable to mmap() a larger space then a file's actual
size, but you will segfault on any page mapped beyond the file's EOF
until the file has been extended into it.
Writing to an mmap()'d page does NOT extend a file. Only write() or
ftruncate() can be used to extend a file.
The typical way to use mmap() in this fashion is:
open/create the file
do the mmap
ftruncate() the file to the correct number of bytes
access the mmap
Some of these operations are interchangeable. You can ftruncate() the
file to be larger first, and then mmap, or you can mmap the expected number
of bytes first, then ftruncate() the file to that size. What you cannot
do is *access* any portion of the mmap that is beyond the file's current
EOF.
-Matt
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200005271821.LAA96710>
