[NTLUG:Discuss] File Locking

Patrick R. Michaud pmichaud at pobox.com
Thu Apr 12 23:53:23 CDT 2007


On Thu, Apr 12, 2007 at 11:12:26PM -0500, Chris Cox wrote:
> Dennis Rice wrote:
> > A co-worker would like to set up a file as being locked when a user 
> > opens it, so that others can not write to the file.  I am not aware of 
> > any permission / attribute that can do this.  Anyone have an idea or 
> > suggestion?  Appreciate.
> 
> Though I don't recommend it... you can open a file with an exclusive
> lock on it (an OS wide lock).  However, if you forget to unlock, it
> will cause things like your backups to hang since the file is forever
> locked.
> 
> man flock
> 
> (you may only have the function call... openSUSE has flock(1))

To add a bit more information... flock provides "advisory" locking
(as opposed to "mandatory" locks).  Advisory locking blocks
only those processes that attempt to set a lock with flock --
processes that aren't using flock can still open and write to
files that are "locked" with flock.

Mandatory locks are performed using the fcntl(2) and/or lockf(3)
function calls, and yes, if a process uses mandatory locking on
a file it can cause problems for other programs that may try
to access that file.

Hope this helps,

Pm



More information about the Discuss mailing list