Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

how do I lock files

how do I lock files

how do I lock files

Hi,  I have written an order form that uses a flat file database to write the data from the form to.  I want to lock the datafile so only one person can write to it at once.  Given a file handle of OFILE can anyone give me some sample code to lock and then unlock the file?

Thanks, Will.

my test site

RE: how do I lock files

trying to beat Tom and Mike to this one......hurry hurry hurry....;^)

Pretty straight forward.....
use the flock function.

In any piece of code that might use that file,

open(OFILE,">someFile") or die "Failed open, $!\n";
flock OFILE,2; # the '2 asks for an exclusive lock.

# if another prog already has the file flock'd, then this
# program will wait for it to come available.  Once the file
# is available, this prog will lock it.

print OFILE "Stuff\n";

flock OFILE,8; # the '8' unlocks the file.

Note: try to keep your open-print-close cycles as tight as possible when using flock.  This will minimize the time any one program or instances of the same program have the file locked.
'hope this helps....

keep the rudder amid ship and beware the odd typo

RE: how do I lock files

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?

I read that somewhere, but I didn't understand it very well :)


my test site

RE: how do I lock files

'would be a good idea.  I'm not real clear how it can get confused.  Seems like, if you open a file, you should start at the top.  But, due to the fact that I have found several references to doing something like....

seek OFILE, 0, 2;

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.????

I'd be interested if anyone knows why/how this would happen????

keep the rudder amid ship and beware the odd typo

RE: how do I lock files

I've never found it necessary to reset the file pointer, but I suppose it doesn't hurt.

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.

You should also be careful of concurrency problems not handled by simply flocking at read and write.  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.  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.  This could lead to false information either in your file or returned to the user operating the scripts.  In this case it might be a good idea to exclusively lock the file from the read all the way through to the write.


Tom Anderson
CEO, Order amid Chaos, Inc.

RE: how do I lock files

Well put.   I concur.  I did not mean to imply that you would do work on the data in between two flocks in the same instance of the same program.  This could cause some nasty problems.  I left it pretty vague.  Thanks for making it more explicit.

keep the rudder amid ship and beware the odd typo

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close