I am writing a program which will periodically read a file which another program sometimes writes to (I haven't written the other program). I don't want my program to stop the other program from being able to read at any time.
How should I implement this so I can read the file without interfering with the other program and also get consistent data when reading? Thanks for your help!
Control both the programs from one central place and place the access to the file in a synchronized block. At any point of time either your read thread or your write thread will access this method. In case one is already using it, the other will have to wait. You can always notify the waiting thread once the active tread returns.
posted 15 years ago
Thanks for your reply! However, I have no possibility of either controlling or modifying the other program... Can I copy the file and then read that one? Will the copy operation lock the file? Any other suggestions?
Switch to Linux. Unlike Windows, Linux doesn't put silly restrictions like that on files. Multiple programs can have a file open and one program can delete a file even when another is using it (it won't actually go away until the other program is done with it)
posted 15 years ago
That is not a very feasible option as I *am* running Windows and have no choice. Do you have any ideas on how to fix this on Windows?
You could use something like a lock file maybe. The program which wants to read would first check for the presence of the lock file in the directory...if it exists it can wait a short while and then retry, this could be done in a loop so that it keeps re-trying to see if the lock file has been deleted by the writing program. The writing program would write the lock file first, then delete it when it had finished updating the file.
A bit of a work-around I known, but it would solve the problem, although how practical it is would of course depend on how oftent the file is being edited and how long those writes take to complete. It would work best where the file writes are sporadic and quick
You need concurrent access to data. The Windows filesystem sucks at concurrent access. Conclusion: files are a bad solution for your problem. If no other application or human needs direct access to this data I'd seriously consider using a database to manage the data. Databases are excellent at concurrent access, and it frees you to concentrate on your application rather than muddling through your persistence logic.