From owner-freebsd-current@FreeBSD.ORG Thu Sep 4 11:20:04 2003 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 643F216A4BF for ; Thu, 4 Sep 2003 11:20:04 -0700 (PDT) Received: from kientzle.com (h-66-166-149-50.SNVACAID.covad.net [66.166.149.50]) by mx1.FreeBSD.org (Postfix) with ESMTP id 80BC743F85 for ; Thu, 4 Sep 2003 11:20:01 -0700 (PDT) (envelope-from kientzle@acm.org) Received: from acm.org ([66.166.149.54]) by kientzle.com (8.12.9/8.12.9) with ESMTP id h84IK0kX014593; Thu, 4 Sep 2003 11:20:00 -0700 (PDT) (envelope-from kientzle@acm.org) Message-ID: <3F578250.7020100@acm.org> Date: Thu, 04 Sep 2003 11:20:00 -0700 From: Tim Kientzle User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.3.1) Gecko/20030524 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Paul Richards References: <1062686653.67807.77.camel@localhost> In-Reply-To: <1062686653.67807.77.camel@localhost> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit cc: current@freebsd.org Subject: Re: Text file busy X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: kientzle@acm.org List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Sep 2003 18:20:04 -0000 Paul Richards wrote: > Overwriting a file that's currently executing results in a "Text file > busy" error. I guess there are folks around who don't know this: When you execute a program, the program is not simply copied into memory. Instead, the kernel keeps the file open and pages the executable in as necessary. This is called "demand-paging of executables" and it's an old performance optimization that improves VM operation (executable code never needs to be copied out to swap; it can just be dumped and paged back in later) and quickens application startup (only the immediately-required parts of the application are read into memory immediately). I'm not certain, but I suspect it first appeared in Unix in the mid-1970s. In essence, the file _is_ the executable contents of memory. Overwriting it is almost always a bad idea; if the system has to swap in another part of that executable, the program is almost certain to crash. > This was something that was fixed way back on FreeBSD but it seems to be > a problem again. Depends on how you're installing the binary. It has always been safe to do either of the following: * Rename the current executable and then install the new one. * Unlink the current executable and then install the new one. Many tools that claim to "overwrite" really do the latter, which causes a certain amount of understandable confusion. (I'm pretty sure "install" does unlink/copy by default and will do rename/copy if you specify -b.) True overwriting of in-use executable files (e.g., "cat new > old") is dangerous and should be prohibited. Tim P.S. I wonder if demand-paging of executables is still a win for program startup on modern systems with dynamically-linked executables? Large reads are a lot more efficient, and it seems that dynamic linking might cause more startup thrashing. Hmmm...