gpt4 book ai didi

flock(2) 的困惑文档

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:36 25 4
gpt4 key购买 nike

If a process uses open(2) (or similar) to obtain more than one descriptor for the same file, these descriptors are treated independently by flock(). An attempt 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 descriptor.

如果 flock() 独立对待描述符,为什么使用其中一个文件描述符锁定文件会被通过另一个描述符放置的锁拒绝?这里的独立是什么意思?

另外,如果我解锁其中一个描述符,其他描述符是否也会解锁?

最佳答案

flock() 独立处理意味着 flock() 在尝试修改另一个描述符时不会“询问”一个描述符。然而,这并不意味着他们是真正独立的。如果 flock() 试图锁定其中一个,而另一个已经锁定,则此尝试可能会被阻止。

将其视为 2 级机制。 flock() 一次只查看一个描述符,但最终,在尝试锁定时,系统会尝试移动到北斗七星级别并实际锁定,问题就出现了。

Also if I unlock one of the descriptor, would other descriptors unlock as well?

我不确定。这句话(下面)指出,如果一个文件有多个来自 fork(2), dup(2) 的描述符,情况确实如此。然而,在处理多个 open(2) 的第 2 段中并没有这样说,这让我相信这不是一件好事:)

来自 here :

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 released using any of these file descriptors. Furthermore, the lock is released either by an explicit LOCK_UN operation on any of these duplicate file descriptors, or when all such file descriptors have been closed.

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 attempt 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.

关于flock(2) 的困惑文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com