6
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. There's a nice IBM guide on using inotify as well as a basic set of tools and C library for accessing inotify.
Linux下通知的主要實現是inotify,它內置於2.6.13及更高版本的內核中,可以修補到2.6.12及更早版本。有一個很好的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). You could also take the more basic approach with stat and do your checking on your own.
沒有標准的實現,因為它是在程序級而不是系統級的基礎上編寫的,盡管系統調用輪詢存在並且似乎為此目的做了一些有用的事情(手冊頁說它類似於select,它等待使事件在文件描述符上可用)。您還可以使用stat的更基本方法並自行進行檢查。