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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I lock files 1

Status
Not open for further replies.

fortytwo

Technical User
Apr 18, 2000
206
GB
Hi,&nbsp;&nbsp;I have written an order form that uses a flat file database to write the data from the form to.&nbsp;&nbsp;I want to lock the datafile so only one person can write to it at once.&nbsp;&nbsp;Given a file handle of OFILE can anyone give me some sample code to lock and then unlock the file?<br><br>Thanks, Will. <p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br>
 
trying to beat Tom and Mike to this one......hurry hurry hurry....;^)<br><br>Pretty straight forward.....<br>use the <i>flock</i> function.<br><br>In any piece of code that might use that file, <br><br><FONT FACE=monospace><br>open(OFILE,&quot;&gt;someFile&quot;) or die &quot;Failed open, $!\n&quot;;<br>flock OFILE,2; # the '2 asks for an exclusive lock.<br><br># if another prog already has the file <i>flock</i>'d, then this<br># program will wait for it to come available.&nbsp;&nbsp;Once the file<br># is available, this prog will lock it.<br><br>print OFILE &quot;Stuff\n&quot;;<br><br>flock OFILE,8; # the '8' unlocks the file.<br></font><br><br>Note: try to keep your open-print-close cycles as tight as possible when using flock.&nbsp;&nbsp;This will minimize the time any one program or instances of the same program have the file locked.<br>'hope this helps.... <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Do I need to reset the file to the start in case another file was trying to access the file whilst I am waiting for the lock?<br><br>I read that somewhere, but I didn't understand it very well :)<br><br>Thanks <p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br>
 
'would be a good idea.&nbsp;&nbsp;I'm not real clear how it can get confused.&nbsp;&nbsp;Seems like, if you open a file, you should start at the top.&nbsp;&nbsp;But, due to the fact that I have found several references to doing something like....<br><FONT FACE=monospace><br>seek OFILE, 0, 2;<br></font><br>after the lock is established, it must be possible that you could get into the file at some point other than the beginning.......I guess maybe where the previous process left it.????<br><br>I'd be interested if anyone knows why/how this would happen???? <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
I've never found it necessary to reset the file pointer, but I suppose it doesn't hurt.<br><br>Also, I would like to note that you can unlock the file at any point using flock, but it will unlock automatically when you close it so it isn't necessary to explicitly unlock it right beforehand.<br><br>You should also be careful of concurrency problems not handled by simply flocking at read and write.&nbsp;&nbsp;If you have one instance of the script read the file and then unlock it, do some calculations, lock and write the file, it is possible that a second instance of the script also read the file while those calculations were performed and it performs its own calculations based on the read.&nbsp;&nbsp;Then, it locks the file after the first instance has finished and writes its own data, thus overwriting whatever was calculated by the first script.&nbsp;&nbsp;This could lead to false information either in your file or returned to the user operating the scripts.&nbsp;&nbsp;In this case it might be a good idea to exclusively lock the file from the read all the way through to the write. <p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
 
Well put.&nbsp;&nbsp;&nbsp;I <i>concur</i>.&nbsp;&nbsp;I did not mean to imply that you would do work on the data in between two <i>flock</i>s in the same instance of the same program.&nbsp;&nbsp;This could cause some nasty problems.&nbsp;&nbsp;I left it pretty vague.&nbsp;&nbsp;Thanks for making it more explicit. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Hi goBoating &lt;embarassed&gt;.<br><br>Mike<br><br>P.S. Forum getting busier though - so we're doing something right, people beginning to rely on Perl @ TT. <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top