Re: Text file busy

From: Tim Kientzle <kientzle_at_acm.org>
Date: Thu, 04 Sep 2003 11:20:00 -0700
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...
Received on Thu Sep 04 2003 - 09:20:04 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:21 UTC