man 2 flock
说:
[L1]
Locks created by flock() are associated with an open file description (see
open(2)). This means that duplicate file descriptors (created by, for example,
fork(2) or dup(2)) refer to the same lock, and this lock may be modified or re‐
leased using any of these file descriptors. Furthermore, the lock is released ei‐
ther by an explicit LOCK_UN operation on any of these duplicate file descriptors,
or when all such file descriptors have been closed.
[L2]
If a process uses open(2) (or similar) to obtain more than one file descriptor for
the same file, these file descriptors are treated independently by flock(). An at‐
tempt to lock the file using one of these file descriptors may be denied by a lock
that the calling process has already placed via another file descriptor.
[L3]
A process may hold only one type of lock (shared or exclusive) on a file. Subse‐
quent flock() calls on an already locked file will convert an existing lock to the
new lock mode.
正如我刚刚了解到的,打开文件描述(OFD)是当您
open
文件时创建的实体。 文件描述符是对打开文件描述的引用 - 这些是您实际传递给系统调用的整数。同一 OFD 可以有多个文件描述符。
所以,我读到[L1]和[L2]说羊群是per-OFD的。从这些段落中,我预计如果您尝试两次
LOCK_EX
同一个文件,一个调用将被阻塞,无论这些调用是否在同一进程中。我预计,如果您对文件有 LOCK_SH
锁定,然后尝试通过不同的 OFD LOCK_EX
它,那么无论进程边界如何,都会再次阻塞。
但是[L3]似乎混淆了事情,似乎是说,如果你有一个
LOCK_SH
文件一个OFD,然后open
在同一过程中再次文件,然后LOCK_EX
第二个OFD......第一个锁会转换为排他锁吗?这对我来说没有任何意义,现在同一个文件有多个“独占”锁..
这里的措辞是否很草率,或者我是否遗漏了有关该系统如何工作的某些内容?
FreeBSD 手册页没有提及任何有关 OFD 的内容,它只是说:
[B1]
Locks are on files, not file descriptors. That is, file descriptors
duplicated through dup(2) or fork(2) do not result in multiple in-
stances of a lock, but rather multiple references to a single lock. If
a process holding a lock on a file forks and the child explicitly un-
locks the file, the parent will lose its lock.
如果我只是相信这个措辞,我对 [L1] 和 [L2] 的所有理解都会消失,我确实希望 LOCK_EX
处理一个文件,而同一进程在该文件上持有
LOCK_SH
只需升级
LOCK_SH
- 多个独占锁没有问题,因为它们只是对同一个锁的引用。
证据:
代码看起来是这样的(诚然我没有仔细阅读)。
两种锁类型之间的主要区别在于 而传统的记录锁与进程相关联, 打开文件描述锁与打开的文件相关联 获取它们的描述,很像获取的锁 与羊群(2).