[rochester-pm-list] flock question

Brian Mathis bmathis at directedge.com
Thu Jun 17 08:50:20 CDT 1999


Yes, if your file is flock()ed by another process, your current process
will just wait until it's free.  It will wait at the flock().

As a side note, you don't want to unlock the file, just close it.
Unlocking then closing actually creates a race condition, because not all
data is flushed to the file until the close.  A close() automatically
unlocks a file as well.

Brian Mathis


On Thu, 17 Jun 1999, Justin C. Sherrill wrote:

> Here's something I never understood about flock(), and I haven't really had
> the chance to test it out.  Could someone on this list hazard an answer for
> me?  For instance, I have this chunk of code:
> 
> open (DB_FH, ">&=$fd") or print "Cant open $fd : $!\n";
> # I have no clue what +<&= does.  It's in the Cookbook.
> flock (BD_FH, LOCK_EX);
> # do stuff to file here that I took out for space
> $db->sync;
> flock(DB_FH, LOCK_UN);
> close (DB_FH);
> 
> What I'm not sure about is this: I can get an exclusive lock here, which is
> fine.  However, this script is running as part of a CGI I wrote, and so
> multiple instances may run at the same time depending on traffic.  Do I have
> to do some sort of check for an existing exclusive lock before obtaining
> one, or will a new lock coming from another process with this script
> automatically wait until the first process releases the lock?
> 
> Justin C. Sherrill
> Rochester Road Runner Webmaster
> http://www.rochester.rr.com/
> "Think slow, type fats"
> 




More information about the Rochester-pm mailing list