如何检测Linux中没有人正在写入文件?
我想知道,是否有一种简单的方法可以判断另一个实体是否打开某个文件进行写入?我没有时间连续使用 iNotify 来等待任何当前的作家完成写作。我需要进行间歇性检查。 谢谢。
I am wondering, is there a simple way to tell whether another entity has a certain file open for writing? I don't have time to use iNotify continuously to wait for any current writer to finish writing. I need to do an intermittent check.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您“没有时间连续使用 iNotify”时,您到底在做什么?首先,您应该使用
IN_CLOSE_WRITE
标志,以便 iNotify 在文件写入后关闭时仅发出一个通知。连续使用它是没有意义的。其次,如果您的时机如此关键,我认为写入文件并不是您理想的解决方案。你控制第一个作家吗?在第一个写入者关闭文件后,您是否需要担心写入文件的其他内容?What exactly are you doing where you "don't have time to use iNotify continuously"? First, you should be using the
IN_CLOSE_WRITE
flag so that iNotify just make one notification when the file gets closed after being written. Using it continuously makes no sense. Second, if your timing is that critical, I'm thinking writing to a file isn't your ideal solution. Do you control the first writer? Do you have to worry about anything else writing to the file after the first writer closes it?lsof
列出打开的文件。fuser
的工作方式也类似(文件用户),告诉您哪个用户正在使用该文件。请参阅:http://www.refining- linux.org/archives/23/16-lsof-and-fuser 简介/
lsof
LiSts Open Files.fuser
also works similarly (File USER), by telling you which user is using the file.See: http://www.refining-linux.org/archives/23/16-Introduction-to-lsof-and-fuser/
由于您似乎想要使用库式界面,而不是系统,请参阅 ofl-lib.c。 (实际上只是从 ofl 程序本身中删除了除主要功能之外的所有内容。)
Since you seem to be wanting to use a library-style interface, and not system, see ofl-lib.c. (It's really just having removed everything but the main function from the ofl program itself.)
在一般情况下你不能轻易做到这一点,即使你可以,你也不能以非活泼的方式使用这些信息(请参阅咖啡馆的评论)。
所以我想说,重新设计你的应用程序,这样你就不需要知道了。
You can't do so easily in the general case, and even if you could, you cannot use the information in a non-racy manner (see caf's comment).
So I'd say, redesign your application so you do not need to know.