我如何在Linux中自动创建一个锁定的文件? [英] How do I atomically create a locked file in Linux?

查看:250
本文介绍了我如何在Linux中自动创建一个锁定的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我有很多进程需要通过网络获取文件。如果该文件已经下载,我希望它在磁盘上缓存。如果另一个进程正在下载文件,阻塞,直到完成下载。

我一直在试图找到最简单的方法来做到这一点。显而易见的方法是:

 只有在文件不存在的情况下, O_EXCL)
如果文件已经存在:
打开文件并获得排它锁
else:
下载到新创建的文件
释放锁


$ b

这个系统完成了上述目标(貌似没有)比赛条件。

,我找不到有关如何使用open()等创建在Linux中锁定的文件的文档。如果我将创建步骤拆分为:

 打开w / O_CREAT | O_EXCL 
flock

创建和锁定之间现在存在竞争条件(非创建过程

我意识到我可以使用每个文件的外部锁定文件(例如文件名+'.lock),这是我在尝试创建文件名,但这感觉..不雅观(我现在需要担心如何实际上有一个.lock后缀的文件!)

是否有原子创建并锁定它(如Windows提供的),或者几乎是标准/必需的外部lockfile方法?

解决方案

如果文件可能存在也可能不存在,那么在尝试锁定文件之前,必须先测试它的存在。但如果文件是你的互斥体,那么你不可能做到这一点,如果文件已经存在(假)和下载到新创建的文件之间的空间是不受限制的。另一个过程可能会来,并创建文件,并开始下载之前,下载开始,你会打破它。

基本上不使用fcntl锁在这里,使用存在文件本身。使用O_CREAT和O_EXCL的 open()将会失败,如果文件已经存在,告诉你别人先到了那里。


Scenario: I have many processes running that need to fetch files over the net. If the file is already downloaded, I want it cached on disk. If another process is downloading the file, block until it is finished downloading.

I've been trying to find the easiest way to do to this. The obvious way is to:

create file w/ an exclusive lock active on it only if it doesn't exist (O_CREAT | O_EXCL)
if file exists already:
   open file and acquire exclusive lock
else:
   download to newly created file
release lock

This system accomplishes the above goals with (seemingly) no race conditions

Unfortunately, I couldn't find documentation on how to use open(), etc. to create a file that is locked in Linux. If I split the create step into:

open w/ O_CREAT | O_EXCL
flock

a race condition now exists between the create and lock (non-creating process acquires the lock before the creator does).

I realize I could use an external lock file per file (e.g. filename + '.lock), which I acquire before attempting to create filename, but this feels.. inelegant (and I need to now worry about how to files that actually have a .lock suffix!)

Is there anyway to atomically create and lock it (as Windows offers) or is the external lockfile method pretty much what is standard/required?

解决方案

The race exists anyway. If the file may or may not exist then you have to test for its existence before trying to lock it. But if the file is your mutex, then you can't possibly do that and the space between "if file exists already" (false) and "download to newly created file" is unconstrained. Another process could come by and create the file and start downloading before your download begins, and you would clobber it.

Basically don't use fcntl locks here, use the existence of the file itself. open() with O_CREAT and O_EXCL will fail if the file already exists, telling you that someone else got there first.

这篇关于我如何在Linux中自动创建一个锁定的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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