File Lock
Contents
Is File lock supported in cluster filesystems?
Yes, newer version of NFS and OCFS2 both support flock.
fcntl, flock or lockf?
All of these three functions can lock a file. Which one to use? Here are some tips:
- Use one of them through you codes.
- Use the same one other codes of your existing program uses.
- Use fcntl if a locking utility is first chosen:
- fcntl is NFS safe, while flock and lockf are not.
- when the program dies for whatever reason (even kill -9, which is not trappable to explicitly remove the lock), the lock disappears.
symlink locking
symlink locking is also NFS safe. It can be used in shell scripts. The symlink doesn't have to point to a real file. You can put any interesting information in the symlink contents. See what Firefox does in it's profile.
Using flock to manage locks from shell scripts
flock is usually used the following manner:
(
flock -s 200
# ... commands executed under lock ...
) 200>/var/lock/mylockfilePython Locks
- threading.Lock() - for inter-thread communication.
- multiprocessing.Lock() - for inter-process communication. Only useful when all the processes are in the same host, because it leverage semaphores provided by the operating system.
Reference
- File locking.
- Linux file locking mechanisms - Flock, Lockf, and Fcntl.
- Profiles need to be protected from running multiple instances of mozilla.
- Linux file locking mechanisms - Mandatory Locking.
- fcntl(2) - Linux man page.
- flock(1) - Linux man page.
- flock(2) - Linux man page.
- http://www.evanfosmark.com/2009/01/cross-platform-file-locking-support-in-python/
- http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/
- http://pypi.python.org/pypi/locknix/
- http://pypi.python.org/pypi/lockfile/
- http://docs.python.org/library/multiprocessing.html
- http://docs.python.org/library/threading.html
