1q-cag
Last Updated: February 25, 2016
·
533
· maandree
Ab927485bccfcf35ba4583723876063f

Program locking

Many programs use lock files to terminate if the the same program is alreadly running.
This is better than just checking all running processes for one with the same command because that does not allow the for programs to lock only in some states.

But there is a better methods, file locks. File locks (flock) allows shared locks and exclusive locks, as well as waiting for the locks to unlock. If you apply a shared lock on a file, it will wait (or fail if using non-blocking flock) for a exclusive lock to unlock if there is one. If you are apply an exclusive lock on file, it will wait (or fail) for all other locks to unlock. So you use shared lock when you are entering a reading state, and exclusive lock if you are entering a write state.

But wait there is more. File locks are handled by the filesystem and they unlock locks that were applied by a process that has exited. So if you use file locks and the program crashes, the user does not have to manually remove a lock file.

So in summation: file locks (flock) are fail safe and supports both shared locks and exclusive locks.

But keep in mind that flock locks are only honour by flock, you can still open a file if you just dot wrap those instructions around flock instructions.

Flock is supported by C, shell script languages, Python, and probably many more languages.

Say Thanks
Respond

2 Responses
Add your response

8578
10ca55864735b3ccefb16bd93a1596a7

Right on time, I was looking for infos on the subject. Thanks :)

over 1 year ago ·
8583
Ab927485bccfcf35ba4583723876063f

@likeyn
NP. That awesome that you are looking for info and not just doing it the naïve way, there is too much of that, and often way do not even put it in the standard location: /run/lock, needless to say /run/lock is for system wide locks that applies to all users, but I guess you can create a directory for program and put users IDs in that for doing user individual locks, that is much cleaner that inside dot directories.

over 1 year ago ·
Filed Under