[英]How can I tell if a file is open elsewhere in C on Linux?
How can I tell if a file is open in C? 如何判断文件是否在C中打开? I think the more technical question would be how can I retrieve the number of references to a existing file and determine with that info if it is safe to open.
我认为更技术性的问题是如何检索对现有文件的引用数量,并确定是否可以安全打开该信息。
The idea I am implementing is a file queue. 我正在实现的想法是文件队列。 You dump some files, my code processes the files.
你转储一些文件,我的代码处理文件。 I don't want to start processing until the producer closes the file descriptor.
在生产者关闭文件描述符之前,我不想开始处理。
Everything is being done in linux. 一切都在linux中完成。
Thanks, Chenz 谢谢,陈兹
Digging out that info is a lot of work(you'd have to search thorugh /proc/*/fd You'd be better off with any of: 挖掘这些信息是很多工作(你必须搜索thorugh / proc / * / fd你最好用以下任何一个:
C has facilities for handling files, but not much for getting information on them. C具有处理文件的功能,但对于获取有关它们的信息并不多。 In portable C, about the only thing you can do is try to open the file in the desired way and see if it works.
在便携式C中,您唯一能做的就是尝试以所需的方式打开文件,看看它是否有效。
generally you can't do that for variuos reasons (eg you cannot say if the file is opened with another user). 通常你不能出于variuos的原因(例如,你不能说文件是否与其他用户一起打开)。
If you can control the processes that open the file and you are try to avoid collisions by locking the file (there are many libraries on linux in order do that) 如果你可以控制打开文件的进程,你试图通过锁定文件来避免冲突(linux上有很多库就可以了)
Use the lsof command. 使用lsof命令。 (List Open Files).
(列出打开的文件)。
如果你同时控制生产者和消费者,你可以使用flock()的lockf()来锁定文件。
在大多数发行版上都有lsof命令,它显示了所有当前打开的文件,如果你的文件在同一个目录中或者有一些可识别的名称模式,你可以使用grep它的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.