My objective: To monitor a text file for modification without the monitor blocking my program, but forming part of a loop (so checking sequentially) instead.
My head says: Either find a way to run iNotify in non-blocking mode, or thread iNotfiy.
I tried the non-blocking way, and disabled O_NONBLOCK for my iNotify instance using the following command:
fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) | O_NONBLOCK);
However, when I do this and I then attempt:
length = read(fd, buffer, BUF_LEN);
It keeps on telling me that for read, a resource is temporarily unavailable.
Can anyone give me some tips on how to achieve what I want to do? Does not need to be this method, but I need the functionality as I am editing a text file with a webserver and want to read modifications into my C++ program to update variables.
Thanks in advance!
How does Inotify Work? The Inotify develops a mechanism for monitoring file system events, which watches individual files & directories. While monitoring directory, it will return events for that directory as well as for files inside the directory.
Inotify can be used to monitor individual files, or to monitor directories. When a directory is monitored, inotify will return events for the directory itself, and for files inside the directory.
Inotify imposes a limit on the number of "watches" that can be in use on a system at any given time.
inotify (short for inode notify) is a Linux kernel subsystem that notices changes in a file system (file/directory) and notifies those changes to applications.
EAGAIN
(resource temporarily unavailable) is the expected error status if there is no data available on the file descriptor being read when the file descriptor is set in non-blocking mode. Since you are using a polling loop, you can just try to read again on the next iteration.
Alternatively, you can attempt to use the signal-driven I/O notification for the inotify
file descriptor:
Since Linux 2.6.25, signal-driven I/O notification is available for
inotify
file descriptors; see the discussion ofF_SETFL
(for setting theO_ASYNC
flag),F_SETOWN
, andF_SETSIG
infcntl(2)
. Thesiginfo_t
structure (described insigaction(2)
) that is passed to the signal handler has the following fields set:si_fd
is set to theinotify
file descriptor number;si_signo
is set to the signal number;si_code
is set toPOLL_IN
; andPOLLIN
is set in si_band.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With