简体   繁体   English

Linux:如何确定文件是否已被其他进程更新?

[英]Linux: How do I find out if a file has been updated by another process?

I am currently watching an XML file from log4j output. 我目前正在观看log4j输出的XML文件。 I have a custom viewer that displays the log-output in GUI. 我有一个自定义查看器,在GUI中显示日志输出。 I need to watch this file as to when it gets updated so that the GUI can re-parse and update itself. 我需要观察这个文件何时更新,以便GUI可以重新解析和更新自己。 In C# there is a FileWatcher concept so on Windows no-probs, but what options do i have using C on Linux. 在C#中有一个FileWatcher概念,所以在Windows上没有probs,但我在Linux上使用C有什么选择。

Is there a standard way of doing this on both unix and linux flavours (POSIX maybe)? 有没有一种标准的方法在unix和linux版本(POSIX可能)上执行此操作?

Thanks 谢谢

Are you looking for something like inotify ? 你在寻找像inotify这样的东西吗?

Alternatively you could poll the file using stat . 或者,您可以使用stat轮询文件。

John mentioned the two main routes you can take for watching files under Unix/Linux systems: notification and polling. John提到了在Unix / Linux系统下观看文件可以采取的两条主要途径:通知和轮询。

Notification is when the system itself (usually the kernel) triggers a message to registered applications whenever the file is accessed or written to. 通知是指系统本身(通常是内核)在访问或写入文件时向注册的应用程序发出消息的时间。 This approach requires a compliant system and might not be available on older machines. 此方法需要兼容的系统,可能在旧机器上不可用。

The primary implementation of notification under Linux is inotify , which is built into kernels 2.6.13 and later and can be patched into 2.6.12 and earlier. Linux下通知的主要实现是inotify ,它内置于2.6.13及更高版本的内核中,可以修补到2.6.12及更早版本。 There's a nice IBM guide on using inotify as well as a basic set of tools and C library for accessing inotify. 有一个很好的IBM指南,关于使用inotify以及一组基本工具和C库来访问inotify。

Polling is when your program repeatedly checks a file at intervals to see if anything about that file - its size, last modification time, owner, etc. - has been changed. 轮询是指您的程序每隔一段时间反复检查一个文件,以查看该文件的任何内容 - 其大小,上次修改时间,所有者等 - 是否已更改。

There's no standard implementation of this, as it's written on a program-level rather than system-level basis, although the system call poll exists and seems to do something useful for this purpose (the man page says it's similar to select , and it waits for an event to become available on a file descriptor). 这没有标准的实现,因为它是在程序级而不是系统级的基础上编写的,尽管系统调用轮询存在并且似乎为此目的做了一些有用的事情(手册页说它类似于select ,它等待使事件在文件描述符上可用)。 You could also take the more basic approach with stat and do your checking on your own. 你也可以采取与更基本的方法统计 ,做你检查你自己的。

If you want something more portable than inotify, which is a Linux-ism, look into the File Alteration Monitor , which is older and more portable technology, but can be less performant. 如果你想要比inotify更便携的东西,这是一个Linux主义,请查看文件更改监视器 ,它是更老,更便携的技术,但性能较差。 On the other hand, it does something useful with NFS-mounted filesystems, which depending on your application could be important. 另一方面,它对NFS安装的文件系统有用,这取决于你的应用程序可能很重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到我的进程在Linux中打开的文件句柄? - How do I find the file handles that my process has opened in Linux? 如何检查已打开的文件是否已被其他进程移动或删除 - How to check if an opened file has been moved or removed by another process 如何将已动态分配的字符串复制到已动态分配的另一个字符串? - How do i copy a string that has been dynamically allocated to another string that has been dynamically allocated? 我可以将文件描述符共享给Linux上的另一个进程,还是它们是进程的本地进程? - Can I share a file descriptor to another process on linux or are they local to the process? 您如何获得进程运行了多长时间? - how do you get how long a process has been running? 是否在linux中有一个命令来查明库是否已在32位或64位模式下构建 - Is there an command in linux to find out whether a library has been build in 32 or 64 bit mode 如何确保我的进程永远不会从文件中锁定另一个进程? - How can I ensure my process never locks another process out of a file? 如何找出文件的文件描述符 - How do I find out the file descriptor of a file 尽管文件已被其他程序更改,但fread不会读取更新的值 - fread isnt reading an updated value despite the file has been changed by another program 如何将 malloc 用于使用 malloc 创建的另一个结构中的结构数组 - How do I use malloc for an array of structs inside another struct that has been created using malloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM