检查另一个程序是否打开了文件
在进行了大量研究但无法找到问题的解决方案后,我决定在 stackoverflow 上发布此文章。
嗯,我的问题有点不寻常,所以我想这就是为什么我无法找到任何答案:
我有一个程序正在将内容记录到文件中。然后我有另一个负责传输该文件。最后,我有第三个来获取文件并处理它。
我的问题是: 文件传输程序需要在文件仍在记录时发送文件。问题是,当文件传输程序到达文件的文件末尾时,并不意味着文件实际上已完成,因为它仍在记录中。
最好有一些东西来检查记录器是否仍然打开该文件,或者它是否已经关闭,以便能够判断文件结尾是否实际上是真正的文件结尾,或者是否没有更多数据可供使用尚未阅读。
希望你能帮我解决这个问题。也许您对如何解决这个问题有另一个想法。
先感谢您。
格科德
After doing tons of research and nor being able to find a solution to my problem i decided to post here on stackoverflow.
Well my problem is kind of unusual so I guess that's why I wasn't able to find any answer:
I have a program that is recording stuff to a file. Then I have another one that is responsible for transferring that file. Finally I have a third one that gets the file and processes it.
My problem is:
The file transfer program needs to send the file while it's still being recorded. The problem is that when the file transfer program reaches end of file on the file doesn't mean that the file actually is complete as it is still being recorded.
It would be nice to have something to check if the recorder has that file still open or if it already closed it to be able to judge if the end of file actually is a real end of file or if there simply aren't further data to be read yet.
Hope you can help me out with this one. Maybe you have another idea on how to solve this problem.
Thank you in advance.
GeKod
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单地说 - 你不能不使用文件系统通知机制,windows、linux 和 osx 都有这种风格。我忘记了 Windows 是如何做到这一点的,但是 linux 有“inotify”,osx 有“knotify”。
处理此问题的简单方法是,录制到 tmp 文件,录制完成后将文件移动到“准备传输文件”目录中,如果这样做可以使文件位于同一目录中文件系统,当您进行移动时,它将是原子且即时的,确保您的传输实用程序“看到”新文件时,它会完全形成并准备就绪。
或者,让您的 tmp 文件没有扩展名,然后在完成后将文件重命名为传输代理正在轮询的扩展名。
Simply put - you can't without using filesystem notification mechanisms, windows, linux and osx all have flavors of this. I forget how Windows does it off the top of my head, but linux has 'inotify' and osx has 'knotify'.
The easy way to handle this is, record to a tmp file, when the recording is done then move the file into the 'ready-to-transfer-the-file' directory, if you do this so that the files are on the same filesystem when you do the move it will be atomic and instant ensuring that any time your transfer utility 'sees' a new file, it'll be wholly formed and ready to go.
Or, just have your tmp files have no extension, then when it's done rename the file to an extension that the transfer agent is polling for.
您是否考虑过在记录器程序和抓取记录数据/文件的程序之间使用流接口?如果您有权访问还提供可靠的流信号/原语结束的流接口(例如操作系统/堆栈服务),您可以考虑替换文件接口。
Have you considered using stream interface between the recorder program and the one that grabs the recorded data/file? If you have access to a stream interface (say an OS/stack service) which also provides a reliable end of stream signal/primitive you could consider that to replace the file interface.
C 中没有可用的函数/库来执行此操作。但一个简单的替代方法是在活动结束后重命名该文件。例如,录音机可以打开名为 -
file.record
的文件,完成录音后,它可以将file.record
重命名为file.transfer ,传输程序应该查找要传输的
file.transfer
,传输完成后,它可以将文件重命名为file.read
,读者可以读取该文件最后将其重命名为file.done
!There is no functions/libraries available in C to do this. But a simple alternative is to rename the file once an activity is over. For example, recorder can open the file with name -
file.record
and once done with recording, it can rename thefile.record
tofile.transfer
and the transfer program should look forfile.transfer
to transfer and once the transfer is done, it can rename the file tofile.read
and the reader can read that and finally rename it tofile.done
!您可以检查文件是否打开,如下
所示:http://linux.about.com /library/cmd/blcmdl8_lsof.htm
you can check if file is open or not as following
refer http://linux.about.com/library/cmd/blcmdl8_lsof.htm
我认为咨询锁会有帮助。因为如果一个人使用另一个程序正在处理的文件,则该人将被阻止或收到错误。但如果强制访问,动作是好的,但结果是不可预测的,为了保持一致性,所有想要访问该文件的进程都应该遵守建议锁规则。我认为这会起作用。
当文件关闭时,锁也会被释放。其他进程可以尝试保留该文件。
I think an advisory lock will help. Since if one using the file which another program is working on it, the one will get blocked or get an error. but if you access it in force,the action is Okey, but the result is unpredictable, In order to maintain the consistency, all of the processes who want to access the file should obey the advisory lock rule. I think that will work.
When the file is closed then the lock is freed too.Other processes can try to hold the file.