Locked file or directory in C

I have a daemon that monitors a specific file for changes and then processes the changes made to the file.

But now I noticed that when I try to read the file (using fgets) I am not getting any data. This only happens after changes are made to the file, but if I try to read the file at any other time, I can read it ok.

I think another process is blocked by the file when I try to read it. How do I determine if a file is locked?

0


source to share


2 answers


When fgets () returns NULL for EOF, it sets a condition that needs to be cleared with clearerr () despite the extra data. (The only common type of file locking in Linux is recommendation, so most likely not your problem. Inotify is probably the best solution for detecting file changes. Http://en.wikipedia.org/wiki/Inotify )



+5


source


try checking the error with ferror () and yes - as Dave pointed out, you should call clearerr () before fgets checks in it



0


source







All Articles