Controlling access to a file
Posted on 2001-06-08
I have a relatively complex situation here. What I want to do is to have a file on the hard drive. This file can be read and written to by an arbitrary number of programs on the computer. What I would like to do is maintain a system of read and write locks on the file. The way I envision it is that any program that requests read access to the file will be given access as long as no one currently has a write lock or has a pending write lock. When someone requests a write lock, no more read locks will be granted and once all existing read locks have been released the write lock will be granted. If possible I would like to suspend the execution of requestors until their lock request is granted. How can I do this?
Note that I have taken a quick look at the mutex which works well for single entry as well as the waitforsingleobject which suspend execution until the state changes. Both of these may come in handy.
Also note that I do not want to have a server managing this. It all must be done by the requesting clients, preferably with the OS doing as much of the work as possible.
I realize this is a hard question and I am willing to up the points if necessary.