locking files

Chris Gonnerman chris.gonnerman at usa.net
Thu Feb 22 22:55:00 EST 2001


----- Original Message -----
From: "Grant Edwards" <grante at visi.com>
Subject: Re: locking files
> Here's a quick hack: When you want to "lock" the file, rename it.  I think
> that on most Unices and filesystems a rename is an atomic operation (not
> sure, about that).  I don't know how you're going to tell if the other
> program is using the file.

Renaming on Windows may be atomic, but on Unix/Linux/BSD etc.
it is the combination of link() and unlink() system calls, so that for a
moment the file has two names.  Also, on Unixoid OS's, if process A
has a file open and process B renames it, process A still has the same file
(inode) open and doesn't know about the rename... EXCEPT if the
file is on a remote NFS file system, in which case process A will either
start getting errors or have "undefined behavior" when it tries to use the
"open" file.  On Windoze, you may be prevented from renaming the file
if it is open in another process, or you may succeed and cause the other
process to have errors.

In other words, renaming is not necessarily a solution.







More information about the Python-list mailing list