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

扶醉桌前 提交于 2019-12-09 18:31:40

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/3267685/how-to-make-sure-not-to-read-a-file-before-finishing-the-write-to-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!