file locking

They have: 447 posts

Joined: Oct 1999

Hi. I am writing some scripts for a very active website and am having trouble finding details about how flock() works and how it should be used.

I recall reading something a year or so ago stating a file must be locked every time it is opened, otherwise other flocks will be ignored. I don't remember the details of this article and can't find any other information supporting this.

What i'd like to know is do I need to flock every file every time i open it regardless of whether its being read, written to or appended?

I also ran across this bit of code in UBB which uses a different method to lock files, im assuming to be compatible with windows machines:

sub Lock
{ local ($lockname) = @_;
local ($endtime);
$endtime = 15;
$endtime = time + $endtime;
while (-e $lockname && time < $endtime)
{ open (LOCKFILE, ">$lockname");
} #end lock sr

sub Unlock
{ local ($lockname) = @_;
close (LOCKFILE);
unlink ($lockname);
}
} # end Unlock sr

#usage
&Lock('lock.file');
open (SOMEOTHERFILE, ">$path_to_someotherfile") or die("Unable to open $path_to_someotherfile");
print SOMEOTHERFILE $someotherdata;
close (SOMEOTHERFILE);
&Unlock('lock.file');
'

A couple of things confuse me about this. First is declaring a sub-routine inside a sub-routine. I didn't know you could, and i dont see the point other than to group the 2 subs.

Second is how it actually works (if it works). It seems to me the while loop in Lock would never execute (because -e $lockname would always be false, if lock.file ever existed it would have been deleted by a previous Unlock).I also question the efficency of continually opening a file for up to 15 seconds.

Any input would be appreciated, i would like to write code that would work on windows machines, but i dont know if i like the UBB alternative.

They have: 62 posts

Joined: May 2000

open(HEH, $file);
flock(HEH, 2);
print HEH $_[2];
flock(HEH, 8);
close(HEH);

I think that works. I'm pretty sure you only have to flock when printing to a file, but not when opening one up.

They have: 447 posts

Joined: Oct 1999

top

k, i ran across this bit of data:

Quote:
CGI processes on a Unix web server can run simultaneously, and if two scripts try to open and write the same file at the same time, the file may be erased, and you'll lose all of your data. To prevent this, we've used flock(OUTF,2) in the survey.cgi to exclusively lock the survey file while we are writing to it. (The 2 means exclusive lock.) The lock will be released when your script finishes running, allowing the next CGI to access the file. This is only effective if all of the CGIs that read and write to that file also use flock; without flock, the CGI will ignore the locks of any other process and open/write/erase the file. Since flock may force the CGI to wait for another CGI to finish writing to a file, you should also reset the file pointer, using the seek function.
...If you were reading the file instead of writing to it, you'd want to do a seek(FILEHANDLE,0,0) to reset the pointer to the beginning of the file.

that partially answers some of my questions, correct me if im wrong. a file that has potential to be accessed from other processes should ALWAYS be flocked, and the file pointer needs to be set to 0 using seek but only if the file was being read from, not written to. correct?

also someone suggested to me that file locking is not a concern on windows machines as the operating system does this for you. any truth to that?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.