Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make sure not to read a file before finishing the write to it

Tags:

c++

c

linux

inotify

When trying to monitor a directory using inotify on Linux, as we know, we get notified as soon as the file gets created (before the other process finish writing to it)

Is there an effective way to make sure that the file is not read before writing to it is complete by the other process?

We could potentially add a delayed read; but as we all know, it is flawed.

For a little bit more clarity on the scenario; the two processes are running as different users; the load expected is about a few hundred files created per second.

like image 281
ϹοδεMεδιϲ Avatar asked Nov 14 '22 09:11

ϹοδεMεδιϲ


1 Answers

Based on your question, it sounds like you're currently monitoring the directory with the IN_CREATE (and maybe IN_OPEN) flag. Why not also use the IN_CLOSE flag so that you get notified when the file is closed? From there, it should be easy to keep track of whether something has the file open and you'll know that you don't want to try reading it yet.

like image 107
jamessan Avatar answered Dec 19 '22 11:12

jamessan