Monitoring file changes using select() within a loop

3.9k Views Asked by At

I am trying to write a program that will constantly keep track of the changes in a file and do several actions accordingly. I am using inotify and select within a loop to track file modifications in a non-blocking manner. The basic structure of the file tracking portion of my program is as follows.

#include <cstdio>
#include <signal.h>
#include <limits.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <string>

int main( int argc, char **argv )
{

    const char *filename = "input.txt";
    int inotfd = inotify_init();
    char buffer[1];

    int watch_desc = inotify_add_watch(inotfd, filename, IN_MODIFY);

    size_t bufsiz = sizeof(struct inotify_event) + 1;
    struct inotify_event* event = ( struct inotify_event * ) &buffer[0];

    fd_set rfds;
    FD_ZERO (&rfds);
    struct timeval timeout;

    while(1)
    {
        /*select() intitialisation.*/
        FD_SET(inotfd,&rfds); //keyboard to be listened
        timeout.tv_sec = 10;
        timeout.tv_usec = 0;

        int res=select(FD_SETSIZE,&rfds,NULL,NULL,&timeout);

        FD_ZERO(&rfds);

        printf("File Changed\n");
    } 

}

I checked the select manual page and reset the fd_set descriptor each time select() returns. However, whenever I modify the file (input.txt), this code just loops infinitely. I not very experienced using inotify and select, so, I am sure if the problem is with the way I use inotify or select. I would appreciate any hints and recommentations.

1

There are 1 best solutions below

0
On

you have to read the contents of the buffer after the select returns. if the select() finds data in the buffer, it returns. so, perform read() on that file descriptor (inotfd). read call reads the data and returns amount of bytes it read. now, the buffer is empty and in the next iteration, the select() call waits until any data is available in the buffer.

while(1)
{
// ...
    char pBuf[1024];
    res=select(FD_SETSIZE,&rfds,NULL,NULL,&timeout);
    read(inotfd,&pBuf, BUF_SIZE);
// ...
}