Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

locking - Why fcntl start=0, len=0, whence=2 works?

According to the fcntl manual, fcntl locking with start=0, len=0, whence=2 should lock the byte range starting from the end of file (whence=2), with offset 0 (start=0) until the end of file (len=0), which in my mind means locking in total 0 bytes from EOF to EOF.

In this case I would expect that locking with those arguments would not lock anything. However, if I try (using python wrapper fcntl), the following code does lock something, and the second copy is waiting for the first to finish:

f = open('some_file', 'a+')
fcntl.lockf(f, fcntl.LOCK_EX, 0, 0, 2)
print('got the lock')
time.sleep(100)

Similarly, the code with parameters whence=2, offset=100, len=0 also works, even those in this case the byte range is backwards [EOF + 100, EOF].

What am I locking?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did some tests, and the answer seems to be as follows - whence=2 does not lock until the EOF, but until the infinity, which is not how I would read the description in the man page:

Specifying 0 for l_len has the special meaning: lock all bytes starting at the location specified by l_whence and l_start through to the end of file, no matter how large the file grows.

??


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...