用`fcntl`和`flock`锁定有什么区别? [英] What is the difference between locking with `fcntl` and `flock`?

查看:50
本文介绍了用`fcntl`和`flock`锁定有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读几个小时,但无法理解两个锁之间的差异.我唯一了解的是fcntl()锁提供了可以锁定特定字节的细化锁,并且只有fcntl()支持NFS锁定.

I'm reading for hours but can't understand what is the difference between the two locks. The only thing I understand is that fcntl() lock is offering a granular lock that can lock specific bytes and that only fcntl() supports NFS locking.

据说它们的语义不同,在被dup()fork()复制时它们的行为如何,但我不知道在实践中有什么区别.

It's said that the difference is in their semantics, how do they behave when being duplicated by dup() or while fork(), but I can't understand what is the difference in practice.

我的情况是,我正在基于fork()的服务器中写入日志文件,当某事发生时,每个分叉的进程都将写入同一文件.为什么我要使用flock(),为什么要使用fcntl()锁?

My scenario is that I'm writing to a log file in a fork() based server, where every forked process is writing to the same file when something happens. Why would I want to use flock() and why would I want to use fcntl() locks?

推荐答案

我试图根据可用的文档找出差异,并得出以下结论(如果我错了,请纠正我):

I have tried to figure out the differences based on available documentation and took the following conclusions (please correct me if I am wrong):

使用fcntl()(POSIX):

With fcntl() (POSIX):

  • 您将在文件系统级别的文件上创建一个锁定记录,包括进程ID.

  • you create a lock record on the file at filesystem level including process id.

如果进程死亡或关闭与此文件相关的任何文件描述符,则系统将删除锁定记录.

If the process dies or closes any filedescriptor to this file, the lock record gets removed by the system.

如果未使用写访问权限打开文件描述符,则对独占锁定的请求将失败.

A request for an exclusive lock shall fail if the file descriptor was not opened with write access.

简单:fnctl锁定以进程<->文件关系的形式工作,而忽略文件描述符

simply: fnctl locks work as a Process <--> File relationship, ignoring filedescriptors

flock()(BSD)是不同的(Linux:自内核2.0起,flock()本身就是作为系统调用实现的,而不是在GNU C库中模拟为对fcntl的调用):

flock() (BSD) is different (Linux: since kernel 2.0, flock() is implemented as a system call in its own right rather than being emulated in the GNU C library as a call to fcntl):

  • flock()在系统的打开文件描述"上创建锁. 打开文件描述"是通过open()调用生成的.

  • flock() creates locks on systems's "Open file descriptions". "Open file descriptions" are generated by open() calls.

文件描述符(FD)是对打开文件描述"的引用.由 dup() fork()生成的FD引用相同的打开文件描述".

a filedescriptor (FD) is a reference to a "Open file description". FDs generated by dup() or fork() refer to the same "Open file description".

一个进程可以通过多次打开()文件来为一个文件生成多个打开文件描述"

a process may generate multiple "Open file descriptions" for one file by opening() the file multiple times

flock()通过FD将其锁放置在打开文件描述"上

因此,flock()可用于在进程和线程(在一个或多个进程中)之间同步文件访问.

therefore flock() may be used to synchronize file access among processes as well as threads (in one ore more processes).

请参见 flock(2),尤其是有关打开文件描述"的详细信息,请 open(2)手册页.

see flock(2) and especially open(2) man pages for details on "Open file descriptions".

在您的方案中,您可能希望使用基于fcntl()的锁,因为分支的进程将自行打开log()日志文件,并且不希望继承带有可能放置的锁的filedescriptor.

In Your scenario you probably want to use fcntl() based locks, because your forked processes will open() the logfile on their own and do not expect to inherit a filedescriptor with a possibly placed lock.

如果需要在多个线程之间进行同步(可能需要在多个进程中进行同步),并且如果系统在不使用fcntl()仿真的情况下支持它们,则应使用基于flock()的锁.然后,每个线程都需要open()文件,而不是使用dup()ed或fork()ed句柄.

If you need synchronisation among multiple threads, possibly in more than one process, you should use flock() based locks if your system supports them without emulation by fcntl(). Then every thread needs to open() the file rather than using dup()ed or fork()ed handles.

这篇关于用`fcntl`和`flock`锁定有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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