看到下面这段文字,没看懂,请解释一下:
A process can never lock itself out of a file region, even when placing locks via
multiple file descriptors referring to the same file. (This contrasts with flock())
另外,如果能全面的解释一下flock与fcntl在文件锁上使用的区别,不胜感谢。
...全文
19517打赏收藏
关于flock与fcntl区别
看到下面这段文字,没看懂,请解释一下: A process can never lock itself out of a file region, even when placing locks via multiple file descriptors referring to the same file. (This contrasts with flock()) 另外,如果能全面的解释一下flock与fcntl在文件锁上使用的区别,不胜感谢。
fcntl:
A process can never lock itself out of a file region, even when placing locks via
multiple file descriptors referring to the same file. (This contrasts with flock())
flock:
Thus, a process can lock itself out of a file using flock(). As we’ll see later, this can’t
happen with record locks obtained by fcntl().
[Quote=引用 6 楼 yang79tao 的回复:]
lock itself out of a file using flock()
里面的out of不再是个短语,out应该与前面的lock结合成lock out短语
意思就是把自己锁在一个文件的外面……
[/Quote]
赞同lz的lock out
对于flock,会在同一个文件上锁住自己
对于fcntl,不会在同一个文件上锁住自己,因为:
At any time, a process can hold just one type of lock on a particular region of a
file. Placing a new lock on a region we have already locked either results in no
change (if the lock type is the same as the existing lock) or atomically converts
the existing lock to the new mode.
这英文真是,琢磨半天,可能要这样理解:
lock itself out of a file region
里面的out of是超出离开的意思,就是超出文件现在的大小范围(跟楼上理解差不多)
lock itself out of a file using flock()
里面的out of不再是个短语,out应该与前面的lock结合成lock out短语
意思就是把自己锁在一个文件的外面,比如:
fd1 = open("a.txt", O_RDWR);
fd2 = open("a.txt", O_RDWR);
flock(fd1, LOCK_EX);
flock(fd2, LOCK_EX); /* Locked out by lock on 'fd1' */
在介绍flock的时候,是这样说的:
Thus, a process can lock itself out of a file using flock(). As we’ll see later, this can’t
happen with record locks obtained by fcntl().
A file lock obtained via flock() is associated with the open file description (Section 5.4), rather
than the file descriptor or the file (i-node) itself.
However, if we use open() to obtain a second file descriptor (and associated
open file description) referring to the same file, this second descriptor is treated
independently by flock().