>> I have 10 files which are accessed by a process. The process Changes
>>the contents of these files randomly. I want to have one more process
>>that should print a message whenever a file has been changed. The Two
>>processes are totally different and have no relation whatsoever.
>>How do i implement it very efficiently.
> There is no standard way of doing this. A simple, inefficient,
> portable way of doing it w=ould be to have the monitoring process poll
> the files and/or directories with stat(2).
> However, various implementatations have non-standard tools / APIs to
> do this (e.g., Linux has dnotify and a FAM implementation is
> available), so it might help to say which platform(s) you are aiming
Linux has dnotify, however you can only register a listener for events
through a directory file descriptor. So, you can know when _some_file has
been modified in a directory, but you have to manually test all of the files
in that directory to deduce which one(s) changed. I'd stay away from FAM,
because i don't see it widely available (unless you are admining the machine
yourself, can patch the kernel and also run the daemon). dnotify should
suffice and is automatically available on all recent Linux kernels.
Free/Open/NetBSD has kqueue, which is much nicer (for most cases at least).
With kqueue you can wait for events on any file descriptor (w/ the caveat
that file modifications do not themeslves trigger an event for any
There are two shell utilities you can use. For dnotify:
I'm working on a portable kqueue implementation which would try to mimic the
BSD kqueue semantics. Initially it is meant for Linux, and then possibly on
Solaris, et al.
Watch is useable (w/ a quirk regarding recycled inodes, which shouldn't
worry you if you're only interested in modifications, not file creation and
deletion). libkqueue is still being mocked up.