开发者

how to make sure not to read a file before finishing the write to it

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 differ开发者_如何学编程ent users; the load expected is about a few hundred files created per second.


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.


Create it somewhere else, write to it, close it, then rename it - or am I missing something obvious?


You can check /proc/<pid>/fd to see if the file is still opened. If it is not listed there, you can be sure that the process is no longer using it.


maybe the lsof command can help. It lists all the opened files. $ man lsof

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜