Search code examples
c++multithreadingfilesystemsmultiprocess

C++ how to check if file is in use - multi-threaded multi-process system


C++: Is there a way to check if a file has been opened for writing by another process/ class/ device ?

I am trying to read files from a folder that may be accessed by other processes for writing. If I read a file that is simultaneously being written on, both the read and the write process give me errors (the writing is incomplete, I might only get a header). So I must check for some type of condition before I decide whether to open that specific file. I have been using boost::filesystem to get my file list. I want compatibility with both Unix and Windows.


Solution

  • You must use a file advisory lock. In Unix, this is flock, in Windows it is LockFile.

    However, the fact that your reading process is erroring probably indicates that you have not opened the file in read-only mode in that process. You must specify the correct flags for read-only access or from the OS' perspective you have two writers.

    Both operating systems support reader-writer locks, where unlimited readers are allowed, but only in the absence of writers, and only at most one writer at a time will have access.

    Since you say your system is multi-process (ie, not multi thread), you can't use a condition variable (unless it's in interprocess shared memory). You also can't use a single writer as a coordinator unless you're willing to shuttle your data there via sockets or shared memory.