I have read enough posts on stackoverflow regarding the difference between flock
/lockf
/fcntl
but I am unable to answer the below 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 behaviour different in the two cases? I know the obvious answer that these are two different locking mechanisms. I am looking for:
- What actually
lockf()
orflock()
does to file (inode/fd
)? - As per the demo, are we allowed taking the same lock recursively?
I understand the basics of fds
and stuff so I would prefer to have a technical answer with more insights to operating system level details.
OSX 10.9.3, Python: 2.7.5