• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Reading from a file that may be in use by another program

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

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!

/Tom
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tomas Nilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Tomas Nilson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks, Tom
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomas,

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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic