I have read enough stackoverflow posts regarding the b / w flock/lockf/fcntl difference, but I cannot answer the following observation:
>>> import fcntl >>> a = open('/tmp/locktest', 'w') >>> b = open('/tmp/locktest', 'w') >>> fcntl.lockf(a, fcntl.LOCK_EX | fcntl.LOCK_NB) >>> fcntl.lockf(a, fcntl.LOCK_EX | fcntl.LOCK_NB) >>> fcntl.lockf(b, fcntl.LOCK_EX | fcntl.LOCK_NB) >>> >>> a.close() >>> b.close() >>> a = open('/tmp/locktest', 'w') >>> b = open('/tmp/locktest', 'w') >>> fcntl.flock(a, fcntl.LOCK_EX | fcntl.LOCK_NB) >>> fcntl.flock(a, fcntl.LOCK_EX | fcntl.LOCK_NB) >>> fcntl.flock(b, fcntl.LOCK_EX | fcntl.LOCK_NB) Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 35] Resource temporarily unavailable
Why is the behavior different in two cases? I know the obvious answer that these are two different locking mechanisms. I'm looking for:
- What does lockf () or flock () actually do for a file (inode / fd)?
- According to the demo, do we allow to use the same lock recursively?
I understand the basics of fds and stuff, so I'd rather get a technical answer with more details about the details of the operating system level.
OSX 10.9.3, Python: 2.7.5
source share