Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

locking files when writing

Status
Not open for further replies.

carlosAlberto

IS-IT--Management
Oct 9, 2001
109
GB
Hi all,

Are files that are edited from a tcl script in liunx locked so somebody else cannot edit at the same time??

If so, is it possible to say, if file1 is open, use file2. ??
 
Tcl has no built-in support for file locking. Two or more programs can have a file open simultaneously. Usually, it a bad idea for multiple programs to try modifying a file simultaneously, but simultaneous reads aren't a problem.

You've got a couple of choices for implementing file locks. The best is to get the TclX extension. (It comes standard with the TclPro and ActiveTcl distributions.) It gives you flock and funlock commands for respectively locking and unlocking files. You could then test to see if a file is locked, and if so, try the next file in sequence, etc. But these TclX commands aren't supported on Windows.

For an all-Tcl solution (or one that works on Windows and/or Macintosh as well), you're going to have to simulate this by creating a lock file when your application is ready to open a file, and delete the lock file when it's finished with it. Then, your application would test for the existance of the appropriate lock file(s) before trying to open a file. The drawback of this approach is that you need to be very careful to clean up the lock file under all circumstances (anyplace your application could abort in response to an error, premature exits, as well as "ordinary" shutdown cases), otherwise you could be left with dangling lock files. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top