Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using non-blocking iNotify [duplicate]

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!

like image 606
Cornel Verster Avatar asked Sep 03 '13 08:09

Cornel Verster


People also ask

How does inotify work?

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.

What is inotify used for?

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.

What is inotify watch?

Inotify imposes a limit on the number of "watches" that can be in use on a system at any given time.

What is inotify tool Linux?

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.


1 Answers

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 of F_SETFL (for setting the O_ASYNC flag), F_SETOWN, and F_SETSIG in fcntl(2). The siginfo_t structure (described in sigaction(2)) that is passed to the signal handler has the following fields set: si_fd is set to the inotify file descriptor number; si_signo is set to the signal number; si_code is set to POLL_IN; and POLLIN is set in si_band.

like image 60
jxh Avatar answered Oct 07 '22 00:10

jxh