简体   繁体   English

为什么 fcntl start=0, len=0, whence=2 有效?

[英]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.根据 fcntl 手册, fcntl 锁定 start=0, len=0, whence=2 应该锁定从文件末尾(whence=2)开始的字节范围,偏移量为 0(start=0)直到文件末尾(len=0),在我看来,这意味着从 EOF 到 EOF 总共锁定 0 个字节。

In this case I would expect that locking with those arguments would not lock anything.在这种情况下,我希望用那些 arguments 锁定不会锁定任何东西。 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:但是,如果我尝试(使用 python 包装器fcntl ),下面的代码确实锁定了一些东西,第二个副本正在等待第一个完成:

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].类似地,带有参数whence=2, offset=100, len=0 的代码也可以工作,即使在这种情况下字节范围是向后的[EOF + 100, EOF]。

What am I locking?我在锁定什么?

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:我做了一些测试,答案似乎如下 - whence=2 直到 EOF 才锁定,但直到无穷大,这不是我阅读手册页中描述的方式:

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.为 l_len 指定 0 具有特殊含义:锁定从 l_whence 和 l_start 指定的位置开始的所有字节,直到文件末尾,无论文件有多大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM