如何确保不写来完成之前,读取文件 [英] how to make sure not to read a file before finishing the write to it

查看:142
本文介绍了如何确保不写来完成之前,读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图监视使用inotify的Linux上的目录,因为我们知道,我们想只要文件被创建(其他进程完成写入之前)通知

When trying to monitor a directory using inotify on Linux, as we know, we get notified as soon as the file gets created (before the other process finish writing to it)

有没有以确保该文件不能读取写入之前的有效途径是通过其他进程完成?

Is there an effective way to make sure that the file is not read before writing to it is complete by the other process?

我们可能会增加延迟读取;但我们都知道,这是有缺陷的。

We could potentially add a delayed read; but as we all know, it is flawed.

有关场景多一点点清晰;这两个进程正在运行不同的用户;预计负载约为每秒创建了几百个文件。

For a little bit more clarity on the scenario; the two processes are running as different users; the load expected is about a few hundred files created per second.

推荐答案

根据您的问题,这听起来像你目前正在监测与目录中的 IN_CREATE则(和也许 IN_OPEN )标志。为什么不也用 IN_CLOSE 标记,以便你当文件被关闭通知?从那里,它应该很容易保持的东西是否打开了该文件跟踪和你就会知道,你不想尝试阅读它。

Based on your question, it sounds like you're currently monitoring the directory with the IN_CREATE (and maybe IN_OPEN) flag. Why not also use the IN_CLOSE flag so that you get notified when the file is closed? From there, it should be easy to keep track of whether something has the file open and you'll know that you don't want to try reading it yet.

这篇关于如何确保不写来完成之前,读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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