Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a locked file with boost::interprocess::file_lock

I'd like to use boost::interprocess::file_lock to ensure that files that are written to a directory x by process P1 are not read by process P2 until they are complete. To do this, I'd like to have P1 lock the files with boost::interprocess::file_lock while it's writing them, and then unlock them when it's done. Then P2 can just skip over (and come back to) any files that are locked.

The problem I'm having is that it appears that boost::interprocess::file_lock only lets you lock files that exist. But if I first create the file, and then lock it, then there's a race condition where:

  1. P1 creates the file
  2. P2 notices the file and starts reading it
  3. P1 locks the file
  4. P1 writes some data
  5. P2 reads some data, gets to the end, and ends up with only part of P1's output.

So what I'd like to do is create a file and have it be locked as soon as it's created. Is there a way to do this using boost::interprocess::file_lock?

like image 782
Edward Loper Avatar asked Nov 15 '12 16:11

Edward Loper


People also ask

When does process 1 lock the first byte in a file?

When the file does not yet exist. Process 1 needs to write data and so must create the file and cannot lock the first byte since it does not exist yet. Meanwhile process 2 becomes ready to write data and finds the file existing and so opens the file and IS able to lock byte 0 since process 1 has started writing it data.

What is a file lock in Java?

A file lock, is a mutual exclusion utility similar to a mutex using a file. A file lock has sharable and exclusive locking capabilities and can be used with scoped_lock and sharable_lock classes. A file lock can't guarantee synchronization between threads of the same process so just use file locks to synchronize threads from different processes.

What are the best c++ (cpp) examples of boost's file_lock?

C++ (Cpp) file_lock::try_lock - 3 examples found. These are the top rated real world C++ (Cpp) examples of boost::interprocess::file_lock::try_lock extracted from open source projects. You can rate examples to help us improve the quality of examples.


3 Answers

You misunderstand the purpose of boost::interprocess::file_lock, when you create a file_lock using method boost::interprocess::file_lock test_lock("my_file"), you are not protecting the file "my_file" from reading/writing
by other processes, you just declare that you have a lock that references to the file "my_file", if other processes also have locks that reference to the same file, you can implement mutual exclusion between these locks, but these locks don't care about the read/write operation on the file "my_file", the file is just a flag

like image 90
skipper Avatar answered Oct 16 '22 09:10

skipper


No. But there is a workaround that only uses one extra empty file.

Before P2 ever tries to scan for files, create an empty file with a name that is well known to both P1 and P2. Before P2 starts scanning it will lock that empty file and release the lock when it is done scanning the directory (i.e. it shouldn't hold the lock while reading in data from files). Before P1 creates a new file it will lock that empty file and release the lock after the new file has been created and locked.

like image 44
Josh Heitzman Avatar answered Oct 16 '22 09:10

Josh Heitzman


I think how you should be able to avoid the race condition is as follows:

  1. P1 creates the file
  2. P2 notices the file: a. Locks it and b. Starts reading it
  3. P1 tries to get a lock on the file, has to wait.
  4. P2 done reading, unlocks the file
  5. P1 locks the file
  6. P1 writes some data

Let me know if that is not clear.

Thanks,

Mohit

like image 45
user3504860 Avatar answered Oct 16 '22 11:10

user3504860