[lug] Polling a file under linux

Michael D. Hirsch mhirsch at nubridges.com
Mon Apr 21 12:15:16 MDT 2003


On Monday 21 April 2003 12:57 pm, Rob Judd wrote:
> I have a C/C++ application that has to connect to an IP address every
> few minutes/seconds (exact timing is not yet clear).  The IP address may
> change as the application is running, and the exact address is stored in
> /etc/myapp.config
>
> Since this file may change at any time I have to check it before each
> connection.  It seems to me that my options are
>
> 1. read the contents of the file before every connection
> 2. stat the file before every connection
>
> both of these are rather expensive, especially if connections are made
> several times a second.
>
> Is there a better way to poll a file to see when it changes?  (This is
> in C or C++)

You might want to check out FAM.  It is a library that does exactly that.  
The blurb says:
    FAM helps make GUI tools more usable by notifying them when the files
    they're interested in are created, modified, executed, and removed. 
but it works for any system interested in a file.

http://oss.sgi.com/projects/fam/

Michael



More information about the LUG mailing list