APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Locking files for shared access

Multiple users having access to the same data usually requires some sort of mechanism to give exclusive access to all or part of the data for some period of time. Locking is an extensive subject, which would need far more space than we have here, but overall there are two basic concepts: advisory locking (enforced by cooperating processes) and mandatory locking (enforced by the kernel).

Don't panic: this doesn't have to be hard.

Advisory Locking

Advisory locking does involve the kernel: the lockf() and fcntl() system calls are really the same function ;early BSD had a more limited flock(). But the kernel only restricts processes that specifically use the lockf() or fcntl() calls: any other process can read or write at will.

It's trivial to demonstrate advisory locking with Perl:

while (1) {
open(LOCKF, ">yourlockfile");
print "I'm waiting for the other guy\n";
flock LOCKF,2;
print "All set!  Go ahead and type.\n";
flock LOCKF,8;
close LOCKF;

If you run that code, you'll see this on your screen:

I'm waiting for the other guy
All set!  Go ahead and type.

Don't type anything (well, don't press ENTER). Switch to another screen and run the script again. This time you'll only see the "I'm waiting for the other guy". It will sit there waiting until you go back to the first instance and press ENTER.

You can fire up even more instances of this - ONE and only one of them will acquire the lock each time you press ENTER at one of the others.

In real use, of course, you'd have two or more entirely different scripts, but they'd all share the locking code:

open(LOCKF, ">yourlockfile");
flock LOCKF,2;
# code in here
flock LOCKF,8;
close LOCKF;

Mandatory locking

Mandatory locking is interesting because it is enabled by a hack of setting a file's set-group-id on and turning group execute off:

[root@mail root]# chmod 2760 t
[root@mail root]# ls -l t
-rwxrwS---    1 root     root          550 Dec  5 09:12 

That odd "S" is the result of this otherwise senseless combination,

Mandatory locking can be surprising though: Stevens points out that it can be circumvented by unlinking and then opening a new version of the file.

See also:

Who locked that file?
Mac OS X Invisible and Locked Files
Race conditions
Controlling concurrent runs with Perl

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Locking files for shared access

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Computers have been taught to distrust each other and will reject attempted connections most of the time. Nowadays, most computers and firewalls are utterly rude about it: it would be like asking someone to dance and having them ignore you as though you were invisible and inaudible. (Tony Lawrence)

This post tagged: