Text file busy

Tim Kientzle kientzle at acm.org
Thu Sep 4 11:20:04 PDT 2003


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...



More information about the freebsd-current mailing list