Filesystem buffering


I am monitoring a diretory using glob() in a loop, to get files which are produced by an external app.

When glob() see the files, the files are not complete written. The function stat() returns the wrong file size.

Even if I do a complete read of the file using fopen() and fread() I get an feof() at the wrong size.

If I put a sleep(2) between glob() sees the file and when I open the file, all works fine.

Is there any way to ensure the file is finished written by the external app ?

(The files reside in a qnx4 filesystem managed by devb-ram. The size of the files are variable.)

Best regards


Look up advisory lock in the documentation: lockf() and fcntl() are your friends

Hello mario,

I used open(…, O_RDWR) and then lockf(fd, F_LOCK, 0)

Both functions didn’t return an error, but stat() gives the wrong file size.

I can’t specify the size at lockf() because I want to get the file size.

Best regards


Just lock the first byte of the file. When the application is finish writing it release the lock. The application that reads just wait until the lock on the first byte is gone. That still leaves a small window between the time the creator of the files creates it and apply the lock, but the size would be 0 and the reader should detect that.

Other solution is to have the writer create a second file to indicated the first fire is finish writing.

Or even better use real notification with pulses or messages.

Thanks for the help. I try to implement it next week.