涌向VS Linux上了lockf [英] flock vs lockf on Linux

查看:246
本文介绍了涌向VS Linux上了lockf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果提供lockf 用于与羊群之间的偏移量为0,有什么区别和提供lockf 独占模式下使用时,如果有的话?

If lockf is used with a 0 offset, what are differences between flock and lockf when used in exclusive mode, if any?

我这么问是因为我读code有条件地编译在任何基于这些平台的两个功能,我想了解可能的原因。

I'm asking because I'm reading code that conditionally compiles in either of these 2 functions based on platform and I want to understand possible reasons why.

推荐答案

之间的实际差异羊群()提供lockf()是语义,适用性通过NFS和其他共享文件系统(相对于关闭和传递的行为),并咨询锁是否使用的fcntl()锁定或没有。

The practical difference between flock() and lockf() is in the semantics (behaviour with respect to closing and passing), applicability over NFS and other shared filesystems, and whether the advisory locks are visible to other processes using fcntl() locks or not.

你用简单的逻辑是基于当前平台上挑选所需的语义库。

The library you're using simply has logic to pick the desired semantics based on the current platform.

如果语义(行为上描述的传球,分叉等)是可以接受的,你应该preFER 提供lockf() / 的fcntl在()锁定羊群()在Linux中锁,仅仅是因为对NFS文件系统等,前者的作品,而后者没有。 (在BSD系统和Mac OS X,我相信你需要明确使用的fcntl()代替。)

If the semantics (behaviour over descriptor passing, forking, etc.) is acceptable, you should prefer lockf()/fcntl() locks over flock() locks in Linux, simply because the former works on NFS etc. filesystems, whereas the latter does not. (On BSDs and Mac OS X, I believe you need to explicitly use fcntl(), instead.)

在Linux中, 提供lockf() 是刚各地 包装的fcntl() ,而 羊群() 锁是独立的(而且只能在本地文件系统的工作,而不是如NFS挂载)。也就是说,一个进程可以有一个咨询专属羊群()锁在文件上,而另一个过程有一个咨询专属的fcntl()上相同的文件锁。两者都是咨询锁,但他们不进行交互。

In Linux, lockf() is just a wrapper around fcntl(), while flock() locks are separate (and will only work on local filesystems, not on e.g. NFS mounts). That is, one process can have an advisory exclusive flock() lock on a file, while another process has an advisory exclusive fcntl() lock on that same file. Both are advisory locks, but they do not interact.

在<一个href=\"https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man3/lockf.3.html\">Mac OS X 和 FreeBSD的了lockf () / 羊群() / 的fcntl()锁定所有互动,虽然开发商都建议只在接口之一在应用程序中使用。然而,只有的fcntl()门锁上的 NFS挂载(显然,只有在两个NFS客户机和服务器已被配置成支持记录锁,这是在如Web托管环境出奇少见;头痛的一个巨大的原因一些网络(框架)的开发人员)。

On Mac OS X and FreeBSD, lockf()/flock()/fcntl() locks all interact, although developers are recommended to use only one of the interfaces in an application. However, only fcntl() locks work on NFS mounts (and, obviously, only if both NFS client and server have been configured to support record locks, which is surprisingly rare in e.g. web hosting environments; a huge cause of headaches for some web (framework) developers).

POSIX没有明确规定如何提供lockf() / 羊群() / 的fcntl ()锁应该互动,并有过在过去的分歧。现在,情况已经平静下来了一点,一年可以的的说,

POSIX does not explicitly specify how lockf()/flock()/fcntl() locks should interact, and there have been differences in the past. Now, the situation has calmed down a bit, and one can approximately say that


  1. 的fcntl()锁是最可靠的。

在整个架构中,他们对如工作权的最佳机会共享文件系统 - NFS和CIFS坐骑,例如:

Across architectures, they have the best chance of working right on e.g. shared filesystems -- NFS and CIFS mounts, for example.

大多数情况下,提供lockf()为速记的实施为的fcntl()

Most often, lockf() is implemented as "shorthand" for fcntl()

另一种选择,为速记为羊群(),是可能的,但现在很少见。

The other alternative, as "shorthand" for flock(), is possible, but nowadays rare.

的fcntl()羊群()有不同的语义WRT。继承和自动发布

fcntl() and flock() have different semantics wrt. inheritance and automatic releases

的fcntl()锁跨越 pserved $ P $的exec(),但不通过继承叉()。当拥有的进程关闭的锁被释放的任何的描述指的是同一个文件。

fcntl() locks are preserved across an exec(), but not inherited across a fork(). The locks are released when the owning process closes any descriptor referring to the same file.

在Linux中,FreeBSD的,和Mac OS X,羊群()锁再加上打开的文件描述符:传递描述符还穿过锁定。 (即的手册页状态的锁在文件上,而不是在文件描述符的,这是不矛盾的。它只是意味着该锁的适用应用于文件,它仍然连接到所述描述符,在这样一种方式,重复的描述符也通过相同的锁,太)。因此,它是可能的多个进程具有相同的独家咨询群()在同一时间同一个文件锁,如果他们获得的描述符鼻祖羊群后()电话。

In Linux, FreeBSD, and MAc OS X, flock() locks are coupled with the open file descriptor: passing the descriptor also passes the lock. (The man pages state that "the lock is on the file, not on the file descriptor". This is not a contradiction. It just means that the lock applies to the file. It is still coupled to the descriptor, in such a way that duplicating the descriptor also passes the same lock, too.) Therefore, it is possible that multiple processes have the same exclusive advisory flock() lock on the same file at the same time, if they obtained the descriptor from the originator after the flock() call.

文件锁定是令人惊讶的复杂的问题。我个人只需坚持的fcntl()锁定了最好的结果。语义WRT。 的fcntl()锁是不是最容易的工作,并在某些情况下可以坦然真气;这只是我发现它产生最好的 - 最可靠,最便携,最令人吃惊的 - 结果

File locking is surprisingly complicated issue. I have personally had best results by simply sticking to fcntl() locking. The semantics wrt. fcntl() locks are not the easiest to work with, and in certain cases can be frankly infuriating; it's just that I've found it to yield the best -- most reliable, most portable, least surprising -- results.

这篇关于涌向VS Linux上了lockf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆