From owner-freebsd-hackers Sat May 27 11:21:53 2000 Delivered-To: freebsd-hackers@freebsd.org Received: from apollo.backplane.com (apollo.backplane.com [216.240.41.2]) by hub.freebsd.org (Postfix) with ESMTP id 3BBEB37B8F9 for ; Sat, 27 May 2000 11:21:47 -0700 (PDT) (envelope-from dillon@apollo.backplane.com) Received: (from dillon@localhost) by apollo.backplane.com (8.9.3/8.9.1) id LAA96710; Sat, 27 May 2000 11:21:38 -0700 (PDT) (envelope-from dillon) Date: Sat, 27 May 2000 11:21:38 -0700 (PDT) From: Matthew Dillon Message-Id: <200005271821.LAA96710@apollo.backplane.com> To: Trent Nelson Cc: hackers@FreeBSD.ORG Subject: Re: Erroneous mmap() behaviour? References: <39300E44.558B632F@student.cowan.edu.au> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG : 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